![]() | Try It – Send a Message with Messenger Platform |
This guide assumes that you have read the Messenger Platform Overview and implemented the needed components for sending messages and receiving messages and notifications.
Learn how your business can send a message to a customer using the Messenger Platform.
You can use this tutorial to send a message from your app or, if you don't have a fully functional app or just want to explore, you can use our Graph API Explorer.
This guide assumes that you have read the Messenger Platform Overview and implemented the needed components for sending messages and receiving messages and notifications.
To make a successful call to the Meta social graph to send a message, your app will need:
pages_show_list
permission and a User access token, requested by you. This allows your app, or the Graph API Explorer, to get your Page ID.
pages_messaging
permission and a Page access token, requested by a person who can perform the MESSAGING
task on your Page, allows your app to get the conversation ID and your Page-scoped ID (PSID)You can get access tokens 3 different ways:
Login to your Facebook account and send a message to your Page. This step will create a PSID for the customer that is specific for the Page, and a conversation ID representing the conversation between you and that Page.
If you have already subscribed to the messaging Webhooks, you can get the PSID, the conversation ID, and the message text from the Webhook notification, and move to Step 3.
You will need the ID for your Page, the PSID for the person who sent the message (you) and the conversation ID.
To get the Page ID for your Page, send a GET
request to the /USER-ID/accounts
endpoint, where the USER-ID is your ID. You can also use me
in place of the your User ID.
The me
endpoint is a special endpoint that represents the ID for the User, Page, or App that is requesting the access token. In the following example, you will use a User access token in the request so me
will represent your User ID.
curl -i -X GET "https://graph.facebook.com/LATEST-API-VERSION/me/accounts &access_token=USER-ACCESS-TOKEN"
On success, your app will receive a JSON object with the Page ID as well as a Page access token that you can use in subsequent requests.
{ "data": [ "access_token": "EAABkWcj...", // PAGE-ACCESS-TOKEN "category": "Pet Service", "category_list": [ { "id": "144982405562750", "name": "Pet Service" } ], "name": "Cisco Dog Page", "id": "4225...", // PAGE-ID "tasks": [ "ADVERTISE", "ANALYZE", "CREATE_CONTENT", "MESSAGING", "MODERATE", "MANAGE" ] ] }
To get the PSID for the customer who sent your Page a message, send a GET
request to the /PAGE-ID/conversations
endpoint and include the participants
field.
curl -i -X GET "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/conversations ?fields=participants &access_token=PAGE-ACCESS-TOKEN"
On success, your app will receive the following JSON response:
{ "data": [ { "participants": { "data": [ { "name": "CUSTOMER-NAME", "email": "PSID@facebook.com", "id": "PSID" }, { "name": "PAGE-NAME", "email": "PAGE-ID@facebook.com", "id": "PAGE-ID" } ] }, "id": "t_10224..." //Conversation ID } ] }
You will need the Page ID, PSID and the Conversation ID for the following steps.
To get a list messages within a conversation with the customer, send a GET
request to the conversation ID endpoint with the messages{messages}
field.
curl -i -X GET "https://graph.facebook.com/LATEST-API-VERSION/CONVERSATION-ID ?fields=messages{message} &access_token=PAGE-ACCESS-TOKEN"
On success, your app will receive the following JSON response:
{ "messages": { "data": [ { "message": "This is a mesage to my Page", "id": "m_kHTNbg0O5..." //Message ID } ] } "id": "t_10224..." //Conversation ID }
To respond to the message a customer sent to your Page, send a POST
request to /PAGE-ID/messages
endpoint with the recipient
parameter set to the customer's PSID, messaging_type
parameter set to RESPONSE
, and the message
parameter set to your response. Note that this must be sent within 24 hours of your Page receiving the customer's message.
curl -i -X POST "https://graph.facebook.com/v14.0/PAGE-ID/messages ?recipient={id:PSID} &message={text:'You did it!'} &messaging_type=RESPONSE &access_token=PAGE-ACCESS-TOKEN"
On success, your app will receive the following JSON response:
{ "recipient_id": "1008...", // The customer's PSID "message_id": "m_AG5Hz2..."} // The message ID
If you have already subscribed to the messaging Webhooks, you can get the PSID, the conversation ID, and the message text from the Webhook notification, and move to Step 3.
You will need the ID for your Page, the Page-scoped ID (PSID) for the person who sent the message (you) and the conversation ID.
Open the Graph API Explorer in a new browser tab or window.
The explorer loads with a default query with the GET
method, the lastest version of the Graph API, the /me
node and the id
and name
fields in the Query String Field, and your Facebook App. If you would like to run this default query, you can click Generate Access Token then Submit. This query will create a User access token and return your name and User ID.
The me
endpoint is a special endpoint that represents the ID for the User, Page, or App that is requesting the access token. In the following example, you will use a User access token in the request so me will represent your User ID. In Step 4, me
will represent your Page since are using a Page access token.
To get the Page ID for your Page:
me/accounts
or /USER-ID/accounts
. If you ran the default query, you can click the ID in the response and it will automatically be moved to the Query String Field.pages_show_list
permission then click Generate Access Token.To get the Conversation ID and the PSID:
/conversations?fields=participants
to the query.pages_messaging
permissionthen click Generate Access Token.To get a list messages within a conversation with the customer:
/conversations?fields=messages{message}
to the query.To respond to the message the customer sent to your Page:
GET
to POST
.me
and click Submit.
me
in the query or click on the ID in the response to replace me
with the ID in the query string.recipient
set to {id:PSID}
messaging_type
set to RESPONSE
message
set to {text:'Hello, new customer!'}
Note that when using the RESPONSE
message type, the message must be sent within 24 hours of your Page receiving the customer's message or an error will occur.