> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Telegram Telegram 頻道讓 Mastra Agent 接收來自 Telegram 的直接訊息、群組訊息及 bot 提及。Mastra 會處理 Agent 連接及 webhook 路由;Chat SDK Telegram adapter 文件則涵蓋 bot 設定、webhook 註冊及輪詢行為。 ## 安裝 adapter 從 Chat SDK 安裝 Telegram adapter: **npm**: ```bash npm install @chat-adapter/telegram ``` **pnpm**: ```bash pnpm add @chat-adapter/telegram ``` **Yarn**: ```bash yarn add @chat-adapter/telegram ``` **Bun**: ```bash bun add @chat-adapter/telegram ``` ## Agent 設定 將 `createTelegramAdapter()` 加入 Agent 的 `channels.adapters` 物件: ```typescript 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: ```typescript import { Mastra } from '@mastra/core' import { telegramAgent } from './agents/telegram-agent' export const mastra = new Mastra({ agents: { telegramAgent }, }) ``` ## Adapter 設定 請按照 [Chat SDK Telegram adapter 文件](https://chat-sdk.dev/adapters/official/telegram) 完成 Telegram 專用設定,包括 BotFather、webhook 註冊、secret token 及輪詢模式。 Adapter 會從環境變數讀取 Telegram 憑證,例如: ```bash TELEGRAM_BOT_TOKEN=your-telegram-bot-token TELEGRAM_WEBHOOK_SECRET_TOKEN=your-webhook-secret-token TELEGRAM_BOT_USERNAME=your_bot_username ``` ## Webhook URL Mastra 會根據 Agent ID 和 adapter key 產生 Telegram webhook 路由: ```text /api/agents/telegram-agent/channels/telegram/webhook ``` 使用公開 Mastra 伺服器 URL 作為 base URL: ```text https://your-app.example.com/api/agents/telegram-agent/channels/telegram/webhook ``` 凡 Telegram adapter 文件要求提供 webhook URL,均請使用此 URL。Telegram 亦支援輪詢模式;如要在本機開發時使用輪詢,請按照 Chat SDK adapter 文件操作。 ## 相關內容 - [頻道概覽](https://mastra.zisheng.pro/zh-HK/docs/capabilities/channels/overview) - [更多](https://mastra.zisheng.pro/zh-HK/docs/capabilities/channels/other-adapters)