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

70
main.go
View File

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