Files
go-telegram/examples/callback/handlers.go
T
lukaszraczyloandGitHub cec37b7b95 chore(api): regenerate for BotFullInfo and optional chat_id (#53)
* chore(api): regenerate for BotFullInfo and optional chat_id

Telegram split BotFullInfo out of User and getMe now returns it, and
chat_id is optional in the methods that accept an ephemeral callback
reply. Regenerate the API, make MeCache cache BotFullInfo, and wrap the
now-optional chat_id arguments in the examples. This is the output of
make regen, so the weekly regen job no longer fails its api test.

* chore(api): pin the 2026-11-29 API snapshot the regen used

codegen-clean regenerates from testdata/html/latest.html, so the new
BotFullInfo output needs a matching snapshot.
2026-11-29 04:42:42 +00:00

76 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"fmt"
"strconv"
"github.com/lukaszraczylo/go-telegram/api"
"github.com/lukaszraczylo/go-telegram/client"
"github.com/lukaszraczylo/go-telegram/dispatch"
)
// register wires all handlers onto the router.
func register(r *dispatch.Router) {
r.OnCommand("/start", handleStart)
r.OnCallback(`^count:(-?\d+):(inc|dec)$`, handleCallback)
}
func handleStart(c *dispatch.Context, m *api.Message) error {
return sendMenu(c.Ctx, c.Bot, m.Chat.ID, 0)
}
func handleCallback(c *dispatch.Context, q *api.CallbackQuery) error {
groups := c.RegexMatch
current, _ := strconv.Atoi(groups[1])
if groups[2] == "inc" {
current++
} else {
current--
}
// Acknowledge the callback (removes the loading spinner).
_, _ = api.AnswerCallbackQuery(c.Ctx, c.Bot, &api.AnswerCallbackQueryParams{
CallbackQueryID: q.ID,
Text: fmt.Sprintf("counter is now %d", current),
})
// Edit the message to reflect the new state.
if q.Message == nil {
return nil
}
msg, ok := q.Message.(*api.Message)
if !ok {
return nil
}
chatID := api.ChatIDFromInt(msg.Chat.ID)
mid := msg.MessageID
_, err := api.EditMessageText(c.Ctx, c.Bot, &api.EditMessageTextParams{
ChatID: &chatID,
MessageID: &mid,
Text: fmt.Sprintf("Counter: %d", current),
ReplyMarkup: counterKeyboard(current),
})
return err
}
func sendMenu(ctx context.Context, bot *client.Bot, chatID int64, value int) error {
_, err := api.SendMessage(ctx, bot, &api.SendMessageParams{
ChatID: api.Ptr(api.ChatIDFromInt(chatID)),
Text: fmt.Sprintf("Counter: %d", value),
ReplyMarkup: counterKeyboard(value),
})
return err
}
func counterKeyboard(value int) *api.InlineKeyboardMarkup {
return &api.InlineKeyboardMarkup{
InlineKeyboard: [][]api.InlineKeyboardButton{
{
{Text: "−", CallbackData: fmt.Sprintf("count:%d:dec", value)},
{Text: "+", CallbackData: fmt.Sprintf("count:%d:inc", value)},
},
},
}
}