> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # WhatsApp WhatsApp Channels 让 Mastra Agent 能够通过 WhatsApp Business Cloud 接收客户消息。Mastra 负责 Agent 接线和 webhook 路由;Chat SDK WhatsApp 适配器文档则介绍 Meta 应用设置、凭据和 webhook 配置。 ## 安装适配器 从 Chat SDK 安装 WhatsApp 适配器: **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 实例上注册该 Agent: ```typescript import { Mastra } from '@mastra/core' import { whatsappAgent } from './agents/whatsapp-agent' export const mastra = new Mastra({ agents: { whatsappAgent }, }) ``` ## 适配器设置 按照 [Chat SDK WhatsApp 适配器文档](https://chat-sdk.dev/adapters/official/whatsapp)完成 WhatsApp 专用设置,包括 Meta 应用配置、访问 token、电话号码 ID、webhook 验证和消息限制。 适配器从以下环境变量中读取 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 和适配器键生成 WhatsApp webhook 路由: ```text /api/agents/whatsapp-agent/channels/whatsapp/webhook ``` 使用公开的 Mastra Server URL 作为基础 URL: ```text https://your-app.example.com/api/agents/whatsapp-agent/channels/whatsapp/webhook ``` 使用此 URL 接收 WhatsApp 事件。生成的 Mastra 路由会处理来自 WhatsApp 的 POST 事件;请按照 Chat SDK WhatsApp 适配器文档完成 Meta 回调和 webhook 验证流程。 ## 相关内容 - [Channels 概览](https://mastra.zisheng.pro/docs/capabilities/channels/overview) - [更多适配器](https://mastra.zisheng.pro/docs/capabilities/channels/other-adapters)