v0.5.0

Telegram Setup

Last updated


This page covers constructing the core/telegram bot, its connection options, and the Input your handler receives.

Constructor and options


  1. bot, err := telegram.New(token,
  2. telegram.WithPollTimeout(50*time.Second),
  3. telegram.WithHTTPClient(&http.Client{Timeout: 70 * time.Second}),
  4. )

New returns token is required for an empty token and wraps SDK errors as Bot.New: .... The SDK constructor calls getMe once with a 5-second timeout, so an invalid token or an unreachable Telegram API already fails here as Bot.New: error call getMe, .... No polling starts until Start.
OptionSetsWhen neither option is passed
WithPollTimeout(d time.Duration)The long-poll timeout sent to getUpdatesSDK defaults apply
WithHTTPClient(c *http.Client)The client used for polling and uploadsSDK defaults apply

When only one option is passed, the other is filled in: the poll timeout defaults to 1 minute, and the client defaults to &http.Client{Timeout: pollTimeout}.
Set client.Timeout longer than the poll timeout. The same client serves long polling and multipart uploads; if the client timeout is equal to or shorter than the poll timeout, an idle poll is cancelled before Telegram answers and surfaces as context deadline exceeded. Large uploads also need the extra headroom.

Start and webhooks


Start(ctx) calls GetMe (a failure is returned), then GetWebhookInfo. If the bot has a webhook URL set, DeleteWebhook runs with DropPendingUpdates: false, because Telegram refuses long polling while a webhook is active. Failures in these two calls are logged and do not stop Start.
Runtime errors from the polling loop are logged. When Telegram answers with HTTP 429 (too many requests) and a retry_after, the error handler sleeps for that many seconds before polling continues.

Input


FieldTypeContent
ChatIDint64Chat the event came from
ChatNamestringGroup/channel Title, else Username, else FirstName LastName (private chats), else ""
MessageIDintThe user's message; for select callbacks, the bot's prompt message
UserID / Usernameint64 / stringSender (From); zero values when absent
Text / CaptionstringMessage text, or caption of a media message
Photo[]models.PhotoSizeAll sizes of a photo; the last entry is usually the largest
Document*models.DocumentAttached document, if any
CallbackDatastringItem tapped in a SendSelect keyboard
CallbackPicks[]stringItems chosen in a SendMultiSelect keyboard
Raw*models.UpdateThe full SDK update, for fields not copied above

CallbackData and CallbackPicks are mutually exclusive. To read a SendInput answer, compare in.Raw.Message.ReplyToMessage.ID with the prompt's ID.

中文