4

Hi I'm very new to coding in PHP and Messenger Bots.

I was wondering how i would access the name of someone who was messaging my chat bot.

Hey
  • 87
  • 1
  • 6
  • Have you considered accepting any answer here? Have any one of them answered your question? – YakovL Aug 29 '17 at 10:09

3 Answers3

7

The User Profile API may help you.

use the event.sender.id received from the messenger bot server (/webhook), and follow the request below

curl -X GET "https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=<PAGE_ACCESS_TOKEN>"

then you could get the returned json below

{
     "first_name": "Peter",
     "last_name": "Chang",
     "profile_pic": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13055603_10105219398495383_8237637584159975445_n.jpg?oh=1d241d4b6d4dac50eaf9bb73288ea192&oe=57AF5C03&__gda__=1470213755_ab17c8c8e3a0a447fed3f272fa2179ce",
     "locale": "en_US",
     "timezone": -7,
     "gender": "male"
}
Hui-Yu Lee
  • 909
  • 8
  • 20
  • So i just paste the curl thing into the php code right? how do i access the returned json? (i.e. i want to say "hello" + ) – Hey Jun 01 '16 at 03:23
  • see how to make a http get request and access the returned json in php http://stackoverflow.com/questions/15617512/get-json-object-from-url – Hui-Yu Lee Jun 01 '16 at 04:23
  • Which permissions should I active? – operator Mar 30 '17 at 08:45
  • @enriquo you don't need further permissions, you just need PAGE_ACCESS_TOKEN, please see https://developers.facebook.com/docs/messenger-platform/user-profile – Hui-Yu Lee Apr 12 '17 at 15:55
0

You can use the below PHP snippet to get name of the user

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name&access_token=<PAGE_ACCESS_TOKEN>');
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
echo 'Hi ' . $obj['first_name'] . ' ' . $obj['last_name']
Rajesh Hegde
  • 2,702
  • 1
  • 18
  • 23
  • Sorry I've never actually used PHP. How am I supposed to use your PHP snippet(i.e. where do i put it in the code)? I have the code that i tried based off yours in a comment below. – Hey Jun 15 '16 at 04:45
  • if($messageText == "hi" ) { //$answer = "Hello" ; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/?fields=first_name,last_name&access_token='); $result = curl_exec($ch); curl_close($ch); $obj = json_decode($result); $answer = 'Hi ' . $obj['first_name'] . ' ' . $obj['last_name']; } else if ($messageText != "") { $answer = "Sorry I dont think I understand what you said. Try asking me something else!"; } – Hey Jun 15 '16 at 04:46
  • @RayyaanMustafa, you have to paste this code where you are processing the message text. – Rajesh Hegde Jun 15 '16 at 05:40
0

@Rajesh Hedge

Your code have a small error:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name&access_token=<PAGE_ACCESS_TOKEN>');
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result); // *** here
echo 'Hi ' . $obj['first_name'] . ' ' . $obj['last_name']

$obj = json_decode($result, **true**);

$result need to be converted into associative array before you can access it like this: $obj['first_name']

See http://php.net/manual/en/function.json-decode.php for details.

Norbert Nowocin
  • 149
  • 1
  • 6