Bot struct is the central object in every Telebot application. It holds your Telegram credentials, owns the update channel, and dispatches incoming events to your registered handlers.
Creating a bot
NewBot calls getMe on the Telegram API to verify your token and populate Bot.Me. If you need to create a bot without a network connection (e.g. in tests), set Settings.Offline = true.
NewBot signature
*Bot or an error if the token is invalid or the Telegram API is unreachable.
Defaults applied by NewBot:
| Field | Default |
|---|---|
Updates | 100 (channel capacity) |
URL | https://api.telegram.org |
Poller | &LongPoller{} |
Client | &http.Client{Timeout: time.Minute} |
OnError | logs update.ID + error to log.Println |
Settings
Pass aSettings struct to NewBot to configure every aspect of the bot.
Your bot token obtained from @BotFather. This is the only required field.
Base URL for the Telegram Bot API. Defaults to
https://api.telegram.org. Override this to point at a local Bot API server.Capacity of the internal
Updates channel. Defaults to 100. Increase this if your bot processes bursts of messages slowly.The update source. Defaults to
&LongPoller{}. See the Poller page for all available implementations.Custom HTTP client used for all Telegram API requests. Useful for setting proxies or custom timeouts.
When
true, handlers run sequentially. ProcessUpdate blocks until the handler returns. Useful in tests.When
true, logs every outgoing API request. Use for debugging only.Default parse mode attached to every outgoing message (
ModeHTML, ModeMarkdown, ModeMarkdownV2). Can be overridden per-send.Skip the initial
getMe API call. Bot.Me is set to an empty &User{}. Intended for unit tests.Called whenever a handler returns an error. Receives the error and the context that produced it. Context may be
nil for non-handler errors.Bot struct fields
The bot’s own
User object, populated by getMe during NewBot.The bot token as passed in
Settings.The base API URL in use.
Buffered channel that receives raw
Update objects from the poller. The bot’s main loop reads from this channel in Start().The active poller instance responsible for fetching updates.
Lifecycle
Create the bot
Call
NewBot(settings). This validates your token, sets Bot.Me, and wires up the internal update channel.Start polling
Call
b.Start(). This is a blocking call — it runs the poller and dispatches updates until b.Stop() is called.The API interface
Bot implements the API interface defined in api.go. This interface exposes every method that talks to the Telegram Bot API — Send, Edit, Delete, Answer, Respond, and many more.
The Context.Bot() method returns API rather than *Bot, which makes handlers easy to test by substituting a mock implementation:
Handler groups
Useb.Group() to create a sub-router that shares a common set of middleware without affecting other handlers:
*Group are stored in the same underlying Bot.handlers map. The group only wraps its middleware around those handlers. See the Middleware page for full details.