tg/broker/mods: Support for message forwarding (may not work)

This commit is contained in:
Hamcha 2017-05-04 17:11:41 +02:00
parent 07ac1880ee
commit e11aff1c8e
5 changed files with 62 additions and 9 deletions

View file

@ -17,5 +17,8 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) {
case tg.CmdSendPhoto: case tg.CmdSendPhoto:
data := *(action.PhotoData) data := *(action.PhotoData)
api.SendPhoto(data) api.SendPhoto(data)
case tg.CmdForwardMessage:
data := *(action.ForwardMessageData)
api.ForwardMessage(data)
} }
} }

View file

@ -114,6 +114,17 @@ func (t Telegram) SendPhoto(data tg.ClientPhotoData) {
checkerr("SendPhoto/http.Do", err) checkerr("SendPhoto/http.Do", err)
} }
func (t Telegram) ForwardMessage(data tg.ClientForwardMessageData) {
postdata := url.Values{
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
"from_chat_id": {strconv.FormatInt(data.FromChatID, 10)},
"message_id": {strconv.FormatInt(data.MessageID, 10)},
}
_, err := http.PostForm(t.apiURL("forwardMessage"), postdata)
checkerr("ForwardMessage/http.PostForm", err)
}
// GetFile sends a "getFile" API call to Telegram's servers and fetches the file // GetFile sends a "getFile" API call to Telegram's servers and fetches the file
// specified afterward. The file will be then send back to the client that requested it // specified afterward. The file will be then send back to the client that requested it
// with the specified callback id. // with the specified callback id.

View file

@ -19,6 +19,12 @@ type Reminder struct {
TargetID int64 TargetID int64
When int64 When int64
Text string Text string
Reference *ReminderReference
}
type ReminderReference struct {
Chat int64
Message int64
} }
var reminders map[string]Reminder var reminders map[string]Reminder
@ -69,11 +75,18 @@ func remind(broker *tg.Broker, update tg.APIMessage) {
} }
id := strconv.FormatInt(update.Chat.ChatID, 36) + "-" + strconv.FormatInt(update.MessageID, 36) id := strconv.FormatInt(update.Chat.ChatID, 36) + "-" + strconv.FormatInt(update.MessageID, 36)
reminders[id] = Reminder{ reminder := Reminder{
TargetID: update.User.UserID, TargetID: update.User.UserID,
When: timestamp.Unix(), When: timestamp.Unix(),
Text: message, Text: message,
} }
if update.ReplyTo != nil {
reminder.Reference = &ReminderReference{
Chat: update.Chat.ChatID,
Message: update.ReplyTo.MessageID,
}
}
reminders[id] = reminder
savereminder() savereminder()
go runreminder(id) go runreminder(id)
@ -98,6 +111,9 @@ func runreminder(id string) {
} }
// Remind! // Remind!
broker.SendTextMessage(&tg.APIChat{ChatID: r.TargetID}, "<b>Heyla! Mi avevi chiesto di ricordarti questo:</b>\n"+r.Text, nil) broker.SendTextMessage(&tg.APIChat{ChatID: r.TargetID}, "<b>Heyla! Mi avevi chiesto di ricordarti questo:</b>\n"+r.Text, nil)
if r.Reference != nil {
broker.ForwardMessage(&tg.APIChat{ChatID: r.TargetID}, tg.APIMessage{MessageID: r.Reference.Message, Chat: &tg.APIChat{ChatID: r.Reference.Chat}})
}
// Delete reminder from pending list and save list to disk // Delete reminder from pending list and save list to disk
delete(reminders, id) delete(reminders, id)
savereminder() savereminder()

View file

@ -63,6 +63,18 @@ func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption
}) })
} }
// ForwardMessage forwards a message between chats.
func (b *Broker) ForwardMessage(chat *APIChat, message APIMessage) {
b.sendCmd(ClientCommand{
Type: CmdForwardMessage,
ForwardMessageData: &ClientForwardMessageData{
ChatID: chat.ChatID,
FromChatID: message.Chat.ChatID,
MessageID: message.MessageID,
},
})
}
// GetFile sends a file retrieval request to the Broker. // GetFile sends a file retrieval request to the Broker.
// This function is asynchronous as data will be delivered to the given callback. // This function is asynchronous as data will be delivered to the given callback.
func (b *Broker) GetFile(fileID string, fn BrokerCallback) int { func (b *Broker) GetFile(fileID string, fn BrokerCallback) int {

View file

@ -33,6 +33,9 @@ const (
// CmdSendPhoto requests the broker to send a photo to a chat // CmdSendPhoto requests the broker to send a photo to a chat
CmdSendPhoto ClientCommandType = "sendPhoto" CmdSendPhoto ClientCommandType = "sendPhoto"
// CmdForwardMessage requests the broker to forward a message between chats
CmdForwardMessage ClientCommandType = "forwardMessage"
// CmdGetFile requests the broker to get a file from Telegram // CmdGetFile requests the broker to get a file from Telegram
CmdGetFile ClientCommandType = "getFile" CmdGetFile ClientCommandType = "getFile"
) )
@ -53,6 +56,13 @@ type ClientPhotoData struct {
ReplyID *int64 `json:",omitempty"` ReplyID *int64 `json:",omitempty"`
} }
// ClientForwardMessageData is the required data for a CmdForwardMessage request
type ClientForwardMessageData struct {
ChatID int64
FromChatID int64
MessageID int64
}
// FileRequestData is the required data for a CmdGetFile request // FileRequestData is the required data for a CmdGetFile request
type FileRequestData struct { type FileRequestData struct {
FileID string FileID string
@ -63,6 +73,7 @@ type ClientCommand struct {
Type ClientCommandType Type ClientCommandType
TextMessageData *ClientTextMessageData `json:",omitempty"` TextMessageData *ClientTextMessageData `json:",omitempty"`
PhotoData *ClientPhotoData `json:",omitempty"` PhotoData *ClientPhotoData `json:",omitempty"`
ForwardMessageData *ClientForwardMessageData `json:",omitempty"`
FileRequestData *FileRequestData `json:",omitempty"` FileRequestData *FileRequestData `json:",omitempty"`
Callback *int `json:",omitempty"` Callback *int `json:",omitempty"`
} }