Messenger Setup
Messenger setup connects a Facebook Page to one Kravos.ai deployment. Customer messages arrive as Meta webhooks, and Kravos.ai sends replies through the conf...
Messenger Setup
Messenger setup connects a Facebook Page to one Kravos.ai deployment. Customer messages arrive as Meta webhooks, and Kravos.ai sends replies through the configured Page.
Page-scoped user IDs
Messenger identifies users with Page-scoped IDs. Kravos.ai stores those IDs as end user channel identities. Facebook profile URLs are not available through this flow.
Required Kravos.ai Configuration
| Variable | Required for | Notes |
|---|---|---|
PUBLIC_APP_URL or NEXTAUTH_URL | Webhook URL display | Use the public HTTPS web app origin. |
META_VERIFY_TOKEN | Meta challenge verification | Must match the token entered in Meta's webhook setup. |
META_APP_SECRET | POST signature verification | Used to verify x-hub-signature-256. |
META_MESSENGER_ACCESS_TOKEN | Messenger outbound replies and profile lookup | Preferred Page-scoped token. |
META_ACCESS_TOKEN | Messenger outbound replies and profile lookup | Fallback when the channel-specific token is not set. |
META_PAGE_ID | Messenger outbound replies | Used in the Graph API /messages URL. |
RUNTIME_URL and RUNTIME_SERVICE_TOKEN | AI replies | Without these, inbound messages are stored but AI generation is skipped. |
The channel settings status endpoint reports this readiness at GET /api/settings/channels.
Meta Setup Checklist
- Create or choose your Meta app and add Messenger.
- Connect the Facebook Page that should send and receive messages.
- Create a Page access token that can send messages for that Page.
- Set
META_PAGE_IDto the Page ID. - Set the callback URL to:
https://your-web-app.example.com/api/webhooks/messenger
- Enter the same verify token that you configured as
META_VERIFY_TOKEN. - Subscribe the Page webhook to message events.
- Subscribe to message echo events if you want manual Page replies to pause AI in Kravos.ai.
- Create or activate a tenant
ChannelConfigforMESSENGERwhen you need first-message fallback routing.
What The Messenger Webhook Accepts
Challenge request
Meta verifies the URL with a GET request that includes:
| Query parameter | Required value |
|---|---|
hub.mode | subscribe |
hub.verify_token | Must equal META_VERIFY_TOKEN |
hub.challenge | Any non-empty challenge string |
When the token matches, Kravos.ai returns the raw challenge text with status 200. Invalid tokens return 403. Missing META_VERIFY_TOKEN returns 503.
Message delivery request
Meta delivers messages with POST /api/webhooks/messenger.
Implemented behavior:
- reads the raw request body before parsing JSON
- verifies
x-hub-signature-256with HMAC SHA-256 andMETA_APP_SECRET - returns
401for missing or invalid signatures - returns
400for invalid JSON - extracts messages from
entry[].messaging[] - uses
message.midas the provider message ID - uses
sender.idas the customer Page-scoped ID for inbound customer messages - deduplicates provider message IDs for 24 hours
- enqueues
channel.inboundjobs for new customer messages
Manual Replies And Echo Events
Messenger can send echo events when the business replies manually from the Page inbox. The implemented handler treats message.is_echo: true as a manual business reply.
For echo events, Kravos.ai:
- resolves the end user from the echo recipient ID
- finds matching
AI_ACTIVEMessenger conversations - changes those conversations to
HUMAN_ACTIVE - returns
echoCountin the webhook response - does not enqueue an inbound AI-processing job
This prevents the AI from replying over a human Page agent.
Message Support
| Direction | Supported now |
|---|---|
| Inbound text | Stored and passed to the agent runtime. |
| Inbound image/audio/file attachments | Stored as placeholders; media download is not implemented. |
| Outbound text | Sent through Messenger Send API as text. |
| Outbound image/video/file | Sent as reusable URL attachments when the agent reply includes a public mediaUrl. |
Messenger profile lookup is best-effort. If META_MESSENGER_ACCESS_TOKEN or META_ACCESS_TOKEN is available, the worker requests the sender's name, first_name, last_name, and profile_pic. Lookup failures do not block message processing.
Testing Messenger
Use Meta's webhook verification first. Then send a test message to the connected Facebook Page.
A successful customer message returns:
{
"data": {
"received": true,
"deduped": false,
"dedupedCount": 0,
"enqueued": 1,
"echoCount": 0
}
}
A manual Page reply echo can return echoCount: 1 and enqueued: 0.
Troubleshooting
Meta cannot verify the webhook
- Confirm the public URL is HTTPS and points to
/api/webhooks/messenger. - Confirm
META_VERIFY_TOKENexactly matches the token entered in Meta. - Confirm the Meta app and Page subscription are active.
POST returns 401
- Confirm
META_APP_SECRETbelongs to the same Meta app sending the webhook. - Confirm the request still has the original raw body when signature verification runs.
POST returns 503
- Check for missing
META_APP_SECRET. - Confirm there is an active
ChannelConfigforMESSENGERif this is a first-time user. - In multi-tenant deployments, confirm first-message routing is not ambiguous across multiple tenants.
AI replies after a human replied in Messenger
- Confirm Meta is sending echo events with
message.is_echo: true. - Confirm the echo recipient ID matches the stored end user's Messenger ID.
- Confirm the conversation was still
AI_ACTIVEbefore the manual reply.
Related Docs
Channels
Understand channel routing, queues, dedupe, and supported message types.
Incoming Webhooks
Review webhook verification, replay safety, retries, and failures.


