Telegram
Telegram 頻道讓 Mastra Agent 接收來自 Telegram 的直接訊息、群組訊息及 bot 提及。Mastra 會處理 Agent 連接及 webhook 路由;Chat SDK Telegram adapter 文件則涵蓋 bot 設定、webhook 註冊及輪詢行為。
安裝 adapter安裝 adapter 的直接連結
從 Chat SDK 安裝 Telegram adapter:
- npm
- pnpm
- Yarn
- Bun
npm install @chat-adapter/telegram
pnpm add @chat-adapter/telegram
yarn add @chat-adapter/telegram
bun add @chat-adapter/telegram
Agent 設定Agent 設定 的直接連結
將 createTelegramAdapter() 加入 Agent 的 channels.adapters 物件:
src/mastra/agents/telegram-agent.ts
import { Agent } from '@mastra/core/agent'
import { createTelegramAdapter } from '@chat-adapter/telegram'
export const telegramAgent = new Agent({
id: 'telegram-agent',
name: 'Telegram Agent',
instructions: 'Answer questions and help with tasks in Telegram.',
model: 'openai/gpt-5.6-sol',
channels: {
adapters: {
telegram: createTelegramAdapter(),
},
},
})
在 Mastra 實例中註冊 Agent:
src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { telegramAgent } from './agents/telegram-agent'
export const mastra = new Mastra({
agents: { telegramAgent },
})
Adapter 設定Adapter 設定 的直接連結
請按照 Chat SDK Telegram adapter 文件 完成 Telegram 專用設定,包括 BotFather、webhook 註冊、secret token 及輪詢模式。
Adapter 會從環境變數讀取 Telegram 憑證,例如:
.env
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
TELEGRAM_WEBHOOK_SECRET_TOKEN=your-webhook-secret-token
TELEGRAM_BOT_USERNAME=your_bot_username
Webhook URLWebhook URL 的直接連結
Mastra 會根據 Agent ID 和 adapter key 產生 Telegram webhook 路由:
/api/agents/telegram-agent/channels/telegram/webhook
使用公開 Mastra 伺服器 URL 作為 base URL:
https://your-app.example.com/api/agents/telegram-agent/channels/telegram/webhook
凡 Telegram adapter 文件要求提供 webhook URL,均請使用此 URL。Telegram 亦支援輪詢模式;如要在本機開發時使用輪詢,請按照 Chat SDK adapter 文件操作。