tg/mods/broker: Add chat action support

This commit is contained in:
Hamcha 2017-05-04 17:55:38 +02:00
parent 62106504ee
commit 124345c02a
8 changed files with 55 additions and 0 deletions

View file

@ -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)
}
}

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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)

View file

@ -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 {

View file

@ -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"`
}