pimax/fb-messenger-php

how can i send message to multiple profile

Closed this issue · 21 comments

how can i send message to multiple profile ?

My current status is: One message == One recipient

@manhag FB does not support sending message to multiple recipients yet. If you want to do so you must do it on your own (loop on them and send messages one by one).
It seems your bot will begin the conversation with your users. Which means your bot app need to have pages_messaging_subscriptions permission.

Hi, correct me if I am wrong.
Graph API and Messenger API are not the same.

@wittfabian
i asked on FBDeveloper
they said YES ...you can
you can post to multiple recipient using Batch method

they are the same ... i think

Can you show me the link?

Ok, they are the same

can you help me to do it ? :)

I will check the documentation and test the request.
But batch requests are currently not supported with this SDK.

Emil Hesslow (software engineer @ Facebook) commented on my post on FB developer

can you read comments please ??

This is how it can be done using curl:

curl  -i \
-F 'include_headers=false' \
-F 'access_token=ACCESSTOKEN' \
-F 'batch=[
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=ACCESSTOKEN",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }, 
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=ACCESSTOKEN",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }
]' \
https://graph.facebook.com/v2.8

you may get the body string using this:

$test = [
    'recipient' => [
        'id' => '860782960624842',
    ],
    'message' => [
        'text' => 'hello, world!'
    ]
];
var_dump(http_build_query($test, null, '&'));

Do not forget to put your own access token at the end of each relative_url

I will need this to manage subscribers and I always thought messenger platform is separated from the graph API. So thx @manhag for bringing this to the table 👍

@a-fawzy thanks :)
i tried your code in php but it doesnot work


$jsonData = '{
"access_token": "' .$PAGE_ACCESS_TOKEN .'",
"Facebook-API-Version":"v2.6",
batch=[
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=$PAGE_ACCESS_TOKEN",
    "body": "recipient%5Bid%5D=1589111404437224&message%5Btext%5D=hello%2C+world%21"
  }, 
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=$PAGE_ACCESS_TOKEN ",
    "body": "recipient%5Bid%5D=1197229497062040&message%5Btext%5D=hello%2C+world%21"
  }
]
}';

I did not try it using php yet but it is tested and working as curl. Kindly note that request sent to the graph API is NOT JSON.

yes i know

here is the complete code


    /*initialize curl*/
    $ch = curl_init($url);

$jsonData = '{
"access_token": "' .$PAGE_ACCESS_TOKEN .'",
"Facebook-API-Version":"v2.6",
batch=[
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=$PAGE_ACCESS_TOKEN",
    "body": "recipient%5Bid%5D=1589111404437224&message%5Btext%5D=hello%2C+world%21"
  }, 
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=$PAGE_ACCESS_TOKEN ",
    "body": "recipient%5Bid%5D=1197229497062040&message%5Btext%5D=hello%2C+world%21"
  }
]
}';
 //   echo urlencode($JSONData1);
    
    /*curl setting to send a json post data*/ 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  
        $result = curl_exec($ch); // user will get the message
    var_Dump($result); 
  

This should work

<?php

$PAGE_ACCESS_TOKEN = '';

$ch = curl_init('https://graph.facebook.com/v2.8');

$fields['access_token'] = $PAGE_ACCESS_TOKEN;

$fields['batch'] = '
[
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=' . $PAGE_ACCESS_TOKEN . '",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }, 
  {
    "method":"POST",
    "headers":[{"name": "Content-Type","value": "application/json"}],
    "relative_url":"me/messages?access_token=' . $PAGE_ACCESS_TOKEN . '",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }
]
';

/* curl setting to send a json post data */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

$result = curl_exec($ch);
var_dump($result);

Thanks bro :D
the problem was with BODY
can i figure out [body] easily more than this method ???
like using JSON and convert it to php string ????
URLEncoding

you can define headers and access_token ONCE only
New Code

$fields['access_token'] = $PAGE_ACCESS_TOKEN;
$fields['headers'] = '{"name": "Content-Type","value": "application/json"}';

$fields['batch'] = '
[
  {
    "method":"POST", 
    "relative_url":"me/messages",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }, 
  {
    "method":"POST", 
    "relative_url":"me/messages",
    "body": "recipient%5Bid%5D=860782960624842&message%5Btext%5D=hello%2C+world%21"
  }
]
';

thanks ^_^
i wish that the method integrated with pimax package

In principle, it is the same as if you send each message individually (as "for-each-loop").
Maybe a little bit faster. I am creating a ticket for batch sending.

This also works

`<?php

$verify_token = ""; // Verify token
$token = ""; // Page token

if (file_exists(DIR . '/config.php')) {
$config = include DIR . '/config.php';
$verify_token = $config['verify_token'];
$token = $config['token'];
}

require_once(dirname(FILE) . '/vendor/autoload.php');

use pimax\FbBotApp;
use pimax\Menu\MenuItem;
use pimax\Menu\LocalizedMenu;
use pimax\Messages\Message;
use pimax\Messages\MessageButton;
use pimax\Messages\StructuredMessage;
use pimax\Messages\MessageElement;
use pimax\Messages\MessageReceiptElement;
use pimax\Messages\Address;
use pimax\Messages\Summary;
use pimax\Messages\Adjustment;
use pimax\Messages\AccountLink;
use pimax\Messages\ImageMessage;
use pimax\Messages\QuickReply;
use pimax\Messages\QuickReplyButton;
use pimax\Messages\SenderAction;

// Make Bot Instance
$bot = new FbBotApp($token);

$bot->send(new Message(1622608101XXXXXX, 'This is a simple text message.'));
$bot->send(new Message(1491940750YYYYYY, 'This is a simple text message.'));
?>
`