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 exampleUsage 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 parametersConstructor 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?:
MethodsMethods的直接链接
Installation lifecycleInstallation lifecycle的直接链接
connect(agentId, options)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)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()listinstallations的直接链接
Lists active and pending Telegram installations without exposing bot or webhook secret tokens.
const installations = await telegram.listInstallations()
Returns: Promise<ChannelInstallationInfo[]>
getInstallation(agentId)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 statusConfiguration and status的直接链接
configure(credentials)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()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()isconfigured的直接链接
Returns whether at least one active Telegram installation exists.
const configured = telegram.isConfigured()
getInfo()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)getadapterinstallationid的直接链接
Returns the live TelegramAdapter for an active installation.
const adapter = telegram.getAdapter(installationId)
Returns: TelegramAdapter | undefined
getRoutes()getroutes的直接链接
Returns the provider's unauthenticated POST /telegram/events/:webhookId route. Mastra registers this route automatically.
const routes = telegram.getRoutes()
Returns: ApiRoute[]
Delivery modesDelivery 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.
RelatedRelated的直接链接
- ChannelProvider: the interface
TelegramProviderimplements - Telegram adapter integration: the lower-level
createTelegramAdapterpath - Channels: channel concepts and agent configuration
- Channels reference: the
channelsconfig on theAgentconstructor