some cleanup

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-25 13:40:40 -05:00
parent 11eccdd3a8
commit 514688e72b

47
main.go
View File

@ -50,7 +50,8 @@ func main() {
} }
if update.Message != nil { if update.Message != nil {
go sendButtonAndDelete(update.Message.Chat.ID, update.Message.ID, update.Message.Text) go sendButton(update.Message.Chat.ID, update.Message.ID, update.Message.Text)
go DeleteMessage(update.Message.Chat.ID, update.Message.ID)
} else if update.CallbackQuery != nil { } else if update.CallbackQuery != nil {
go markButtonChecked(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.ID, update.CallbackQuery.Data) go markButtonChecked(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.ID, update.CallbackQuery.Data)
go answerCallbackQuery(update.CallbackQuery.ID) go answerCallbackQuery(update.CallbackQuery.ID)
@ -86,12 +87,7 @@ func markButtonChecked(chatID int, messageID int, text string) {
log.Println(err) log.Println(err)
return return
} }
body, err := io.ReadAll(resp.Body) logBody(resp.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
} }
func answerCallbackQuery(callbackQueryID string) { func answerCallbackQuery(callbackQueryID string) {
@ -101,27 +97,25 @@ func answerCallbackQuery(callbackQueryID string) {
log.Println(err) log.Println(err)
return return
} }
body, err := io.ReadAll(resp.Body) logBody(resp.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
} }
func sendButtonAndDelete(chatID int, messageID int, text string) { func sendButton(chatID int, messageID int, text string) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", apiToken) url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", apiToken)
sendMsgFmt := ` sendMsgFmt := `
{ {
"chat_id": "%d", "chat_id": "%d",
"text": "ok", "text": "ok",
"reply_markup": { "reply_markup": {
"inline_keyboard": [[ "inline_keyboard": [
[
{ {
"text": "%s", "text": "%s",
"callback_data": "%s" "callback_data": "%s"
} }
]] ]
]
}
} }
` `
sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, text, text) sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, text, text)
@ -130,19 +124,26 @@ func sendButtonAndDelete(chatID int, messageID int, text string) {
log.Println(err) log.Println(err)
return return
} }
body, err := io.ReadAll(resp.Body) logBody(resp.Body)
if err != nil {
log.Println(err)
return
} }
log.Println(string(body)) func DeleteMessage(chatID int, messageID int) {
deleteUrl := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", apiToken, chatID, messageID) deleteUrl := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", apiToken, chatID, messageID)
resp, err = http.Get(deleteUrl) resp, err := http.Get(deleteUrl)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
body, err = io.ReadAll(resp.Body) logBody(resp.Body)
}
func logBody(respBody io.ReadCloser) {
defer func() {
err := respBody.Close()
if err != nil {
log.Println(err)
}
}()
body, err := io.ReadAll(respBody)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return