From 124345c02a837417c3e9b4b6d92410f6ff82fec6 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 4 May 2017 17:55:38 +0200 Subject: [PATCH] tg/mods/broker: Add chat action support --- broker/action.go | 3 +++ broker/telegram.go | 10 ++++++++++ mods/memegen.go | 2 ++ mods/snapchat.go | 2 ++ mods/stt.go | 1 + mods/unsplash.go | 2 ++ tg/broker.go | 11 +++++++++++ tg/command.go | 24 ++++++++++++++++++++++++ 8 files changed, 55 insertions(+) diff --git a/broker/action.go b/broker/action.go index 60a05fb..826b6a6 100644 --- a/broker/action.go +++ b/broker/action.go @@ -20,5 +20,8 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) { case tg.CmdForwardMessage: data := *(action.ForwardMessageData) api.ForwardMessage(data) + case tg.CmdSendChatAction: + data := *(action.ChatActionData) + api.SendChatAction(data) } } diff --git a/broker/telegram.go b/broker/telegram.go index ac776fe..cafcc03 100644 --- a/broker/telegram.go +++ b/broker/telegram.go @@ -125,6 +125,16 @@ func (t Telegram) ForwardMessage(data tg.ClientForwardMessageData) { checkerr("ForwardMessage/http.PostForm", err) } +func (t Telegram) SendChatAction(data tg.ClientChatActionData) { + postdata := url.Values{ + "chat_id": {strconv.FormatInt(data.ChatID, 10)}, + "action": {string(data.Action)}, + } + + _, err := http.PostForm(t.apiURL("sendChatAction"), postdata) + checkerr("SendChatAction/http.PostForm", err) +} + // 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 // with the specified callback id. diff --git a/mods/memegen.go b/mods/memegen.go index f81f061..7a66b3a 100644 --- a/mods/memegen.go +++ b/mods/memegen.go @@ -86,6 +86,8 @@ func memegen(broker *tg.Broker, update tg.APIMessage) { return } + broker.SendChatAction(update.Chat, tg.ActionUploadingPhoto) + //TODO Clean up this mess // Create target image diff --git a/mods/snapchat.go b/mods/snapchat.go index d578684..a7c8a61 100644 --- a/mods/snapchat.go +++ b/mods/snapchat.go @@ -86,6 +86,8 @@ func snapchat(broker *tg.Broker, update tg.APIMessage) { return } + broker.SendChatAction(update.Chat, tg.ActionUploadingPhoto) + // Create target image bounds := img.Bounds() iwidth := float64(bounds.Size().Y) / 1.6 diff --git a/mods/stt.go b/mods/stt.go index 6925950..1375418 100644 --- a/mods/stt.go +++ b/mods/stt.go @@ -57,6 +57,7 @@ func stt(broker *tg.Broker, update tg.APIMessage) { return } + broker.SendChatAction(update.Chat, tg.ActionTyping) resp, err := sttClient.Recognize(sttCtx, &speechpb.RecognizeRequest{ Config: &speechpb.RecognitionConfig{ Encoding: speechpb.RecognitionConfig_OGG_OPUS, diff --git a/mods/unsplash.go b/mods/unsplash.go index d2436e6..fbe007d 100644 --- a/mods/unsplash.go +++ b/mods/unsplash.go @@ -98,6 +98,8 @@ func unsplash(broker *tg.Broker, update tg.APIMessage) { return } + broker.SendChatAction(update.Chat, tg.ActionUploadingPhoto) + // Darken image img = imaging.AdjustBrightness(imaging.AdjustGamma(imaging.AdjustSigmoid(img, 0.5, -6.0), 0.8), -20) diff --git a/tg/broker.go b/tg/broker.go index 2b93c44..8ed3e3e 100644 --- a/tg/broker.go +++ b/tg/broker.go @@ -75,6 +75,17 @@ func (b *Broker) ForwardMessage(chat *APIChat, message APIMessage) { }) } +// SendChatAction sets a chat action for 5 seconds or less (canceled at first message sent) +func (b *Broker) SendChatAction(chat *APIChat, action ChatAction) { + b.sendCmd(ClientCommand{ + Type: CmdSendChatAction, + ChatActionData: &ClientChatActionData{ + ChatID: chat.ChatID, + Action: action, + }, + }) +} + // GetFile sends a file retrieval request to the Broker. // This function is asynchronous as data will be delivered to the given callback. func (b *Broker) GetFile(fileID string, fn BrokerCallback) int { diff --git a/tg/command.go b/tg/command.go index f14c341..021108c 100644 --- a/tg/command.go +++ b/tg/command.go @@ -38,6 +38,9 @@ const ( // CmdGetFile requests the broker to get a file from Telegram CmdGetFile ClientCommandType = "getFile" + + // CmdSendChatAction requests the broker to set a chat action (typing, etc.) + CmdSendChatAction ClientCommandType = "sendChatAction" ) // ClientTextMessageData is the required data for a CmdSendTextMessage request @@ -63,6 +66,26 @@ type ClientForwardMessageData struct { MessageID int64 } +// ClientChatActionData is the required data for a CmdSendChatAction request +type ClientChatActionData struct { + ChatID int64 + Action ChatAction +} + +// ChatAction is the action name for CmdSendChatAction requests +type ChatAction string + +const ( + ActionTyping ChatAction = "typing" + ActionUploadingPhoto ChatAction = "upload_photo" + ActionRecordingVideo ChatAction = "record_video" + ActionUploadingVideo ChatAction = "upload_video" + ActionRecordingAudio ChatAction = "record_audio" + ActionUploadingAudio ChatAction = "upload_audio" + ActionUploadingDocument ChatAction = "upload_document" + ActionFindingLocation ChatAction = "find_location" +) + // FileRequestData is the required data for a CmdGetFile request type FileRequestData struct { FileID string @@ -74,6 +97,7 @@ type ClientCommand struct { TextMessageData *ClientTextMessageData `json:",omitempty"` PhotoData *ClientPhotoData `json:",omitempty"` ForwardMessageData *ClientForwardMessageData `json:",omitempty"` + ChatActionData *ClientChatActionData `json:",omitempty"` FileRequestData *FileRequestData `json:",omitempty"` Callback *int `json:",omitempty"` }