From 11eccdd3a80cef19eb3ededd464e768850460ae0 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Sun, 25 Dec 2022 01:01:35 -0500 Subject: [PATCH] working --- main.go | 70 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 1e097ab..582a5ba 100644 --- a/main.go +++ b/main.go @@ -39,8 +39,8 @@ func main() { Chat struct { ID int `json:"id"` } - Data string } + Data string } `json:"callback_query"` }{} err = json.Unmarshal(body, &update) @@ -52,24 +52,63 @@ func main() { if update.Message != nil { go sendButtonAndDelete(update.Message.Chat.ID, update.Message.ID, update.Message.Text) } else if update.CallbackQuery != nil { - answerUrl := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", apiToken, update.CallbackQuery.ID) - resp, err := http.Get(answerUrl) - if err != nil { - log.Println(err) - return - } - body, err = io.ReadAll(resp.Body) - if err != nil { - log.Println(err) - return - } - log.Println(string(body)) + go markButtonChecked(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.ID, update.CallbackQuery.Data) + go answerCallbackQuery(update.CallbackQuery.ID) } }) port := 28923 log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) } +func markButtonChecked(chatID int, messageID int, text string) { + url := fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText", apiToken) + text = fmt.Sprintf("✓ %s", text) + sendMsgFmt := ` + { + "chat_id": "%d", + "message_id": "%d", + "text": "ok", + "reply_markup": { + "inline_keyboard": [ + [ + { + "text": "%s", + "callback_data": "%s" + } + ] + ] + } + } + ` + sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, messageID, text, text) + resp, err := http.Post(url, "application/json", strings.NewReader(sendMsgReq)) + if err != nil { + log.Println(err) + return + } + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return + } + log.Println(string(body)) +} + +func answerCallbackQuery(callbackQueryID string) { + answerUrl := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", apiToken, callbackQueryID) + resp, err := http.Get(answerUrl) + if err != nil { + log.Println(err) + return + } + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return + } + log.Println(string(body)) +} + func sendButtonAndDelete(chatID int, messageID int, text string) { url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", apiToken) sendMsgFmt := ` @@ -80,18 +119,17 @@ func sendButtonAndDelete(chatID int, messageID int, text string) { "inline_keyboard": [[ { "text": "%s", - "callback_data": "42" + "callback_data": "%s" } ]] } ` - sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, text) + sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, text, text) resp, err := http.Post(url, "application/json", strings.NewReader(sendMsgReq)) if err != nil { log.Println(err) return } - log.Println(resp) body, err := io.ReadAll(resp.Body) if err != nil { log.Println(err)