在Facebook messenger bot中显示发送按钮作为响应时出错

时间:2017-04-08 18:06:03

标签: php facebook facebook-messenger-bot dialogflow

我正在使用api.ai集成构建一个Facebook messenger bot。我的api.ai代理连接到webhook - api.php。我现在的挑战是我想通过我的webhook向机器人发送按钮响应(格式化消息)。 我按照这个文件 - https://docs.api.ai/docs/webhook

我在api.php中这样做但是没有显示按钮

<?php
header('Content-Type: application/json');
ob_start();
$json = file_get_contents('php://input'); 
$request = json_decode($json, true);
$action = $request["result"]["action"];
$parameters = $request["result"]["parameters"];

$data =json_encode([
'speech' => "test",
'displayText' => "test",
'data' => "{
        'facebook': {
  'recipient':{
    'id':'USER_ID'
  },
  'message':{
    'attachment':{
      'type':'template',
      'payload':{
        'template_type':'button',
        'text':'What do you want to do next?',
        'buttons':[
          {
            'type':'web_url',
            'url':'https://petersapparel.parseapp.com',
            'title':'Show Website'
          },
          {
            'type':'postback',
            'title':'Start Chatting',
            'payload':'USER_DEFINED_PAYLOAD'
          }
        ]
      }
    }
  }
}
    }
 }",
'source' => "source"
]);
echo $data;
?>

谢谢。

1 个答案:

答案 0 :(得分:0)

    $qrString = "Date|Location|Category";
    $qrActions = "ActionSetDate|ActionSetLocation|ActionSetCategory";
    $webhookResponseArray['data']['facebook']['text'] = "Choose an action...";
    // make array of prompts and actions
    $qrArray = array();
    $actionArray = explode('|', $qrActions);
    $i = 0;
    $a = array();
    foreach (explode('|', $qrString) as $qrButton)
        {
        $a['content_type'] = 'text';
        $a['title'] = $qrButton;
        $a['payload'] = $actionArray[$i];
        $qrArray[] = $a;
        $i++;
        }
    $webhookResponseArray['data']['facebook']['quick_replies'] = $qrArray;
    // encode and send webhookResponseArray