TelegramEx.Server (TelegramEx v1.2.1)

Copy Markdown View Source

GenServer that long-polls Telegram for updates and dispatches them to handlers.

This module implements the core polling loop that fetches updates from Telegram and routes them to the appropriate handlers in your bot module and routers.

Responsibilities

  • Long polling: Continuously fetches updates from Telegram API
  • Message parsing: Converts raw Telegram updates into typed structs
  • Handler dispatch: Routes messages and callbacks to appropriate handlers
  • FSM integration: Loads and saves per-user state before/after handlers
  • Router chain: Tries routers in order before falling back to main bot module

Handler Chain

When an update arrives, handlers are tried in this order:

  1. Each router module (in the order specified)
  2. The main bot module

If a handler returns :pass, the next handler in the chain is tried. Otherwise, the result is processed and the chain stops.

Summary

Types

Internal server state.

Functions

Returns a specification to start this module under a supervisor.

Types

chat_id()

@type chat_id() :: TelegramEx.Types.chat_id()

state()

@type state() :: %{
  bot_module: module(),
  bot_name: atom(),
  token: String.t(),
  routers: [module()],
  offset: integer()
}

Internal server state.

Tracks bot configuration, routers, and current update offset for polling.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(bot_module, bot_name, routers \\ [])

@spec start_link(module(), atom(), [module()]) :: GenServer.on_start()