Facebook Messenger Chatbot - 我无法回复回传

Facebook Messenger Chatbot - I can't answer to a postback

我正在 PHP 为 Facebook 编写一个机器人。我能够发送文本并发送和回答快速回复。但是我不可能回答 post 回复。我将 ngrok 用于本地主机测试目的,但我知道这不是问题,因为 1. 它在我的 vps 上也不起作用 2. 所有其他功能都按预期工作。

我发送 post-back 的代码:

$jsonData = '{
      "recipient":{
        "id":"'.$sender.'"
      },
      "message":{
        "attachment":{
          "type":"template",
          "payload":{
            "template_type":"button",
            "text":"What do you want to do next?",
            "buttons":[
              {
                "type":"postback",
                "title":"Start Chatting",
                "payload":"USER_DEFINED_PAYLOAD"
              }
            ]
          }
        }
      }
    }';
    $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
$ch = curl_init($url);

//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, $jsonData);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request

if(!empty($message_body['message'])){
  $result = curl_exec($ch);
}

然后,当我收到 post-back 时,我从 FB 信使那里得到了这个:

{
    "object": "page",
    "entry": [
        {
            "id": "590445400970275",
            "time": 1494251031827,
            "messaging": [
                {
                    "recipient": {
                        "id": "590445400970275"
                    },
                    "timestamp": 1494251031827,
                    "sender": {
                        "id": "1075794782546272"
                    },
                    "postback": {
                        "payload": "USER_DEFINED_PAYLOAD"
                    }
                }
            ]
        }
    ]
}

和我解析它的代码:

$input = json_decode(file_get_contents('php://input'), true);
$message_body = $input['entry'][0]['messaging'][0];
$sender = $message_body['sender']['id'];
if (isset($message_body['postback'])){
  //Reception d'un postback
  $postback = $message_body['postback']['payload'];
  if ($postback == "USER_DEFINED_PAYLOAD"){
    // The JSON data.
    $jsonData = '{
      "recipient":{
        "id":"'.$sender.'"
      },
      "message":{
        "text":"messagetoreply"
      }
    }';
  }
}

这是 curl 函数发送的,作为之前的消息。

然而,信使从未收到答复:

谁能帮我找出问题所在?

非常感谢

这部分我的代码有错误:

if(!empty($message_body['message'])){
  $result = curl_exec($ch);
}

因为 $message_body 变量不是 'message' 而是 'postback',curl 没有发送响应。