Files
go-telegram/examples/conversation/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
2.6 KiB
Go

package main
import (
"github.com/lukaszraczylo/go-telegram/api"
"github.com/lukaszraczylo/go-telegram/client"
"github.com/lukaszraczylo/go-telegram/dispatch"
"github.com/lukaszraczylo/go-telegram/dispatch/conversation"
msgfilter "github.com/lukaszraczylo/go-telegram/dispatch/filters/message"
)
// onMsg lifts a Filter[*api.Message] into a Filter[*api.Update].
func onMsg(f dispatch.Filter[*api.Message]) dispatch.Filter[*api.Update] {
return func(u *api.Update) bool { return u.Message != nil && f(u.Message) }
}
// notCmd matches message updates that are NOT a bot command.
var notCmd = onMsg(msgfilter.AnyCommand().Not())
// buildConv constructs the /newbot conversation. Accepts an optional Storage
// so tests can inject MemoryStorage for isolation; pass nil for the default.
func buildConv(storage conversation.Storage) *conversation.Conversation {
conv := &conversation.Conversation{
Storage: storage,
EntryPoints: []conversation.Step{{
Filter: onMsg(msgfilter.Command("/newbot")),
Handler: func(c *dispatch.Context, u *api.Update) error {
_, _ = api.SendMessage(c.Ctx, c.Bot, &api.SendMessageParams{
ChatID: api.Ptr(api.ChatIDFromInt(u.Message.Chat.ID)),
Text: "What should the bot's name be?",
})
return conversation.Next("await_name")
},
}},
States: map[conversation.State][]conversation.Step{
"await_name": {{
Filter: notCmd,
Handler: func(c *dispatch.Context, u *api.Update) error {
_, _ = api.SendMessage(c.Ctx, c.Bot, &api.SendMessageParams{
ChatID: api.Ptr(api.ChatIDFromInt(u.Message.Chat.ID)),
Text: "Got it! What's the description?",
})
return conversation.Next("await_desc")
},
}},
"await_desc": {{
Filter: notCmd,
Handler: func(c *dispatch.Context, u *api.Update) error {
_, _ = api.SendMessage(c.Ctx, c.Bot, &api.SendMessageParams{
ChatID: api.Ptr(api.ChatIDFromInt(u.Message.Chat.ID)),
Text: "Done! Your bot has been configured.",
})
return conversation.End()
},
}},
},
Exits: []conversation.Step{{
Filter: onMsg(msgfilter.Command("/cancel")),
Handler: func(c *dispatch.Context, u *api.Update) error {
_, _ = api.SendMessage(c.Ctx, c.Bot, &api.SendMessageParams{
ChatID: api.Ptr(api.ChatIDFromInt(u.Message.Chat.ID)),
Text: "Cancelled.",
})
return conversation.End()
},
}},
}
return conv
}
// register wires the conversation middleware onto the router.
func register(r *dispatch.Router, bot *client.Bot) {
_ = bot // bot available for future handlers if needed
conv := buildConv(nil)
r.Use(conv.Dispatch)
}