Birden çok yanıtı gönderen Facebook Sohbet bot (PHP webhook)

oy
2

Benim Facebook sohbet bot çalışıyor ama buna benim ilk mesajdan sonra birden çok ileti geri gönderiyor. Bu benim webhook komut dosyası (ben çok kaba çalışma örneği var takdir) 'dir:

$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);
Oluştur 13/04/2016 saat 21:04
kaynak kullanıcı
Diğer dillerde...                            


3 cevaplar

oy
8

FB orijinal gelen mesajla webhook url vurur ve bunu işlemek. Daha sonra kullanıcıya geri bir yanıt gönderiyorsanız ve komut biter. mesajı kullanıcıya teslim edilir sonra, daha FB webhook URL'ye bir teslim onay gönderir. senaryonuz hep göndermek için ayarlanır beri "Hey Lee!" denir ki her zaman, teslimat geri arama aslında başka mesaj gönderilmek üzere tetikliyor ve sonra başka teslimat onay gelir ve daha sonra bu süreç o kendini tekrar ediyor. Bunu düzeltmek için bir mesaj göndermek için kodunun çevresindeki eğer bildiri koydu. İşte bir örnek.

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

Umarım yardımcı olur.

Cevap 14/04/2016 saat 00:58
kaynak kullanıcı

oy
8

Ben gönderilen mesajlar boş olup olmadığını doğrulamak yok çünkü düşünüyorum:

Bunun yerine bu deneyin:

$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);
}
Cevap 14/04/2016 saat 07:18
kaynak kullanıcı

oy
0

Güvenilir aynı, ilk isteğin gerçek kullanıcı mesaj, diğer istekler değil tutar. Ben sadece bir cevap göndermek
$message = $input['entry'][0]['messaging'][0]['message']['text'];boş değil:

if ($message){
//send your message here
}
Cevap 18/11/2016 saat 18:12
kaynak kullanıcı

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