WhatsApp Channels 让 Mastra Agent 能够通过 WhatsApp Business Cloud 接收客户消息。Mastra 负责 Agent 接线和 webhook 路由;Chat SDK WhatsApp 适配器文档则介绍 Meta 应用设置、凭据和 webhook 配置。
安装适配器安装适配器的直接链接
从 Chat SDK 安装 WhatsApp 适配器:
- npm
- pnpm
- Yarn
- Bun
npm install @chat-adapter/whatsapp
pnpm add @chat-adapter/whatsapp
yarn add @chat-adapter/whatsapp
bun add @chat-adapter/whatsapp
Agent 配置Agent 配置的直接链接
将 createWhatsAppAdapter() 添加到 Agent 的 channels.adapters 对象:
src/mastra/agents/whatsapp-agent.ts
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:
src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { whatsappAgent } from './agents/whatsapp-agent'
export const mastra = new Mastra({
agents: { whatsappAgent },
})
适配器设置适配器设置的直接链接
按照 Chat SDK WhatsApp 适配器文档完成 WhatsApp 专用设置,包括 Meta 应用配置、访问 token、电话号码 ID、webhook 验证和消息限制。
适配器从以下环境变量中读取 WhatsApp 凭据:
.env
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 URLWebhook URL的直接链接
Mastra 根据 Agent ID 和适配器键生成 WhatsApp webhook 路由:
/api/agents/whatsapp-agent/channels/whatsapp/webhook
使用公开的 Mastra Server URL 作为基础 URL:
https://your-app.example.com/api/agents/whatsapp-agent/channels/whatsapp/webhook
使用此 URL 接收 WhatsApp 事件。生成的 Mastra 路由会处理来自 WhatsApp 的 POST 事件;请按照 Chat SDK WhatsApp 适配器文档完成 Meta 回调和 webhook 验证流程。