2017-04-08 70 views
0

我正在構建與api.ai集成的Facebook messenger bot。我的api.ai代理連接到webhook - api.php。我現在面臨的挑戰是我想通過webhook向機器人發送按鈕響應(格式化的消息)。 我跟着這個文檔 - https://docs.api.ai/docs/webhook錯誤顯示發送按鈕作爲響應在Facebook messenger bot

而且我在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; 
?> 

謝謝。

+0

你是什麼意思,它不顯示?這是不是傳遞的信息?如果這是問題,你可以發佈你的堆棧跟蹤嗎? – akinmail

回答

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