Facebook Messenger 机器人 - 收到一条第一条消息

Facebook messenger bot - receives single and very first message

Facebook Messenger 机器人 - 每 2 分钟连续收到一条消息和第一条消息。

我已经在 PHP 中创建了机器人并设置了 webhook。但是无论我是否有 added/received 新消息,我每两分钟都会收到 webhook 触发器。

还有一件事是我们只收到第一条消息。该消息之后有很多新消息,但我们只收到一条消息。

我哪里错了?我已经关注了这篇文章: http://blog.adnansiddiqi.me/develop-your-first-facebook-messenger-bot-in-php/

我们得到了解决方案:

$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = isset($input['entry'][0]['messaging'][0]['message']['text']) ? $input['entry'][0]['messaging'][0]['message']['text'] : '';
if (!empty($input['entry'][0]['messaging'])) {

    foreach ($input['entry'][0]['messaging'] as $message) {

        $command = "";

        // When bot receive message from user
        if (!empty($message['message'])) {
            $command = $message['message']['text'];
        }
        // When bot receive button click from user
        else if (!empty($message['postback'])) {
            $command = $message['postback']['payload'];
        }
    }
}



$pagetoken = "PAGE TOKEN"; // Facebook TOKEN
if ($command) {

    if ($command == "hii") {
        $message_to_reply = "test_response";
    } else if ($command == "need more info") {
        $message_to_reply = "Please fill form at link ";
    } else if ($command == "\ud83d\ude00") {
        $message_to_reply = "smiley";
    }

    if ($message_to_reply != "") {
        $url = "https://graph.facebook.com/v2.6/me/messages?access_token=$pagetoken";
//Initiate cURL.
        $ch = curl_init($url);
//The JSON data.
        $jsonData = '{
        "recipient":{
        "id":"' . $sender . '"
        }, 
         "message":{
            "text":"' . $message_to_reply . '"
         }
       }';
        $jsonDataEncoded = $jsonData;
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        if (!empty($input['entry'][0]['messaging'][0]['message'])) {
            $result = curl_exec($ch);
        }
        curl_close($ch);
    }
}

header('HTTP/1.1 200 OK');  //  This line needs to be added

die;