dsgdg54ef4365 2017-05-08 13:56 采纳率: 0%
浏览 148
已采纳

Facebook Messenger Chatbot - 我无法回复回复

I'm writing a bot for Facebook in PHP. I'm able to send text and send and answer to quickreplies. However is impossible to me to answer to a postback. I use ngrok for localhost testing purposes but I know that it's not that the problem since 1. it doesn't work on my vps neither 2. All the other functionalities work as expected.

My code for sending the 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);
}

Then, when I receive the post-back I get this from FB messenger :

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

and my code to parse it :

$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"
      }
    }';
  }
}

This is sent by the curl function as the message before.

However the messenger never receives an answer : enter image description here

Could anyone help me finding out where's the problem ?

Thank you very much

  • 写回答

2条回答 默认 最新

  • duanchun5520 2017-05-14 12:34
    关注

    There was a mistake in my code at this part :

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

    since the $message_body var is not 'message' but a 'postback', the curl was not sending the response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程