Facebook Chat bot (PHP webhook) trimiterea mai multor raspunsuri

voturi
2

My Facebook chat-ul bot este de lucru, dar este trimiterea înapoi mai multe mesaje după mesajul meu inițial să-l. Acesta este scenariul meu webhook (apreciez că este un exemplu de lucru foarte dur):

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'MY_VERIFICATION_TOKEN') {
  echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];


//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = '{
    recipient:{
        id:'.$sender.'
    }, 
    message:{
        text:Hey Lee!
    }
}';

//Encode the array into JSON.
$jsonDataEncoded = $jsonData;

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
$result = curl_exec($ch);
Întrebat 13/04/2016 la 21:04
sursa de către utilizator
În alte limbi...                            


3 răspunsuri

voturi
8

FB hit-uri URL-ul dvs. webhook cu mesajul de intrare original și îl prelucrează. Atunci va trimite un răspuns înapoi la utilizator și script-ul se termină. Apoi, odată ce mesajul este livrat către utilizator, FB trimite o confirmare de livrare la URL-ul webhook. Deoarece script-ul dvs. este întotdeauna setat pentru a trimite „Hei Lee!“ în orice moment care este numit, callback de livrare este de fapt, declanșând un alt mesaj pentru a fi trimis, iar apoi o altă confirmare de livrare vine, iar apoi acest proces se repeta de sine. Pentru a remedia acest lucru, a pus o declarație în cazul în jurul valorii de codul pentru a trimite un mesaj. Iată un exemplu.

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'MY_VERIFICATION_TOKEN') {
  echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];


//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';

//Initiate cURL.
$ch = curl_init($url);

if($message=="hello")
{
        //The JSON data.
        $jsonData = '{
        "recipient":{
                "id":"'.$sender.'"
        },
        "message":{
                "text":"Hey Lee!"
        }
        }';
}

//Encode the array into JSON.
$jsonDataEncoded = $jsonData;

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
$result = curl_exec($ch);

Sper că vă ajută.

Publicat 14/04/2016 la 00:58
sursa de către utilizator

voturi
8

Cred că e pentru că nu verifică dacă mesajele trimise sunt goale:

încercați acest lucru în schimb:

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'MY_VERIFICATION_TOKEN') {
  echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];


//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
    }, 
    "message":{
        "text":"Hey Lee!"
    }
}';

//Encode the array into JSON.
$jsonDataEncoded = $jsonData;

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
Publicat 14/04/2016 la 07:18
sursa de către utilizator

voturi
0

A încercat același lucru, prima cerere deține mesajul de utilizator real, celelalte solicitări nu. Tocmai am trimite un răspuns în cazul în care
$message = $input['entry'][0]['messaging'][0]['message']['text'];nu este nulă:

if ($message){
//send your message here
}
Publicat 18/11/2016 la 18:12
sursa de către utilizator

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