chat / adapter-messenger
AGENTS.md2.4k starsChanged 59 days agoMITView on GitHub
Guidance for coding agents working inside the Messenger adapter package. The top-level repository AGENTS.md covers monorepo-wide build, lint, and release rules — read it first. This file documents the adapter-specific surface, conventions, and pitfalls. @chat-adapter/messenger connects a Chat SDK bot to Facebook Messenger via the Meta Messenger Platform. It covers: - HTTP webhook endpoint at /api/webhooks/messenger with the GET challenge handshake plus signed POST event delivery (HMAC-SHA256 via appSecret). - Outbound messages via the Send API: text, attachments, templates (button…
- Reads credentials
Trending
Quiet since March.thin dataabout the whole repository
Who's talking about vercel/chat
- @camilleroux.com“Un SDK TypeScript unifié pour créer des chatbots qui fonctionnent sur Slack, Teams, Google Chat, Discord, …”Bluesky · Mar 3, 2026 · 3 likes
- @technews4869.bsky.social“github.com/vercel/chat GitHub - vercel/chat: A unified TypeScript SDK for building chat bots across Slack, …”Bluesky · Mar 7, 2026 · 1 likes
- @yug1224.com“チャットアプリ開発に使える統一SDKを提供するツールらしい 様々なプラットフォーム対応で便利そうだな yug1224 starred vercel/chat https://github.com/vercel/chat”Bluesky · Mar 7, 2026 · 1 likes
- @news.humancoders.com“Vercel lance Chat SDK : un SDK TypeScript unifié pour créer des bots multi-plateformes”Bluesky · Mar 5, 2026 · 1 likes
- @github-trending-js.bsky.social“🔥 Hot Repo! 🔥 (100+ new stars) 📦 vercel / chat ⭐ 871 (+121) 🗒 TypeScript A unified TypeScript SDK for …”Bluesky · Feb 27, 2026 · 1 likes
The biggest 5 of 5 mentions linking to the repository that our crawl found. Each line is their own headline.
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository44 other files it gives its agents.
- AGENTS.mdAGENTS.md
- apps/docs/AGENTS.mdAGENTS.md
- chat-sdkAGENTS.md
- packages/adapter-discord/AGENTS.mdAGENTS.md
- packages/adapter-gchat/AGENTS.mdAGENTS.md
- packages/adapter-github/AGENTS.mdAGENTS.md
- packages/adapter-instagram/AGENTS.mdAGENTS.md
- packages/adapter-linear/AGENTS.mdAGENTS.md
- packages/adapter-notion/AGENTS.mdAGENTS.md
- packages/adapter-slack/AGENTS.mdAGENTS.md
- packages/adapter-teams/AGENTS.mdAGENTS.md
- packages/adapter-telegram/AGENTS.mdAGENTS.md
- packages/adapter-web/AGENTS.mdAGENTS.md
- packages/adapter-whatsapp/AGENTS.mdAGENTS.md
- packages/chat/src/adapters/AGENTS.mdAGENTS.md
- packages/create-chat-sdk/AGENTS.mdAGENTS.md
- packages/create-chat-sdk/_template/AGENTS.mdAGENTS.md
- packages/state-ioredis/AGENTS.mdAGENTS.md
- packages/state-memory/AGENTS.mdAGENTS.md
- packages/state-pg/AGENTS.mdAGENTS.md
- packages/state-redis/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- packages/adapter-discord/CLAUDE.mdCLAUDE.md
- packages/adapter-gchat/CLAUDE.mdCLAUDE.md
- packages/adapter-github/CLAUDE.mdCLAUDE.md
- packages/adapter-linear/CLAUDE.mdCLAUDE.md
- packages/adapter-messenger/CLAUDE.mdCLAUDE.md
- packages/adapter-slack/CLAUDE.mdCLAUDE.md
- packages/adapter-teams/CLAUDE.mdCLAUDE.md
- packages/adapter-telegram/CLAUDE.mdCLAUDE.md
- packages/adapter-web/CLAUDE.mdCLAUDE.md
- packages/adapter-whatsapp/CLAUDE.mdCLAUDE.md
- packages/create-chat-sdk/CLAUDE.mdCLAUDE.md
- packages/create-chat-sdk/_template/CLAUDE.mdCLAUDE.md
- packages/state-ioredis/CLAUDE.mdCLAUDE.md
- packages/state-memory/CLAUDE.mdCLAUDE.md
- packages/state-pg/CLAUDE.mdCLAUDE.md
- packages/state-redis/CLAUDE.mdCLAUDE.md
- add-adapterSkill
- technical-writerSkill
- chat-sdkSkill
- chat-sdkSkill
- chat-sdkSkill
- chat-sdkSkill
The file
We split this AGENTS.md into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 202
When you add support for a new event type, capture a fresh fixture in sample-messages.md.
- Distinctive: line 207
Use named exports throughout.
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md — @chat-adapter/messenger
Guidance for coding agents working inside the Messenger adapter package. The top-level repository AGENTS.md covers monorepo-wide build, lint, and release rules — read it first. This file documents the adapter-specific surface, conventions, and pitfalls.
Overview
@chat-adapter/messenger connects a Chat SDK bot to Facebook
Messenger via the Meta Messenger Platform. It covers:
- HTTP webhook endpoint at
/api/webhooks/messengerwith the GET challenge handshake plus signed POST event delivery (HMAC-SHA256 viaappSecret). - Outbound messages via the Send API: text, attachments, templates (button + generic + receipt), quick replies, and persistent menu setup.
- Page-scoped addressing — Messenger threads are 1:1 between the page and a customer's PSID (page-scoped user id).
- 24-hour standard messaging window with message-tag escapes for certain transactional flows.
Directory layout
packages/adapter-messenger/
├── src/
│ ├── index.ts # MessengerAdapter + createMessengerAdapter
│ ├── index.test.ts
│ ├── cards.ts # PostableMessage / Card → button/generic templates
│ ├── cards.test.ts
│ ├── markdown.ts # MessengerFormatConverter (mdast ↔ plain text)
│ ├── markdown.test.ts
│ └── types.ts # Send API + webhook event typings
├── sample-messages.md # captured Messenger webhook deliveries
├── package.json
├── tsconfig.json
├── tsup.config.ts
├── vitest.config.ts
└── README.mdBuild, test, typecheck
pnpm build
pnpm dev
pnpm test
pnpm test:watch
pnpm typecheck
pnpm clean
# from repo root
pnpm --filter @chat-adapter/messenger build
pnpm --filter @chat-adapter/messenger testReplay tests live in
packages/integration-tests/src/replay-messenger-*.test.ts.
Public surface
Main exports from src/index.ts:
createMessengerAdapter(config?)— primary factory. Auto-detectsMESSENGER_PAGE_ACCESS_TOKEN,MESSENGER_APP_SECRET,MESSENGER_VERIFY_TOKEN, and the optionalMESSENGER_PAGE_ID.MessengerAdapterclass — implementsAdapter<MessengerThreadId, unknown>. Public methods:handleWebhook,postMessage,editMessage,deleteMessage,addReaction,removeReaction,startTyping,markRead,fetchThread,fetchMessages,fetchSingleMessage,fetchChannelInfo,postChannelMessage,openDM,setPersistentMenu,setGetStarted.- Configuration:
MessengerAdapterConfig,MessengerThreadId. - Helpers:
cardToTemplate,cardToFallbackText,MessengerFormatConverter,decodeThreadId,encodeThreadId,isDM.
Thread ID format
Messenger conversations are scoped by page ID and the user's PSID:
messenger:{pageId}:{psid}isDM(threadId) always returns true — Messenger has no group
construct exposed to bots. Group chats (Workplace) require a
different adapter.
Webhook flow
MessengerAdapter.handleWebhook(request, options) is the entry
point.
- Verification handshake (GET) — Meta sends the same
hub.verify_token/hub.challengeflow as WhatsApp. The adapter checksverify_tokenagainstMESSENGER_VERIFY_TOKEN. - Event delivery (POST) — signed with HMAC-SHA256 using
appSecret. The adapter verifies theX-Hub-Signature-256header before parsing. - Event routing
messaging[].message→chat.handleIncomingMessage. Mentions are detected viamessage.mentions(rare on Messenger; mostly used in group chats which we don't support).messaging[].postback→chat.handleActionfor persistent menu callbacks and Get-Started flows.messaging[].quick_reply→chat.handleActionfor tapped quick replies.messaging[].delivery/messaging[].read→ optional delivery receipts.messaging[].reaction→chat.handleReaction.
waitUntil— required because Meta retries on slow 200s.
Authentication
Two pieces of state per page:
- Page Access Token — long-lived (exchanged from a short-lived user token via the Graph API token-exchange endpoint). Persist it in your secret store.
- App Secret — used only for signature verification, never in outbound calls.
Out-of-webhook code uses the page access token directly; there's no per-conversation state to manage.
Format conversion
Messenger renders plain text. MessengerFormatConverter (in
markdown.ts):
- mdast → plain text — strips all formatting, preserves links by
flattening to
label (url)notation, preserves bullet/ordered list bullets via prefix characters (•,1.). - Plain text → mdast — wraps the body in a single
paragraphwith links auto-detected via the URL regex. - Tables flatten to fixed-width text.
The Send API has a 2000-character limit per message. The adapter splits longer text into multiple messages with sensible breakpoints.
Cards (templates)
Messenger supports several template types; the adapter chooses based on card structure:
- Button template — short body + up to 3 buttons. Used when the card has a single section, no images, and 1–3 actions.
- Generic template — rich card with image, title, subtitle, and
buttons. Used when the card has a
<Image>child or richer layout. - Quick replies — up to 13 inline reply chips. Used when the card has many short button options that fit the chip pattern.
<LinkButton> becomes a web_url button. <Button> becomes a
postback button with payload = callbackId.
File uploads
The Send API accepts attachments via either upload or URL reference.
postMessage prefers URL references when the file is already hosted
publicly; binary uploads go through POST
/{pageId}/message_attachments first.
Inbound attachments arrive with public payload.url — surface them
to handlers as Chat SDK Attachments with the URL preserved.
Messenger quirks worth remembering
- 24-hour window. Free-form messages outside the window require
a
messaging_typeofMESSAGE_TAGwith an approved tag (HUMAN_AGENT,CONFIRMED_EVENT_UPDATE,POST_PURCHASE_UPDATE,ACCOUNT_UPDATE). The adapter throwsValidationErrorif you bypass the window without a tag. - PSID stability. PSIDs are page-scoped — the same user has a different PSID on a different page.
- Persistent menu.
setPersistentMenuconfigures the hamburger-style menu shown to all users;setGetStartedconfigures the Get Started button payload. Both are page-wide. - Page subscription. The Messenger app must be subscribed to the
page's webhooks (
POST /{pageId}/subscribed_apps) before events fire. Subscription is per-page, not per-user. - Reactions are limited to a small set (
smile,angry,sad,wow,love,like,dislike). The adapter normalizes viacards.ts. - Webhook deliveries can include multiple events per request.
Iterate the
entry[].messaging[]array fully.
Testing approach
- Unit tests colocated with each module (
*.test.ts). The template-selection tests incards.test.tsare particularly important. - Replay tests in
packages/integration-tests/src/replay-messenger-*.test.tsconsume recorded webhook events. - Send API client is mocked at the
fetchboundary; never hitgraph.facebook.comfrom unit tests.
When you add support for a new event type, capture a fresh fixture
in sample-messages.md.
Coding conventions
- Use named exports throughout. No default exports.
- Send API typings live in
types.ts. Extend them rather than pulling a third-party dependency. - Errors map to
@chat-adapter/shared(AuthenticationError,AdapterRateLimitError,NetworkError,ValidationError). - Top-level regex literals only.
- Page access tokens are sensitive — never log them, never include them in serialized error messages.
Releases
Behavioural changes need a changeset (pnpm changeset, choose
@chat-adapter/messenger plus chat if a public type changed).
Sample fixtures and AGENTS.md edits don't.
Where to look next
- User-facing docs:
apps/docs/content/adapters/official/messenger.mdx - README:
packages/adapter-messenger/README.md - Shared error/utility helpers:
packages/adapter-shared/src/index.ts - Core Adapter contract:
packages/chat/src/adapter.ts - Sample webhook payloads:
packages/adapter-messenger/sample-messages.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

