Facebook Messenger (Public Beta)
Warning
Public Beta
Facebook Messenger is currently available as a Public Beta product. This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available.
Public Beta products are not covered by a Twilio SLA.
Facebook Messenger is a user-initiated channel, which means that end users have to reach out to your business before you can respond. This makes it a great fit for customer support, service and ads-driven sales use cases (e.g. click to message). We recommend using Facebook Messenger with Twilio Flex or Conversations API. You may also use Facebook Messenger with Programmable Messaging API if you have set up webhooks to triage inquiries.
All of the following steps are done through the Twilio Console. In the Facebook Messenger ecosystem, users communicate with brands and companies by messaging their Facebook Page. Twilio creates a Sender for each Facebook Page that you wish to use for messaging. A Sender is the identity used in the "From" field to send messages to your users.
To get started, first go to the Channels area in the Twilio Console, click on Facebook Messenger and install it.
Next, let's configure the Facebook Page you wish to use as a sender. While on the Twilio Channels Catalog page:
- Under the "Properties" heading in the Unique Name input field, enter a name of your choice for your page (Optional).
- Under the "Credentials" heading click the Select a Page dropdown and select a single Facebook Page to use as a sender.
- Under the "Configuration" heading in the Callback URL input field, enter a webhook URL where you can receive incoming messages sent to the Facebook Page from your end users.
Finally, click the "Save" button. This creates an instance of the Facebook Page that you can now use as a Sender.
Once a Channel is authenticated, you can now configure it to work with a Twilio API :
Configuration Parameter | Description | |
---|---|---|
Page Friendly Name | This is the name of the Facebook Page. This cannot be changed. | |
Callback URL | A URL where Twilio will POST each time a message is received by Twilio. The format of this request is the same as Twilio's Inbound SMS TwiML Request. Non-relative URLs must contain a valid hostname (underscores are not allowed). | |
Callback Method | Http method to use with Callback URL. GET or POST . | |
Fallback URL | Fallback URL to which Twilio will call if the Callback URL above returns an error. | |
Fallback Method | Http method to use with Fallback URL. GET or POST .e ID | This is a unique identifier for the Facebook Page, which is used as the Sender: messenger:<page id> . This is also a unique identifier that can be used to navigate to the Facebook Page by following this URL: m.me/<page id> . This ID can not be changed. |
Status Callback URL | A URL where Twilio will call each time your outbound message status changes to one of the following: failed , sent . Twilio will POST the standard TwiML request parameters as well as standard Status parameters : MessageStatus and ErrorCode . Non-relative URLs must contain a valid hostname (underscores aren't allowed). | |
Status Callback Method | Http method to use with Status Callback. GET or POST . |
Twilio supports creating multiple instances so that your application can support different Facebook Pages. If you wish to configure more than one Facebook Page as a sender, you can click "Add Another Instance" and repeat the steps above for each new Facebook Page.
For Independent Software Vendors (ISV's) and Software Integrators (SI's) who are managing Facebook Messenger communications on behalf of your clients, you need to have admin access to each Facebook page.
You can use up to 25 Facebook pages per Twilio account or subaccount. To use additional Facebook pages, use additional subaccounts.
Info
If you are an ISV or SI managing multiple pages on behalf of many brands, we recommend using subaccounts for each brand. For more information on using subaccounts, see the support article Getting Started with Twilio Projects and Subaccounts.
Now that your Facebook Messenger Sender is set up, you need to receive a message from an end user in order to respond. Facebook users need to initiate contact with you by messaging your Facebook Page before you are able to contact them. Once a user's message is received, you can respond back to the user for 24 hours. For more details, see Platform Policy Overview (Facebook for Developers).
Warning
You must receive a message from an end user before you can respond. Facebook Messenger is not a notification channel.
Info
You can send yourself a message using m.me/<page id>
. This is the easiest way to test your sender.
Info
Twilio tags messages with "ACCOUNT_UPDATE" if sent after the 24 hr session is up.
When a user sends a message to your Facebook Page, your callback URL will receive the message with the same parameters as a standard Twilio WhatsApp or SMS message. Facebook Messenger sender and recipients have the format messenger:{messenger id}
. You will need to retrieve the Facebook Messenger user ID from the callback response.
To send a Facebook message, make an HTTP POST
request to Twilio's Programmable Messaging Message REST API resource with three required pieces of information:
- A recipient.
- A sender.
- Your message body.
See the REST API: Sending a SMS or MMS or REST API: Messages page for details.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createMessage() {11const message = await client.messages.create({12body: "Would you like to play a game?",13from: "messenger:{facebook_page_id}",14to: "messenger:{messenger_user_id}",15});1617console.log(message.body);18}1920createMessage();
Response
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"api_version": "2010-04-01",4"body": "Would you like to play a game?",5"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",6"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",7"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",8"direction": "outbound-api",9"error_code": null,10"error_message": null,11"from": "messenger:{facebook_page_id}",12"num_media": "0",13"num_segments": "1",14"price": null,15"price_unit": null,16"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"status": "queued",19"subresource_uris": {20"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"21},22"tags": {23"campaign_name": "Spring Sale 2022",24"message_type": "cart_abandoned"25},26"to": "messenger:{messenger_user_id}",27"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"28}
You are also responsible for managing consumer opt-ins and opt-outs to Facebook Messenger platforms. Users grant you permission to message them by way of initiating the conversation with your Facebook Page. When users request that you stop messaging, you are required to stop messaging them. Users also have the option to block your page from reaching out to them again.
A business must strictly adhere to Facebook's commerce and business policies and is also expected to provide high quality experiences in Messenger. During or after the interaction in Messenger, users can provide negative feedback to Facebook when a page's conversations are considered spammy, abusive, or unpleasant. It is recommended that you maintain high quality, value-adding conversations that are aligned with the user's intention in engaging with you. If a business shows a pattern of violating Facebook's policies, Facebook will disable the page from using Facebook Messenger.