From 462f289121a4e7ee8e14107dd3edc3657acd0c4d Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 13 Dec 2018 11:51:11 +0100 Subject: [PATCH] Initial support for callback queries --- api.go | 22 +++++++++++++++++----- command.go | 9 +++++++++ telegram.go | 26 ++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/api.go b/api.go index 8a570fe..bcefde5 100644 --- a/api.go +++ b/api.go @@ -135,9 +135,10 @@ type APILocation struct { // APIUpdate represents the "Update" JSON structure type APIUpdate struct { - UpdateID int64 `json:"update_id"` - Message *APIMessage `json:"message"` - Inline *APIInlineQuery `json:"inline_query,omitempty"` + UpdateID int64 `json:"update_id"` + Message *APIMessage `json:"message"` + Inline *APIInlineQuery `json:"inline_query,omitempty"` + Callback *APICallbackQuery `json:"callback_query,omitempty"` } // APIFile represents the "File" JSON structure @@ -185,8 +186,9 @@ type APIInlineKeyboardMarkup struct { // APIInlineKeyboardButton is an inline message button type APIInlineKeyboardButton struct { - Text string `json:"text"` - URL string `json:"url,omitempty"` + Text string `json:"text"` + URL string `json:"url,omitempty"` + CallbackData string `json:"callback_data,omitempty"` } // APIInputMediaPhoto is a media photo element (already on telegram servers or via HTTP URL) for albums and other cached pictures @@ -196,3 +198,13 @@ type APIInputMediaPhoto struct { Caption string `json:"caption,omitempty"` ParseMode string `json:"parse_mode,omitempty"` } + +// APICallbackQuery is a callback query triggered by an inline button +type APICallbackQuery struct { + ID string `json:"id"` + User APIAudio `json:"from"` + Message *APIMessage `json:"message,omitempty"` + InlineID *string `json:"inline_message_id,omitempty"` + ChatID string `json:"chat_instance"` + Data *string `json:"data,omitempty"` +} diff --git a/command.go b/command.go index 8b80a94..b5f3fcd 100644 --- a/command.go +++ b/command.go @@ -79,6 +79,15 @@ type ClientChatActionData struct { Action ChatAction } +// ClientCallbackQueryData is the required data for a CmdAnswerCallback request +type ClientCallbackQueryData struct { + QueryID string + Text string + ShowAlert bool + URL string + CacheTime int64 +} + // ClientAlbumData is the required data for a CmdSendAlbum request type ClientAlbumData struct { ChatID int64 diff --git a/telegram.go b/telegram.go index f0c0ee0..896ea4f 100644 --- a/telegram.go +++ b/telegram.go @@ -158,10 +158,10 @@ func (t Telegram) SendAlbum(data ClientAlbumData) { "media": {string(jsonmedia)}, } if data.Silent { - postdata["disable_notification"] = []string{"true"} + postdata.Set("disable_notification", "true") } if data.ReplyID != nil { - postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)} + postdata.Set("reply_to_message_id", strconv.FormatInt(*(data.ReplyID), 10)) } _, err = http.PostForm(t.apiURL("sendMediaGroup"), postdata) @@ -191,6 +191,28 @@ func (t Telegram) SendChatAction(data ClientChatActionData) { checkerr("SendChatAction/http.PostForm", err) } +// AnswerCallback stops the progress bar after a callback query is initiated +func (t Telegram) AnswerCallback(data ClientCallbackQueryData) { + postdata := url.Values{ + "callback_query_id": {data.QueryID}, + } + if data.Text != "" { + postdata.Set("text", data.Text) + } + if data.URL != "" { + postdata.Set("url", data.URL) + } + if data.ShowAlert { + postdata.Set("show_alert", "true") + } + if data.CacheTime != 0 { + postdata.Set("cache_time", strconv.FormatInt(data.CacheTime, 10)) + } + + _, err := http.PostForm(t.apiURL("answerCallbackQuery"), postdata) + checkerr("AnswerCallback/http.PostForm", err) +} + // AnswerInlineQuery replies to an inline query func (t Telegram) AnswerInlineQuery(data InlineQueryResponse) error { jsonresults, err := json.Marshal(data.Results)