Repository navigation
EN Routing and Middleware
Andrewkydev edited this page Aug 26, 2026
·
3 revisions
Русский: RU-Routing-and-Middleware
app.Command("start", start)
app.Callback("menu.*", menu)
app.Text("help", help)
app.Match(regexp.MustCompile(`(?i)^find\s+(.+)$`), search)
app.Message(anyMessage)
app.On(event.TypeJoin, welcome)
app.Fallback(unhandled)- All
On(ctx.Event.Type)handlers run in registration order. - Commands are matched against a
Commandhandler. - Callback payloads are decoded and matched against a
Callbackhandler. - Text is checked against
Text, thenMatch. - All
Messagehandlers run. -
Fallbackis used only when there are noMessagehandlers.
Commands are case-insensitive. Do not include the leading / when registering one. Configure prefixes once before startup:
app.CommandPrefixes("/", "!")kb := keyboard.Inline().Row().
Callback("Profile", payload.New("menu", "profile", nil)).
Build()
app.Command("menu", func(ctx *botctx.Context) error {
return ctx.Send("Choose an action", &kb)
})
app.Callback("menu.profile", func(ctx *botctx.Context) error {
if err := ctx.AnswerCallback("Opening"); err != nil {
ctx.Log().Warn("callback answer failed", "error", err)
}
return ctx.EditText("Profile")
})Exact routes and globs such as menu.* are supported. Exact routes always win; overlapping glob patterns are checked in registration order. Re-registering the same pattern replaces its handler without changing its position.
app.Use(loggingMiddleware)
private := app.Group(authMiddleware)
private.Command("profile", profile)The first global middleware is the outermost one. Group middleware is appended after the global chain. Registering the same command again replaces its previous handler.
For persisted localization, order the middleware explicitly:
app.Use(state.SessionMiddleware(store.Sessions(), 24*time.Hour))
app.Use(i18n.Middleware(bundle))
app.Use(fsm.Middleware())This lets localization resolve saved session locale before the platform locale and bundle default. See Localization.