Come accedere al nome di una persona con messenger chat bot? (Facebook Messenger SDK)

voti
3

Ciao io sono molto nuovo per la codifica in PHP e Messenger Bot.

Mi chiedevo come avrei accedere al nome di qualcuno che è stato messaggistica mia chat bot.

È pubblicato 30/05/2016 alle 05:36
fonte dall'utente
In altre lingue...                            


3 risposte

voti
5

Il profilo utente API può aiutare.

utilizzare la event.sender.idricevuta dal server bot Messenger (/ webhook), e seguire la richiesta sottostante

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>"

allora si potrebbe ottenere il JSON restituita sotto

{
     "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"
}
Risposto il 30/05/2016 a 06:45
fonte dall'utente

voti
0

È possibile utilizzare il frammento di PHP qui sotto per ottenere nome dell'utente

$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']
Risposto il 13/06/2016 a 07:21
fonte dall'utente

voti
0

Hedge @Rajesh

Il tuo codice ha un piccolo errore:

$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 devono essere convertiti in array associativo prima di potervi accedere in questo modo: $obj['first_name']

Vedere http://php.net/manual/en/function.json-decode.php per i dettagli.

Risposto il 29/08/2017 a 09:26
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more