Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrHandlerNotReady = errors.New("handler is not ready")
ErrHandlerNotReady is returned by HandleUpdate while the bot is not running in webhook mode or has not finished registering its webhook handler.
Functions ¶
func HealthModule ¶ added in v0.7.0
HealthModule returns an fx module that registers the Bot API activity liveness provider into healthfx's provider group.
func Module ¶
func Module(opts ...ModuleOption) fx.Option
Module returns an fx module that provides a Bot, runs it according to the configured delivery mode, and exposes HandleUpdate as a WebhookHandler.
func RouterModule ¶
RouterModule returns an fx module that provides a Router and runs it.
Types ¶
type Bot ¶
func New ¶
New creates a new Bot from the given configuration and options.
Options own the bot's HTTP surface: WithHTTPClient supplies the optional client, WithBotOptions passes extra telego options. telegofx installs its own client first - a shallow copy of the supplied one whose transport records getUpdates completions for the WithHealth liveness check - then the caller's options, and the zap logger last, so the caller's options take precedence.
func (*Bot) HandleUpdate ¶ added in v0.4.0
HandleUpdate delivers a raw webhook update payload to the registered handler. It returns ErrHandlerNotReady until the bot is running in webhook mode and its handler has been registered, and after the bot has stopped.
func (*Bot) LastAPIActivity ¶ added in v0.7.0
LastAPIActivity returns the time of the last completed getUpdates request - success or failure - or the zero time while polling has not completed one yet (including before the bot starts polling). A request that stalls forever never completes and never updates this timestamp. Non-polling API calls (sendMessage, getMe, ...) do not refresh it: the heartbeat tracks the receive loop, not outbound traffic.
type Config ¶
type Config struct {
// Token is the bot's API token.
Token string
// Mode selects the update-delivery mode and defaults to ModePolling.
//
// In ModeWebhook the application is responsible for mounting the
// WebhookHandler on its own HTTP(S) server and validating incoming
// requests (including the X-Telegram-Bot-Api-Secret-Token header) before
// invoking it. Registering the endpoint with Telegram's API is also an
// application concern: use telego directly, e.g. via
// telego.WithWebhookSet(ctx, &telego.SetWebhookParams{...}) before
// starting the module, and call DeleteWebhook before switching back to
// ModePolling.
Mode Mode
}
type Mode ¶ added in v0.4.0
type Mode string
Mode selects how the bot receives updates. The zero value (empty string) means ModePolling.
const ( // ModePolling receives updates via getUpdates long polling. ModePolling Mode = "polling" // ModeWebhook receives updates via a webhook. The application must mount // the WebhookHandler on its own HTTP(S) server and call DeleteWebhook // before switching back to ModePolling. ModeWebhook Mode = "webhook" )
type ModuleOption ¶ added in v0.7.0
type ModuleOption func(*moduleConfig)
ModuleOption configures Module.
func WithFSM ¶ added in v0.6.0
func WithFSM() ModuleOption
WithFSM includes the FSM module in the returned Module. The application must provide an fsm.Storage implementation in the fx graph, otherwise fx fails at startup with a missing type error.
func WithHealth ¶ added in v0.7.0
func WithHealth() ModuleOption
WithHealth includes the getUpdates activity liveness provider in the returned Module. The served check goes red once no getUpdates request has completed for three times the HTTP client timeout, catching a polling loop stalled without errors. Requires healthfx.Module() in the graph for the check to be served.
The check observes polling through the recording transport telegofx wraps around the client supplied with WithHTTPClient (see New). A telego client or caller option passed with WithBotOptions bypasses that transport, so in polling mode the check then stays red no matter how healthy the bot is. Do not combine such options with WithHealth.
func WithRouter ¶ added in v0.4.0
func WithRouter() ModuleOption
WithRouter includes the router module in the returned Module.
type Option ¶ added in v0.4.0
type Option func(*options)
Option configures New.
func WithBotOptions ¶ added in v0.7.0
WithBotOptions passes additional telego bot options through to telego.NewBot. They apply after telegofx's own options, so a caller or client option among them (telego.WithAPICaller, telego.WithHTTPClient, telego.WithFastHTTPClient) replaces what telegofx installed and hides traffic from Bot.LastAPIActivity and the WithHealth check.
func WithHTTPClient ¶ added in v0.7.0
WithHTTPClient supplies the HTTP client the bot's API traffic rides. It is optional: without it New falls back to a plain default client (no proxy, no timeout). The client carries the proxy routing and the per-request timeout that keep a stalled Telegram request from hanging the polling loop (a client without a Timeout is only warned about, never mutated).
type Router ¶
type Router struct {
*th.BotHandler
}
func NewRouter ¶
NewRouter creates the update router for bot with panic recovery registered by default.
Registration order is load-bearing: telegohandler is first-match-wins over a flat routes slice, and this constructor runs before the application registers any Handle call, so the recovery middleware always lands first and wraps every route. A panicking handler therefore surfaces as an error on telego's error path (logged through the bot logger) while the process and the dispatch loop keep running.
type WebhookHandler ¶ added in v0.4.0
WebhookHandler receives a raw webhook update payload. The application must mount it on its own HTTP(S) server and invoke it with the request body after validating the request (including the optional secret token from the X-Telegram-Bot-Api-Secret-Token header).
The handler is provided by telego. It returns an error wrapping ctx.Err() when ctx is canceled, and it may block while the internal update buffer is full. If updates must be processed after the HTTP request ends, invoke the handler with a context that outlives the request, for example context.WithoutCancel.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package fsm provides a storage-agnostic conversation-state layer for telego bots: a ctx-based Storage contract, a Store facade over it, and telegohandler predicates that read the state inside the predicate.
|
Package fsm provides a storage-agnostic conversation-state layer for telego bots: a ctx-based Storage contract, a Store facade over it, and telegohandler predicates that read the state inside the predicate. |