【问题标题】:Facebook Chat bot (PHP webhook) sending multiple repliesFacebook 聊天机器人 (PHP webhook) 发送多个回复
【发布时间】:2016-08-05 04:58:28
【问题描述】:

我的 Facebook 聊天机器人正在运行,但在我向其发送初始消息后,它会发回多条消息。这是我的 webhook 脚本(我很欣赏这是一个非常粗糙的工作示例):

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'MY_VERIFICATION_TOKEN') {
  echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];


//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
    }, 
    "message":{
        "text":"Hey Lee!"
    }
}';

//Encode the array into JSON.
$jsonDataEncoded = $jsonData;

//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, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
$result = curl_exec($ch);

【问题讨论】:

    标签: facebook-graph-api webhooks facebook-messenger facebook-chatbot


    【解决方案1】:

    我认为这是因为您没有验证发送的消息是否为空:

    试试这个:

    $challenge = $_REQUEST['hub_challenge'];
    $verify_token = $_REQUEST['hub_verify_token'];
    
    if ($verify_token === 'MY_VERIFICATION_TOKEN') {
      echo $challenge;
    }
    
    $input = json_decode(file_get_contents('php://input'), true);
    
    $sender = $input['entry'][0]['messaging'][0]['sender']['id'];
    $message = $input['entry'][0]['messaging'][0]['message']['text'];
    
    
    //API Url
    $url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    //The JSON data.
    $jsonData = '{
        "recipient":{
            "id":"'.$sender.'"
        }, 
        "message":{
            "text":"Hey Lee!"
        }
    }';
    
    //Encode the array into JSON.
    $jsonDataEncoded = $jsonData;
    
    //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, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    
    //Execute the request
    if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
    }
    

    【讨论】:

    • 出现这些奇怪的错误:`
      已弃用:自动填充 $HTTP_RAW_POST_DATA 已弃用,将在未来版本中删除。为避免此警告,请在 php.ini 中将“always_populate_raw_post_data”设置为“-1”,并改用 php://input 流。在 0

      警告行的 Unknown 中:无法修改标头信息 - 标头已在 Unknown 第 0 行
      {"error":{"message":"无效的 OAuth 访问令牌。","type":"OAuthException","code":190," fbtrace_id":"BsNG\/mJVYem"}} ^As
    • 你需要把exit();在回声$挑战之后;行
    • 我也面临类似的问题,但我添加了一个空消息检查(交付)。当我检查 webhook URL 时,似乎它被击中了两次。我将 ngrok 与本地服务器一起用作 webhook,我可以清楚地看到 webhook url 被访问了两次,并且两次都填充了消息字段。有什么想法吗??
    • 感谢您的回答!有用!奇怪的错误... :)
    【解决方案2】:

    FB 使用原始传入消息点击您的 webhook url,然后您对其进行处理。然后,您将响应发送回用户,脚本结束。然后,一旦消息传递给用户,FB 就会向 webhook url 发送传递确认。由于您的脚本始终设置为发送“Hey Lee!”任何时候调用它,传递回调实际上都会触发另一个消息发送,然后另一个传递确认进来,然后这个过程会自我重复。要解决此问题,请在您的代码周围放置一个 if 语句以发送消息。这是一个例子。

    $challenge = $_REQUEST['hub_challenge'];
    $verify_token = $_REQUEST['hub_verify_token'];
    
    if ($verify_token === 'MY_VERIFICATION_TOKEN') {
      echo $challenge;
    }
    
    $input = json_decode(file_get_contents('php://input'), true);
    
    $sender = $input['entry'][0]['messaging'][0]['sender']['id'];
    $message = $input['entry'][0]['messaging'][0]['message']['text'];
    
    
    //API Url
    $url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    if($message=="hello")
    {
            //The JSON data.
            $jsonData = '{
            "recipient":{
                    "id":"'.$sender.'"
            },
            "message":{
                    "text":"Hey Lee!"
            }
            }';
    }
    
    //Encode the array into JSON.
    $jsonDataEncoded = $jsonData;
    
    //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, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    
    //Execute the request
    $result = curl_exec($ch);

    希望对您有所帮助。

    【讨论】:

    • 我正在使用示例。 Webhook 已通过验证,当我尝试向我的机器人发送消息时,它什么也没返回。我也不知道如何调试 Webhook 页面
    • 我也遇到了调试的挑战,但是因为 Drupal(我的聊天机器人的后端平台)很强大,它有一个名为 Watchdog 的功能,它是一个日志模块,因此您可以将所有内容发送到 Watchdog 进行调试。使过程非常非常容易。像这样: $input = json_decode(file_get_contents('php://input'), true); watchdog("收到 Webhook 数据", '
      ' . print_r( $input, true) . '
      ');
    • 发布 webhook 以进行交付确认的事实让我很感动。非常感谢@Drew。
    • 很棒的解释。很有意义。
    【解决方案3】:

    尝试相同,第一个请求包含实际的用户消息,其他请求没有。如果
    $message = $input['entry'][0]['messaging'][0]['message']['text']; 不为空,我只会发送答案:

    if ($message){
    //send your message here
    }
    

    【讨论】:

    • 如何对一条消息发送多个响应,如果有人打招呼,机器人应该回复 Hi(第一条消息),你叫什么名字(第二条消息)..
    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2017-04-12
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多