Facebook Chat bot (PHP webhook) het verzenden van meerdere antwoorden

stemmen
2

Mijn Facebook-chat bot werkt, maar het is het terugsturen van meerdere berichten na mijn eerste bericht om het. Dit is mijn webhook script (ik waardeer het is een zeer ruwe werkend voorbeeld):

$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);
De vraag is gesteld op 13/04/2016 om 21:04
bron van user
In andere talen...                            


3 antwoorden

stemmen
8

FB raakt uw webhook url met de originele inkomend bericht en je het te verwerken. U bent dan een antwoord terug te sturen naar de gebruiker en het script eindigt. Dan, als de boodschap aan de gebruiker wordt geleverd, FB stuurt een ontvangstbevestiging aan de webhook url. Aangezien uw script altijd is ingesteld op te sturen "Hey Lee!" elke keer dat het wordt genoemd, wordt de levering callback daadwerkelijk triggering ander bericht te verzenden, en vervolgens nog eens leveringsbevestiging binnenkomt, en dat proces wordt herhalende zichzelf. Om dit te verhelpen, zet een if-statement rond je code om een ​​bericht te sturen. Hier is een voorbeeld.

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

Hoop dat het helpt.

antwoordde op 14/04/2016 om 00:58
bron van user

stemmen
8

Ik denk dat het komt omdat je niet controleren of de verzonden berichten zijn leeg:

probeer dit in plaats daarvan:

$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);
}
antwoordde op 14/04/2016 om 07:18
bron van user

stemmen
0

Hetzelfde geprobeerd, het eerste verzoek houdt de feitelijke gebruiker bericht, de andere verzoeken niet. Ik stuur een antwoord als het
$message = $input['entry'][0]['messaging'][0]['message']['text'];niet ongeldig is:

if ($message){
//send your message here
}
antwoordde op 18/11/2016 om 18:12
bron van user

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