> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # WhatsApp WhatsApp Channels 讓 Mastra Agent 透過 WhatsApp Business Cloud 接收客戶訊息。Mastra 會處理 Agent 接線及 webhook 路由;Chat SDK WhatsApp adapter 文件則涵蓋 Meta 應用程式設定、憑證及 webhook 設定。 ## 安裝 adapter 從 Chat SDK 安裝 WhatsApp adapter: **npm**: ```bash npm install @chat-adapter/whatsapp ``` **pnpm**: ```bash pnpm add @chat-adapter/whatsapp ``` **Yarn**: ```bash yarn add @chat-adapter/whatsapp ``` **Bun**: ```bash bun add @chat-adapter/whatsapp ``` ## Agent 設定 將 `createWhatsAppAdapter()` 加至 Agent 的 `channels.adapters` 物件: ```typescript import { Agent } from '@mastra/core/agent' import { createWhatsAppAdapter } from '@chat-adapter/whatsapp' export const whatsappAgent = new Agent({ id: 'whatsapp-agent', name: 'WhatsApp Agent', instructions: 'Answer customer questions and help with tasks in WhatsApp.', model: 'openai/gpt-5.6-sol', channels: { adapters: { whatsapp: createWhatsAppAdapter(), }, }, }) ``` 在 Mastra instance 上註冊 Agent: ```typescript import { Mastra } from '@mastra/core' import { whatsappAgent } from './agents/whatsapp-agent' export const mastra = new Mastra({ agents: { whatsappAgent }, }) ``` ## Adapter 設定 請依照 [Chat SDK WhatsApp adapter 文件](https://chat-sdk.dev/adapters/official/whatsapp)完成 WhatsApp 專屬設定,包括 Meta 應用程式設定、存取權杖、電話號碼 ID、webhook 驗證及訊息限制。 Adapter 會從以下環境變數讀取 WhatsApp 憑證: ```bash WHATSAPP_ACCESS_TOKEN=your-meta-access-token WHATSAPP_APP_SECRET=your-meta-app-secret WHATSAPP_PHONE_NUMBER_ID=your-whatsapp-phone-number-id WHATSAPP_VERIFY_TOKEN=your-webhook-verify-token ``` ## Webhook URL Mastra 會根據 Agent ID 及 adapter key 產生 WhatsApp webhook 路由: ```text /api/agents/whatsapp-agent/channels/whatsapp/webhook ``` 使用公開的 Mastra 伺服器 URL 作為 base URL: ```text https://your-app.example.com/api/agents/whatsapp-agent/channels/whatsapp/webhook ``` 使用此 URL 接收 WhatsApp 傳入的事件。所產生的 Mastra 路由會處理來自 WhatsApp 的 POST 事件;請依照 Chat SDK WhatsApp adapter 文件完成 Meta callback 及 webhook 驗證流程。 ## 相關內容 - [Channels 概覽](https://mastra.zisheng.pro/zh-HK/docs/capabilities/channels/overview) - [更多內容](https://mastra.zisheng.pro/zh-HK/docs/capabilities/channels/other-adapters)