【问题标题】:Messenger bot not working信使机器人不工作
【发布时间】:2016-12-15 02:59:01
【问题描述】:

我正在尝试设置一个非常简单的机器人。我生成了一个访问令牌,在我的 Web 服务器上创建了一个简单的 index.php 文件(经过 SSL 认证),设置了一个指向我的 index.php 文件的 webhook,将我的 webhook 订阅到我的页面事件(developers.facebook.com 上的所有内容) ),给我的机器人发消息但没有得到答复。可能是什么问题?

(我已经检查了所有内容:两个令牌,我是管理员等)

这是我的代码:

<?php
$access_token = "i-filled-this-out";
$verify_token = "i-also-filled-this-out";
$hub_verify_token = null;

if(isset($_REQUEST['hub_challenge'])) {
    $challenge = $_REQUEST['hub_challenge'];
    $hub_verify_token = $_REQUEST['hub_verify_token'];
}

if ($hub_verify_token === $verify_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'];
$message_to_reply = '';

$message_to_reply = 'Huh! what do you mean?';

//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
    },
    "message":{
        "text":"'.$message_to_reply.'"
    }
}';
//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'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
}

【问题讨论】:

    标签: php bots facebook-messenger


    【解决方案1】:

    确保您的 webhook 正在向 facebook 发送的 POST 请求返回 HTTP 200 响应。

    您正在运行什么网络服务器?检查日志以查看您的 webhook 是否正在接收来自 facebook 的 POST 请求(并返回 200)。

    测试您的 webhook 的最佳方法是使用 postman 之类的工具来模仿 facebook 发布的标准 POST 并检查您提供的响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2016-07-29
      • 1970-01-01
      • 2016-09-30
      相关资源
      最近更新 更多