How it fits together
Messenger and Instagram messaging bots are Meta apps that receive webhooks for a Facebook Page or an Instagram professional account linked to a Page. You reply with the Send API using a Page access token. WhatsApp uses a separate product, the WhatsApp Cloud API, even though the webhook verification looks the same.
- Create an app in the Meta developer dashboard and add the Messenger product (and Instagram messaging if needed).
- Connect the Page and generate a Page access token. Store it as a secret.
- Configure the webhook URL and a verify token, then subscribe the Page to fields such as
messagesandmessaging_postbacks. - Test with accounts that have a role on the app. Other people can only use the bot after the required permissions pass app review.
Webhooks
Verification is a GET with hub.mode, hub.verify_token and hub.challenge: compare the token and echo the challenge. Every POST carries an X-Hub-Signature-256 header, an HMAC-SHA256 of the raw body with your app secret. Verify it with a constant-time comparison before parsing. You can generate test signatures with the webhook signature generator.
Respond 200 quickly and do the work asynchronously. Meta retries failed deliveries and may disable a webhook that keeps failing.
Sending messages
curl -X POST "https://graph.facebook.com/v21.0/me/messages" \
-H "Authorization: Bearer $PAGE_TOKEN" -H "Content-Type: application/json" \
-d '{"recipient":{"id":"PSID"},"messaging_type":"RESPONSE","message":{"text":"Hi!"}}'
Users are identified by a page-scoped ID (PSID), different for every Page. Graph API versions are dated; pin one and upgrade on purpose.
The 24-hour window
Businesses can message a person freely within 24 hours of that person's last message. Outside the window only specific cases are allowed, such as approved message tags (for example account updates) or flows the platform explicitly supports. Promotional messages outside the window violate the policy and can get the Page restricted. Read the current rules in the Messenger Platform policy documentation.
Useful features
- Quick replies for short choices and postback buttons for menus.
- Persistent menu and ice breakers to help people start.
- Handover protocol to pass a conversation to a human inbox and take it back.
- m.me links with a
refparameter to attribute where a conversation started. The deep link builder creates them.
Checklist
- Verify signatures and keep the app secret and Page token out of source control.
- Store PSIDs, not names, and delete data when a person asks.
- Offer a clear way to reach a human and to stop messages.
- Keep a privacy policy URL ready: app review requires it. See legal, terms and privacy.