Messenger Bot Subscription Using Broadcast API in PHP

If you are planning to implement Subscription System in Messenger Bot, here is a good news. With the release of Broadcast API, sending message to numerous recipientsĀ  has been so much easier.

Generally, interaction between users and Messenger is one-on-one basis. However, in some cases, you need to send same message to many users at once. Message may be new events, popular news or other updates. This will increase user engagement with your Bot.

Messenger bots having pages_messaging_subscriptions permission can send updates to their subscribers using Send API. However, sending a message to many recipients can be inefficient as it was one on one basis.

Using the Broadcast API, you can send (broadcast) message to everyone that currently has an open conversation with your Page or a custom set of people.

In this article, I am going to discuss how to broadcast message to multiple users through Messenger Bot. It involves two major steps:

  • Creating a Broadcast Message
  • Sending a Broadcast Message

Requirements for using Broadcast API

  • Page Access Token
  • pages_messaging & pages_messaging_subscriptions permissions

Page Access Token is required to access the Broadcast API. If you don’t have Page Access Token, follow How to build Facebook Messenger Chat Bot using PHP.

Your Messenger bot must have the pages_messaging & pages_messaging_subscriptions permissions to use the Broadcast API.

Broadcast API is available only in Graph API v2.11 and above.

Creating a Broadcast Message

You need to create broadcast message in advance.

Message is any message you can send via the Send API.

Request URL forĀ  creating a broadcast message is as follows:

https://graph.facebook.com/v2.11/me/message_creatives?access_token=PAGE_ACCESS_TOKEN

The API call will return message_creative_id on success. You can use this message_creative_id to broadcast message.

The following PHP snippets creates broadcast message using cURL.

<?php


$access_token = 'PAGE ACCESS TOKEN'; 

$messageJSON =  '{    
  "messages":[
    {
    "dynamic_text": {
      "text": "Hello , {{first_name}}!",
      "fallback_text": "Hello friend"
    } 
  }
  ]
}';
  
//API Url
$api_url = 'https://graph.facebook.com/v2.11/me/message_creatives?access_token='.$access_token;

  
//Initiate cURL.
$ch = curl_init($api_url);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);  
// Return the API response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $messageJSON);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);

// Get Message Creative Id
$response = json_decode($result);
$message_creative_id = $response->message_creative_id;

?>

Sending a Broadcast Message

After creating the broadcast message, its time to send the message using the message_creative_id.

Request URL for sending the broadcast message is as follows:

https://graph.facebook.com/v2.11/me/broadcast_messages?access_token=PAGE_ACCESS_TOKEN

The API call will return a numeric broadcast_id on success.

The following PHP snippets sends broadcast message using cURL.

<?php

$creativeIdJSON = '{    
    "message_creative_id": "' . $message_creative_id . '"
  }';
  
$api_url = 'https://graph.facebook.com/v2.11/me/broadcast_messages?access_token='.$access_token;

$ch = curl_init($api_url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $creativeIdJSON);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);

?>

Conclusion

In this way, you can easily broadcast message using the broadcast API. Furthermore, you can schedule your broadcast using cronjobs.

For more details on Broadcast API, please check here.

Leave a Reply