Facebook Chat bot (PHP webhook) την αποστολή πολλαπλών απαντήσεων

ψήφοι
2

My Facebook συνομιλία bot λειτουργεί αλλά έχει στείλει πίσω πολλά μηνύματα μετά την αρχική μου μήνυμα σε αυτό. Αυτό είναι το σενάριο webhook μου (εκτιμώ ότι είναι μια πολύ πρόχειρη παράδειγμα εργασίας):

$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);
Δημοσιεύθηκε 13/04/2016 στις 21:04
πηγή χρήστη
Σε άλλες γλώσσες...                            


3 απαντήσεις

ψήφοι
8

FB χτυπά webhook url σας με την αρχική εισερχόμενο μήνυμα και να το επεξεργαστεί. Στη συνέχεια, θα στείλει μια απάντηση πίσω στον χρήστη και το σενάριο τελειώνει. Στη συνέχεια, όταν το μήνυμα παραδοθεί στον χρήστη, FB στέλνει μια επιβεβαίωση παράδοσης στο url webhook. Από το script σας είναι πάντα οριστεί να στείλει «Γεια σου Λι!» κάθε φορά που καλείται η επιστροφή κλήσης παράδοση είναι στην πραγματικότητα ενεργοποιεί ένα άλλο μήνυμα που πρέπει να σταλεί, και στη συνέχεια μια άλλη επιβεβαίωση παράδοσης έρχεται, και στη συνέχεια η διαδικασία επαναλαμβάνεται μόνος. Για να το διορθώσετε αυτό, βάλτε μια εντολή if γύρω κωδικό σας για να στείλετε ένα μήνυμα. Εδώ είναι ένα παράδειγμα.

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

Ελπίδα που βοηθά.

Απαντήθηκε 14/04/2016 στις 00:58
πηγή χρήστη

ψήφοι
8

Νομίζω ότι είναι επειδή δεν έχετε ελέγξει αν τα μηνύματα που αποστέλλονται είναι κενές:

δοκιμάστε αυτό αντ 'αυτού:

$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);
}
Απαντήθηκε 14/04/2016 στις 07:18
πηγή χρήστη

ψήφοι
0

Προσπάθησε το ίδιο, η πρώτη αίτηση κατέχει το πραγματικό μήνυμα των χρηστών, τα άλλα αιτήματα δεν είναι. Απλά στείλτε μια απάντηση αν η
$message = $input['entry'][0]['messaging'][0]['message']['text'];δεν είναι μηδέν:

if ($message){
//send your message here
}
Απαντήθηκε 18/11/2016 στις 18:12
πηγή χρήστη

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