TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tucnak/telebot/llms.txt
Use this file to discover all available pages before exploring further.
middleware subpackage (gopkg.in/telebot.v4/middleware) provides a set of ready-to-use middleware functions that can be applied globally, to a group, or to individual handlers.
Types
MiddlewareFunc
MiddlewareFunc is defined in the core telebot package. It wraps a HandlerFunc with additional logic and returns a new HandlerFunc. Middleware runs before the endpoint handler.
RecoverFunc
Recover middleware. Receives the recovered error and the current context.
Applying Middleware
There are three ways to attach middleware in Telebot.Global — b.Use()
Applies middleware to every handler registered on the bot.
Group — g.Use()
Creates a scoped group and applies middleware only to handlers registered within it.
Per-handler — b.Handle(..., m)
Passes one or more middleware functions as trailing arguments to Handle. They run only for that specific endpoint, after any group or global middleware.
Built-in Middleware
AutoRespond
c.Respond() for every update that contains a callback query. This prevents the Telegram client from showing a loading spinner indefinitely after a button press.
Without responding to a callback, Telegram shows a loading indicator on the button for up to 30 seconds.
AutoRespond eliminates the need to call c.Respond() in every callback handler.IgnoreVia
message.Via != nil). Useful when you want to ignore forwarded-style messages that appear in groups through inline bots.
Recover
panic that occurs inside a handler and converts it into an error. If no onError callback is supplied, it falls back to the bot’s own OnError handler; if that is also unavailable, it logs to log.Default().
The recovered value must be either an error or a string; other panic values are silently discarded.
Logger
json.MarshalIndent. If no *log.Logger is provided, log.Default() is used.
The logged payload is the full tele.Update struct serialised to JSON, which includes all fields present in the incoming Telegram update.
Whitelist
nil without being called).
Blacklist
Restrict
Whitelist and Blacklist are built on. Checks whether the sender’s ID appears in RestrictConfig.Chats and dispatches to different handlers accordingly.
RestrictConfig
Middleware Execution Order
Middleware is applied in the order it is registered. When usingb.Use() together with per-handler middleware, the global chain runs first: