Skip to contentSkip to navigationSkip to topbar
On this page

Send a message with Facebook Messenger (public beta)


(new)

Public Beta

Facebook Messenger is available as a Public Beta product, and the information contained in this document is subject to change.

Some features aren't yet implemented, and others may change before the product becomes Generally Available (GA). Public Beta products aren't covered by a Service Level Agreement (SLA).

On this page, you'll learn to use Twilio Programmable Messaging(link takes you to an external page) to programmatically send messages with Facebook Messenger using your web application or cURL(link takes you to an external page).


Review considerations

review-considerations page anchor
  • Facebook users must initiate contact with you by messaging your Facebook Page before you can contact them. Once you receive a user's message, you can respond back to the user for 24 hours. To learn more, see Platform Policy Overview (Facebook for Developers)(link takes you to an external page).

    If you send a message after the 24-hour window has elapsed, Twilio tags messages with ACCOUNT_UPDATE.

  • You're responsible for managing consumer opt-ins and opt-outs to the Facebook Messenger platform. Users grant you permission to message them by initiating the conversation with your Facebook Page. When users request that you stop messaging, you must stop messaging them. Users also have the option to block your page.

  • You must strictly adhere to Facebook's commerce and business policies and 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 spammy, abusive, or unpleasant. Maintain high quality, value-adding conversations that align with the user's intentions when engaging with you. If a business shows a pattern of violating Facebook's policies, Facebook disables the page from using Facebook Messenger.


Complete the prerequisites

complete-the-prerequisites page anchor

Before you send a message with Facebook Messenger, complete the Facebook Messenger setup.


Send a message with Facebook Messenger

send-a-message-with-facebook-messenger page anchor

You can send messages with Facebook Messenger using code that makes HTTP POST requests to the Message resource(link takes you to an external page) in the Twilio REST API.

To send a message with Facebook Messenger, follow the steps to send an SMS message, replacing the from field with messenger:<facebook_page_id> and to field with messenger:<messenger_user_id> as shown in the following example.

To find your Facebook Page ID:

  1. Open the Channels page in the Twilio Console(link takes you to an external page).
  2. Click Facebook Messenger.
  3. Check the Page ID field.

To find your recipient's Messenger User ID, check your callback URL. When a user sends a message to your Facebook Page, your callback URL receives the message with the same parameters as a standard Twilio SMS message.

To test your Facebook Messenger sender, you can send yourself a message by replacing the to field with m.me/<page_id>.

Respond Using the Programmable Messaging APILink to code sample: Respond Using the Programmable Messaging 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
body: "Would you like to play a game?",
13
from: "messenger:{facebook_page_id}",
14
to: "messenger:{messenger_user_id}",
15
});
16
17
console.log(message.body);
18
}
19
20
createMessage();