Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i send message to multiple profile #66

Closed
manhag opened this issue Mar 3, 2017 · 21 comments
Closed

how can i send message to multiple profile #66

manhag opened this issue Mar 3, 2017 · 21 comments
Labels

Comments

@manhag
Copy link

manhag commented Mar 3, 2017

how can i send message to multiple profile ?

@wittfabian
Copy link
Contributor

My current status is: One message == One recipient

@a-fawzy
Copy link
Contributor

a-fawzy commented Mar 4, 2017

@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.

@manhag
Copy link
Author

manhag commented Mar 4, 2017

@manhag
Copy link
Author

manhag commented Mar 4, 2017

@wittfabian
Copy link
Contributor

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

@manhag
Copy link
Author

manhag commented Mar 4, 2017

@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

@wittfabian
Copy link
Contributor

Can you show me the link?

@wittfabian
Copy link
Contributor

Ok, they are the same

@manhag
Copy link
Author

manhag commented Mar 4, 2017

can you help me to do it ? :)

@wittfabian
Copy link
Contributor

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

@manhag
Copy link
Author

manhag commented Mar 4, 2017

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

can you read comments please ??

@a-fawzy
Copy link
Contributor

a-fawzy commented Mar 4, 2017

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

@a-fawzy
Copy link
Contributor

a-fawzy commented Mar 4, 2017

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 👍

@manhag
Copy link
Author

manhag commented Mar 4, 2017

@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"
  }
]
}';

@a-fawzy
Copy link
Contributor

a-fawzy commented Mar 4, 2017

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.

@manhag
Copy link
Author

manhag commented Mar 4, 2017

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); 
  

@a-fawzy
Copy link
Contributor

a-fawzy commented Mar 4, 2017

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);

@manhag
Copy link
Author

manhag commented Mar 4, 2017

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

@wittfabian
Copy link
Contributor

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.

@erhantez
Copy link

erhantez commented Dec 12, 2017

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.'));
?>
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants