Facebook Chat Bot (PHP webhook) l'invio di risposte multiple

voti
2

La mia chat di Facebook bot sta lavorando, ma è l'invio di messaggi multipli di nuovo dopo il mio messaggio iniziale ad esso. Questo è il mio script webhook (Mi rendo conto che è un esempio di lavoro molto approssimativa):

$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);
È pubblicato 13/04/2016 alle 21:04
fonte dall'utente
In altre lingue...                            


3 risposte

voti
8

FB colpisce il vostro URL webhook con il messaggio in arrivo originale e si elabora. Si sono quindi inviare una risposta indietro per l'utente e lo script finisce. Poi, una volta che il messaggio viene consegnato all'utente, FB invia una conferma di recapito per l'url webhook. Dal momento che lo script è sempre impostato per inviare "Hey Lee!" ogni volta che viene chiamata, la richiamata consegna è in realtà innescando un altro messaggio da inviare, e poi un altro conferma di consegna entra, e quindi tale processo si ripete lo stesso. Per risolvere questo problema, inserire un'istruzione if attorno al codice per inviare un messaggio. Ecco un esempio.

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

Spero possa aiutare.

Risposto il 14/04/2016 a 00:58
fonte dall'utente

voti
8

Penso che sia perché non si verifica se i messaggi inviati sono vuote:

Prova a modificare:

$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);
}
Risposto il 14/04/2016 a 07:18
fonte dall'utente

voti
0

Ho provato la stessa, la prima richiesta detiene il messaggio utente effettivo, le altre richieste non. Mi basta inviare una risposta se la
$message = $input['entry'][0]['messaging'][0]['message']['text'];non è nullo:

if ($message){
//send your message here
}
Risposto il 18/11/2016 a 18:12
fonte dall'utente

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