3

I have been working with Facebook PHP SDK a couple of days ago. And I have read in the Facebook Docs here that it is possible to send a private messages to users if you have their phone number but this does not seem to work.

Here is my Code:

     $fb = new Facebook\Facebook([
      'app_id' => 'xxxxxxxxxxxxx',
      'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
      'default_graph_version' => 'v2.3',
      // . . .
      ]);
    $post = [
  'access_token' => $_SESSION['page_access_token'],
  'message'      =>  
  [
    'text' => 'Hello, wadup?!!!'
  ],
  'recipient' => [
    'id' => '+1(0)0000000000'
  ]
];

$response = $fb->post('https://graph.facebook.com/v2.6/me/messages?', $post, $_SESSION['page_access_token']);
print_r($response);

The output:

Facebook\FacebookResponse Object ( [httpStatusCode:protected] => 200 [headers:protected] => Array ( [x-page-usage] => {"call_count":1,"total_cputime":1,"total_time":1} [Strict-Transport-Security] => max-age=15552000; preload [x-fb-trace-id] => CQO0hjDHbf0 [x-fb-rev] => 3535263 [Expires] => Sat, 01 Jan 2000 00:00:00 GMT [Content-Type] => application/json; charset=UTF-8 [facebook-api-version] => v2.11 [Cache-Control] => private, no-cache, no-store, must-revalidate [Pragma] => no-cache [Access-Control-Allow-Origin] => * [Vary] => Accept-Encoding [X-FB-Debug] => WYq6KntFxQ/uTEK8B4tsvF+UfAXzsTtpm4yJcVxkrXkxTPo04yx/ITqLTe8Ho7k+tLhzC87eNojGjjgKXaGnIg== [Date] => Mon, 18 Dec 2017 09:43:11 GMT [Connection] => keep-alive [Content-Length] => 82 ) [body:protected] => {"id":"1157989877575188","url":"https:\/\/graph.facebook.com\/v2.6\/me\/messages"} [decodedBody:protected] => Array ( [id] => 1157989877575188 [url] => https://graph.facebook.com/v2.6/me/messages ) [request:protected] => Facebook\FacebookRequest Object ( [app:protected] => Facebook\FacebookApp Object ( [id:protected] => 1709050269133889 [secret:protected] => 1ceb6a6e1e4419f2799e699ebae2808e ) [accessToken:protected] => EAAYSX0Yx7EEBAH11ZAg7RWx60038nfYK1V1gOzqnu2NG0riV9DWiMRIJ9tZBfQwxqo92OaXx6uzhDZBn3xji8LXBP3mChJpbrCKpRLniUob9YlE1wZA3cnMSFIjA8eBjEaAUMJRfYvOBGfP7hIqMzNDU7fOPQOyXLFlPZAbdMZAQZDZD [method:protected] => POST [endpoint:protected] => https://graph.facebook.com/v2.6/me/messages [headers:protected] => Array ( [Content-Type] => application/x-www-form-urlencoded ) [params:protected] => Array ( [message] => Array ( [text] => Hello, wadup?!!! ) [recipient] => Array ( [id] => 100002192125690 ) ) [files:protected] => Array ( ) [eTag:protected] => [graphVersion:protected] => v2.3 ) [thrownException:protected] => )

I have also tried using the recipients Facebook ID according to the documentation, but it doesn't work still.

Erisan Olasheni
  • 2,395
  • 17
  • 20
  • are you trying to REPLY to a message? because that would be important, you can only reply. – andyrandy Dec 18 '17 at 11:06
  • No, trying to send for the first. But Facebook states in their documentation that this could be possible provided that you have the recipient's phone number – Erisan Olasheni Dec 18 '17 at 11:23
  • the biggest question is: how did you even GET the phone number? – andyrandy Dec 18 '17 at 11:38
  • also, according to the docs, the "id" is not the phone number, there is a specific phone number field. the id is the one you get when users interact with your messenger bot. – andyrandy Dec 18 '17 at 11:40
  • @Aldan My client who has this job legally collects Names, emails and Phone numbers from his customers, now he wants to reach them through Facebook – Erisan Olasheni Dec 18 '17 at 17:36
  • @luschn thanks so much. But I have also tried the `phone_number` field in the `recipient` object but it still doesn't work – Erisan Olasheni Dec 18 '17 at 17:38

1 Answers1

1

I know is an old question, but i arrived here and this could helps other in their researches.

Facebook does not allow to a bot to write to an user if the user did not interact with the bot on the Messanger app.

You can find an exaustive list of the possible interactions here

Greedo
  • 3,438
  • 1
  • 13
  • 28