@chat-adapter/messenger

中文文档
4.44.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证近 30 天下载 10.2万

Facebook Messenger adapter for Chat SDK — Send API webhooks, templates, and quick replies

原始使用文档
文档目录20 节
  1. Installation
  2. Scaffold with the CLI
  3. Usage
  4. Facebook Messenger setup
  5. 1. Create a Meta app
  6. 2. Create a Facebook Page
  7. 3. Configure Messenger API
  8. Environment variables
  9. Webhook setup
  10. Features
  11. Messaging
  12. Rich content
  13. Conversations
  14. Inbound attachments
  15. Read receipts
  16. Message history
  17. Interactive messages
  18. Thread ID format
  19. AI Coding Agents
  20. License

Facebook Messenger adapter for Chat SDK

@chat-adapter/messenger

npm package: @chat-adapter/messenger

Agent Stack MIT License

Facebook Messenger adapter for Chat SDK, using the Messenger Platform API.

Documentation: chat-sdk.dev/adapters/official/messenger · Guides: vercel.com/kb/chat-sdk

Building for Instagram Direct Messages? Use @chat-adapter/instagram instead. It connects through Instagram API with Instagram Login and does not require a Facebook Page.

Installation

pnpm add @chat-adapter/messenger

Scaffold with the CLI

To scaffold a new Messenger bot with this adapter preselected:

npx create-chat-sdk@latest my-bot --adapter messenger memory

Visit the adapters directory to see other available official and vendor-official adapters.

Usage

import { Chat } from "chat";
import { createMessengerAdapter } from "@chat-adapter/messenger";

const bot = new Chat({
  userName: "mybot",
  adapters: {
    messenger: createMessengerAdapter(),
  },
});

bot.onDirectMessage(async (thread, message) => {
  await thread.post("Hello from Messenger!");
});

When using createMessengerAdapter() without arguments, credentials are auto-detected from environment variables.

Facebook Messenger setup

1. Create a Meta app

  1. Go to developers.facebook.com/apps
  2. Click Create App
  3. Select the use case "Engage with customers on Messenger from Meta"
  4. Enter your app name and contact email, then create the app
  5. Go to App > App Settings > Basic and copy your App Secret — this becomes FACEBOOK_APP_SECRET

2. Create a Facebook Page

Your Messenger bot needs a Facebook Page to send and receive messages. If you don't have one:

  1. The easiest approach is to create a Facebook Business profile first
  2. Then create a Page under that business profile
  3. Note the Page name — users will message this Page to interact with your bot

3. Configure Messenger API

  1. In your Meta app dashboard, go to Use Cases
  2. Find "Engage with customers on Messenger from Meta" and click Customize
  3. Then open Messenger API Settings

Configure webhooks

  1. Under Configure webhooks, click Add Callback URL
  2. Enter your webhook URL: https://your-domain.com/api/webhooks/messenger
  3. Enter a Verify Token — this is a secret string you create (this becomes FACEBOOK_VERIFY_TOKEN)
  4. Click Verify and Save
  5. After verification, click Add Subscriptions and enable:
    • messages
    • messaging_postbacks
    • messaging_reactions
    • message_deliveries
    • message_reads

Generate a Page Access Token

  1. Under Generate access tokens, click Add or remove Pages
  2. Your Pages should populate — select the Page you created
  3. Assign the standard permissions when prompted
  4. Click Generate Token
  5. Copy the token — this becomes FACEBOOK_PAGE_ACCESS_TOKEN

Environment variables

FACEBOOK_APP_SECRET=...              # App secret from App Settings > Basic
FACEBOOK_PAGE_ACCESS_TOKEN=...       # Generated Page access token
FACEBOOK_VERIFY_TOKEN=...            # User-defined webhook verification secret
FACEBOOK_BOT_USERNAME=...            # Optional, defaults to "messenger-bot"
FACEBOOK_API_URL=...                 # Optional, override the Meta Graph API base URL

Webhook setup

Messenger uses two webhook mechanisms:

  1. Verification handshake (GET) — Meta sends a hub.verify_token challenge that must match your FACEBOOK_VERIFY_TOKEN.
  2. Event delivery (POST) — incoming messages, reactions, and postbacks, verified via X-Hub-Signature-256.
// Next.js App Router example
import { bot } from "@/lib/bot";

export async function GET(request: Request) {
  return bot.webhooks.messenger(request);
}

export async function POST(request: Request) {
  return bot.webhooks.messenger(request);
}

Features

Messaging

FeatureSupported
Post messageYes
Edit messageNo (Messenger limitation)
Delete messageNo (Messenger limitation)
StreamingBuffered (accumulates then sends)
Typing indicatorYes
Mark as readYes

Rich content

FeatureSupported
Card formatGeneric/Button Templates
ButtonsYes (max 3 per message)
Link buttonsYes (web_url)
Select menusNo
TablesText fallback
FieldsText fallback
Images in cardsYes (Generic Template)
ModalsNo

Conversations

FeatureSupported
ReactionsReceive only
Typing indicatorYes
DMsYes (DM-only platform)
PostbacksYes

Inbound attachments

Inbound attachment URLs remain available as attachment.url. The adapter's fetchData function downloads media only from Meta's fbsbx.com and fbcdn.net hosts, refuses private and internal addresses (including after redirects), limits responses to 25 MB, and times out after 30 seconds. External fallback and link-share URLs are preserved but rejected before any network request.

Read receipts

Use thread.markAsRead() in a message handler to send Messenger's mark_seen sender action:

await thread.markAsRead();

Messenger applies this action to the conversation's seen state rather than a single message, even when you pass a message or message ID.

Message history

FeatureSupported
Fetch messagesCached sent messages only
Fetch thread infoYes

Interactive messages

Card elements are automatically converted to Messenger templates:

  • Generic Template — Used when the card has a title or imageUrl. Supports up to 3 buttons.
  • Button Template — Used when the card has text content and buttons but no title/image. Max 640 characters.
  • Text Fallback — Used when the card contains unsupported elements (tables, select menus) or exceeds constraints.

Template constraints:

  • Maximum 3 buttons per template
  • Button titles limited to 20 characters (truncated with ellipsis)
  • Subtitles limited to 80 characters
  • Button Template text limited to 640 characters

Thread ID format

messenger:{recipientId}

Example: messenger:27161130920158013

AI Coding Agents

If you use an AI coding agent such as OpenAI Codex, Claude Code, or Cursor, install the Chat SDK skill so it knows the SDK APIs, adapter patterns, and project conventions before writing code.

npx skills add vercel/chat

The skill references bundled documentation in node_modules/chat/docs, plus adapter guides and starter templates in the published package.

You can also install the Vercel Plugin for a broader agent toolkit — it includes the Chat SDK skill alongside specialist agents, agent slash commands, and more:

npx plugins add vercel/vercel-plugin

The plugin is optional; the skill alone is enough to build with Chat SDK.

For agent-readable documentation, see chat-sdk.dev/llms.txt (page index) or chat-sdk.dev/llms-full.txt (full text).

License

MIT

版本与发布记录

@chat-adapter/messenger 共发布 20 个版本,首次发布于 2026/05/08;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.44.0latest2026/11/026 天前
4.43.02026/10/1921 天前
4.42.02026/10/1425 天前
4.41.12026/09/291 个月前
4.41.02026/09/181 个月前
4.40.02026/09/052 个月前

关于 @chat-adapter/messenger 的常见问题

@chat-adapter/messenger 是什么?

Facebook Messenger adapter for Chat SDK — Send API webhooks, templates, and quick replies

如何安装 @chat-adapter/messenger?

在项目目录运行 npm install @chat-adapter/messenger,或使用 pnpm add / yarn add / bun add。

@chat-adapter/messenger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chat-adapter/messenger 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chat-adapter/messenger 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chat-adapter/messenger 可以商用吗?

@chat-adapter/messenger 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chat-adapter/messenger 的下载量有多少?

根据 npm 官方下载统计,@chat-adapter/messenger 在 2026/10/10 – 2026/11/08 这 30 天内被下载 102,331 次,日均约 3,411 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chat-adapter/messenger 还在维护吗?

最新版本 4.44.0 发布于 2026/11/02(6 天前);至今共发布 20 个版本,其中过去 12 个月发布了 20 个;首次发布于 2026/05/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。