TelegramProvider
TelegramProvider connects Mastra agents to Telegram bots through the Bot API. Register it on Mastra.channels to manage bot installations, choose webhook or polling delivery, verify webhook secrets, register commands, and route Telegram conversations to agents.
Use TelegramProvider when you want Mastra to own the bot lifecycle. For the lower-level path where you configure the Telegram adapter and webhook yourself, use createTelegramAdapter on the agent's channels.adapters.
Usage exampleDirect link to Usage example
Create a bot with BotFather, set TELEGRAM_BOT_TOKEN, and register the provider with a public base URL for webhook delivery:
import { Agent } from '@mastra/core/agent'
import { Mastra } from '@mastra/core/mastra'
import { TelegramProvider } from '@mastra/telegram'
const supportAgent = new Agent({
id: 'support',
name: 'Support agent',
instructions: 'Help users with product questions.',
model: 'openai/gpt-5-mini',
})
const telegram = new TelegramProvider({
baseUrl: 'https://your-app.example.com',
})
export const mastra = new Mastra({
agents: { supportAgent },
channels: { telegram },
})
await telegram.connect('support', {
botToken: process.env.TELEGRAM_BOT_TOKEN,
})
When botToken is omitted, connect() creates a pending installation and returns a BotFather deep link instead of activating the bot immediately.
Constructor parametersDirect link to Constructor parameters
TelegramProviderConfig combines Telegram lifecycle options, adapter behavior, and a curated subset of ChannelConfig options forwarded to each connected agent. All fields are optional.
baseUrl?:
storage?:
apiBaseUrl?:
encryptionKey?:
mode?:
allowedUpdates?:
longPolling?:
commands?:
commandScope?:
streaming?:
typingStatus?:
toolDisplay?:
tools?:
waitUntil?:
resolveWaitUntil?:
handlers?:
inlineMedia?:
inlineLinks?:
state?:
threadContext?:
chatOptions?:
resolveResourceId?:
cors?:
formatError?:
logger?:
onInstall?:
MethodsDirect link to Methods
Installation lifecycleDirect link to Installation lifecycle
connect(agentId, options)Direct link to connectagentid-options
Connects an agent to a Telegram bot. The provider validates a BotFather token with getMe, prepares the selected delivery mode and commands, then stores the installation before activating the adapter.
const result = await telegram.connect('support', {
botToken: process.env.TELEGRAM_BOT_TOKEN,
name: 'Support bot',
commands: [
{ command: 'help', description: 'Show support options' },
{ command: 'status', description: 'Check service status' },
],
})
TelegramConnectOptions fields:
botToken?:
name?:
commands?:
Returns: Promise<ChannelConnectResult>
disconnect(agentId)Direct link to disconnectagentid
Stops the active transport and removes its stored installation. In webhook mode, it also removes the Telegram webhook.
await telegram.disconnect('support')
Returns: Promise<void>
listInstallations()Direct link to listinstallations
Lists active and pending Telegram installations without exposing bot or webhook secret tokens.
const installations = await telegram.listInstallations()
Returns: Promise<ChannelInstallationInfo[]>
getInstallation(agentId)Direct link to getinstallationagentid
Returns the full installation for an agent, including sensitive bot and webhook tokens, or null when no installation exists.
const installation = await telegram.getInstallation('support')
Returns: Promise<TelegramInstallation | null>
Configuration and statusDirect link to Configuration and status
configure(credentials)Direct link to configurecredentials
Updates the Bot API origin or webhook base URL at runtime. Passing null is a no-op because Telegram credentials belong to individual bot installations.
await telegram.configure({
baseUrl: 'https://new-app.example.com',
apiBaseUrl: 'https://api.telegram.org',
})
Returns: Promise<void>
initialize()Direct link to initialize
Restores active installations from storage and rebuilds their Telegram adapters before reconnecting the registered agents. Mastra calls this during startup.
await telegram.initialize()
Returns: Promise<void>
isConfigured()Direct link to isconfigured
Returns whether at least one active Telegram installation exists.
const configured = telegram.isConfigured()
getInfo()Direct link to getinfo
Returns channel discovery metadata for the Editor UI, including connection status and the botToken and name connect-option schema.
const info = telegram.getInfo()
Returns: ChannelPlatformInfo
getAdapter(installationId)Direct link to getadapterinstallationid
Returns the live TelegramAdapter for an active installation.
const adapter = telegram.getAdapter(installationId)
Returns: TelegramAdapter | undefined
getRoutes()Direct link to getroutes
Returns the provider's unauthenticated POST /telegram/events/:webhookId route. Mastra registers this route automatically.
const routes = telegram.getRoutes()
Returns: ApiRoute[]
Delivery modesDirect link to Delivery modes
webhookregisters a per-bot webhook under/telegram/events/:webhookIdand verifies theX-Telegram-Bot-Api-Secret-Tokenheader.pollingremoves any existing webhook before starting Telegram'sgetUpdatesloop.autoselects webhooks when a public base URL is available and polling otherwise.
In production, use persistent channels storage and set encryptionKey or MASTRA_ENCRYPTION_KEY, because the in-memory fallback doesn't preserve installations across restarts.
RelatedDirect link to Related
- ChannelProvider: the interface
TelegramProviderimplements - Telegram adapter integration: the lower-level
createTelegramAdapterpath - Channels: channel concepts and agent configuration
- Channels reference: the
channelsconfig on theAgentconstructor