Facebook Chat bot (PHP webhook) wysyłania wielu odpowiedzi

głosy
2

My Facebook Chat bot działa, ale to wysyłanie wielu wiadomości z powrotem po mojej pierwszej wiadomości do niego. To jest mój skrypt webhook (i docenić to bardzo szorstki pracy przykład):

$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);
Utwórz 13/04/2016 o 21:04
źródło użytkownik
W innych językach...                            


3 odpowiedzi

głosy
8

FB uderza url webhook z oryginalnej wiadomości przychodzących i przetwarza je. Jesteś następnie wysyłając odpowiedź z powrotem do użytkownika i skryptu. Wtedy, gdy wiadomość zostanie dostarczona do użytkownika, FB wysyła potwierdzenia dostarczenia do adresu URL webhook. Ponieważ skrypt jest zawsze ustawiony na wysyłanie „Hej Lee!” za każdym razem, że to się nazywa, callback dostawa jest rzeczywiście wywołując kolejną wiadomość do wysłania, a następnie kolejne potwierdzenie dostawy przychodzi, a następnie proces ten powtarza to samo. Aby rozwiązać ten problem, umieścić if wokół swojego kodu, aby wysłać wiadomość. Oto przykład.

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

Nadzieję, że pomaga.

Odpowiedział 14/04/2016 o 00:58
źródło użytkownik

głosy
8

Myślę, że to dlatego, że nie sprawdza, czy wiadomości wysyłane są puste:

spróbuj zamiast tego:

$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);
}
Odpowiedział 14/04/2016 o 07:18
źródło użytkownik

głosy
0

Próbowaliśmy to samo, pierwszy wniosek posiada rzeczywistą wiadomość użytkownika, pozostałe wnioski nie. Ja po prostu wysłać odpowiedź, jeśli
$message = $input['entry'][0]['messaging'][0]['message']['text'];nie jest null:

if ($message){
//send your message here
}
Odpowiedział 18/11/2016 o 18:12
źródło użytkownik

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