Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Using Facebook Messenger with Content Templates


Facebook Messenger Pre-requisites for Content Templates

facebook-messenger-pre-requisites-for-content-templates page anchor

To get started with Facebook Messenger and Content Templates, you need the following:

  1. A Facebook page for your business
  2. A Facebook account with admin access
  3. Facebook Messenger Channel integrated with the Twilio Console

For more details on the prerequisites of using Facebook Messenger with the Content API, see the Twilio FBM channel help article here(link takes you to an external page).


Important considerations

important-considerations page anchor
  • Twilio/location and twilio/list-picker do not work on FBM.

  • FBM is in-session (user initiated) only. It means:

    • You can't start a conversation with a user, only respond to them.

    • Once they've messaged you, you can respond any number of times within a 24 hour period.

      • FBM is subject to FB's commerce and business policies.
      • Users can report spam, abuse, and unpleasant messages. If there is a pattern of such messages, FB will disable the page from FBM.
  • Quick Reply button IDs aren't returned in the message metadata and don't work. Quick reply button text will be returned instead.


Find your Facebook page's Messenger ID

find-your-facebook-pages-messenger-id page anchor
  1. Log into your Facebook Messenger Account.

  2. Go to your Facebook Page.

  3. Open a chat in Messenger.

    Messenger options menu with Twilio Tester chat open.
  4. Note the page URL's suffix, this your page's FBM ID. (for instance, 103800709108123 in this URL). You will use this later in the configuration process.

    Facebook message URL in browser address bar.

Twilio ConsoleLegacy Console
  1. Go to the Twilio Console and navigate to Products & Services > Numbers & Senders > Facebook Messenger.
  2. Click Enable Facebook Messenger and sign in with the Facebook account that has admin access to your Facebook Page.
  3. Add the Facebook Page that you want to send messages from confirming the FBM ID matches.
(warning)

Messages intended for Twilio must be sent from the Twilio platform

Messages sent from Facebook Business Manager won't appear in Twilio. Ensure that all messages intended for Twilio are sent directly from the Twilio platform.


Add your FBM Sender to your Messaging Service

add-your-fbm-sender-to-your-messaging-service page anchor
  1. Set up a Messaging Service if you don't already have one. In the Twilio Console, go to Messaging Services(link takes you to an external page) and click Create a Messaging Service. This is the same Messaging Service and sender pool to which you can add your WhatsApp and SMS/MMS senders.
  2. To add a FBM Sender, navigate to the Messaging Service created previously. Go to the Sender Pool section and add the Facebook Sender that you've set up.

*This is not a hard requirement. But if you do not do this then you will need to set up the webhook in your FBM Sender. Additionally, you will need to specify a Messaging Service Sid parameter and put your FBM sender in the from field. Similar to here except replace phone numbers with the respective FBM sender id.


Set up Webhooks for your Messaging Service

set-up-webhooks-for-your-messaging-service page anchor

To send a message in FBM, you need to know your recipient's FBM ID. Facebook does not make FBM IDs public so you will need to use a webhook to retrieve the FBM ID from an inbound message. For setup purposes and testing purposes, you will need to set up a webhook and message yourself to get your personal FBM ID.

  1. Navigate to Messaging Services and go to the Integration tab.

  2. Select Send a webhook.

  3. Add a "Request URL" where you can receive incoming messages from your end users. If you need a testing webhook, try going to webhook.site

  4. Copy the webhook into the Delivery Status Callback URL section.

    Webhook integration settings with request and fallback URLs for HTTP Post.

Find your Customer's FBM ID

find-your-customers-fbm-id page anchor
  1. Send a message to your page from your personal Facebook Messenger:

    • Log into your FBM account and click Message on your FBM page.
    • Send any message.
  2. After sending your FB page a message with your personal FBM account. Check the webhook for the FROM field. This will contain the end user's FBM ID.

  3. In your send request, you'll use the FBM ID found in the From field for the incoming message's webhook response.

Sending a Facebook Messenger Message with Content APILink to code sample: Sending a Facebook Messenger Message with Content API
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createMessage() {
11
const message = await client.messages.create({
12
contentSid: "HXXXXXXXXXXXXXXXX",
13
contentVariables: JSON.stringify({
14
1: "YOUR_VARIABLE1",
15
2: "YOURVARIABLE2",
16
}),
17
from: "MGXXXXXXXXXXXXXXX",
18
to: "messenger:REPLACE_WITH_VALUE_FROM_WEBHOOK",
19
});
20
21
console.log(message.body);
22
}
23
24
createMessage();

Response

Note about this response
1
{
2
"account_sid": "ACXXXXXXXXXXXXXXXXXXX",
3
"api_version": "2010-04-01",
4
"body": "Hello! 👍",
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": "MGXXXXXXXXXXXXXXX",
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
"to": "messenger:REPLACE_WITH_VALUE_FROM_WEBHOOK",
23
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
24
}