Initial support for callback queries

This commit is contained in:
Hamcha 2018-12-13 11:51:11 +01:00
parent 487ffb1bf7
commit 462f289121
Signed by: hamcha
GPG key ID: A40413D21021EAEE
3 changed files with 50 additions and 7 deletions

22
api.go
View file

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

View file

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

View file

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