This commit is contained in:
Balakrishnan Balasubramanian 2022-12-25 01:01:35 -05:00
parent 454aaf8460
commit 11eccdd3a8

58
main.go
View File

@ -39,8 +39,8 @@ func main() {
Chat struct { Chat struct {
ID int `json:"id"` ID int `json:"id"`
} }
Data string
} }
Data string
} `json:"callback_query"` } `json:"callback_query"`
}{} }{}
err = json.Unmarshal(body, &update) err = json.Unmarshal(body, &update)
@ -52,22 +52,61 @@ func main() {
if update.Message != nil { if update.Message != nil {
go sendButtonAndDelete(update.Message.Chat.ID, update.Message.ID, update.Message.Text) go sendButtonAndDelete(update.Message.Chat.ID, update.Message.ID, update.Message.Text)
} else if update.CallbackQuery != nil { } else if update.CallbackQuery != nil {
answerUrl := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", apiToken, update.CallbackQuery.ID) go markButtonChecked(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.ID, update.CallbackQuery.Data)
resp, err := http.Get(answerUrl) 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 { if err != nil {
log.Println(err) log.Println(err)
return return
} }
body, err = io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
log.Println(string(body)) log.Println(string(body))
} }
})
port := 28923 func answerCallbackQuery(callbackQueryID string) {
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) 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) { func sendButtonAndDelete(chatID int, messageID int, text string) {
@ -80,18 +119,17 @@ func sendButtonAndDelete(chatID int, messageID int, text string) {
"inline_keyboard": [[ "inline_keyboard": [[
{ {
"text": "%s", "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)) resp, err := http.Post(url, "application/json", strings.NewReader(sendMsgReq))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
log.Println(resp)
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Println(err) log.Println(err)