फेसबुक चैट बॉट (PHP webhook) से अधिक जवाबों भेजने

वोट
2

मेरा फेसबुक चैट बॉट काम कर रहा है, लेकिन यह यह करने के लिए अपने प्रारंभिक संदेश के बाद एक से अधिक संदेशों वापस भेज रहा है। यह मेरा 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

अमेरिकन प्लान मूल भेजे संदेश के साथ अपने webhook यूआरएल हिट और आप इसे संसाधित। इसके बाद आप एक प्रतिक्रिया वापस उपयोगकर्ता के भेज रहे हैं और स्क्रिप्ट समाप्त होता है। फिर, एक बार संदेश उपयोगकर्ता को प्रदान किए, अमेरिकन प्लान 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);

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