Facebook Chat-Bot (PHP Webhook) Senden mehrerer Antworten

stimmen
2

Mein Facebook-Chat-Bot funktioniert, aber es mehrere Nachrichten nach meiner ersten Nachricht an sie zurückschicken. Das ist mein Webhook Skript (ich schätze, es ist ein sehr grobes Arbeitsbeispiel):

$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);
Veröffentlicht am 13/04/2016 um 21:04
quelle vom benutzer
In anderen Sprachen...                            


3 antworten

stimmen
8

FB trifft Ihre Webhook URL mit der ursprünglichen eingehenden Nachricht und Sie es verarbeiten. Sie senden dann eine Antwort zurück an den Benutzer und das Skript beendet. Dann, wenn die Nachricht an den Benutzer geliefert wird, sendet FB eine Empfangsbestätigung an den Webhook url. Da Ihr Skript ist immer gesetzt zu senden „Hey Lee!“ jederzeit, dass es die Lieferung Rückruf aufgerufen wird, auslöst tatsächlich eine andere Nachricht und eine andere Lieferbestätigung kommt dann gesendet werden, werden, und dann wird dieser Prozess es selbst zu wiederholen. Um dies zu beheben, setzen Sie eine if-Anweisung um den Code, um eine Nachricht zu senden. Hier ist ein Beispiel.

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

Ich hoffe, das hilft.

Beantwortet am 14/04/2016 um 00:58
quelle vom benutzer

stimmen
8

Ich denke, es ist, weil Sie nicht überprüfen, ob die gesendeten Nachrichten sind leer:

versuchen Sie stattdessen:

$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);
}
Beantwortet am 14/04/2016 um 07:18
quelle vom benutzer

stimmen
0

Versucht das gleiche, hält die erste Anforderung die tatsächliche Benutzernachricht, die anderen Anforderungen nicht. Ich sende nur eine Antwort , wenn die
$message = $input['entry'][0]['messaging'][0]['message']['text'];nicht null ist:

if ($message){
//send your message here
}
Beantwortet am 18/11/2016 um 18:12
quelle vom benutzer

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