【问题标题】:Empty message from facebook messenger bot来自 facebook messenger bot 的空消息
【发布时间】:2018-01-21 14:21:44
【问题描述】:

我按照指南从here 构建了一个 facebook Messenger 机器人

但在我的软件中,我总是收到一个空响应。

这是我的机器人代码:



    define('PAGE_TOKEN',"xxx");
    define('VERIFY_TOKEN',"xxx");

    if(isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])) {
        if($_GET['hub_verify_token']==VERIFY_TOKEN && $_GET['hub_mode']=='subscribe') {
            echo $_GET['hub_challenge'];
        }
    }

    $input = json_decode(file_get_contents('php://input'), true);
    // Get the Senders Graph ID
    $sender = $input['entry'][0]['messaging'][0]['sender']['id'];
    // Get the returned message
    $message = $input['entry'][0]['messaging'][0]['message']['text'];

    error_log($input);
    error_log($sender);
    error_log($message);

验证阶段很好,但是当我在 error_log 文件中向我的机器人发送消息时,我发现只有空值:



    [Sun Aug 13 17:35:49.617919 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617951 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617972 2017] [:error] [pid 22501] [client 173.252.124.11:18834]

这里还有我的access_log:



    173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"

那么为什么 $input 变量总是空的?

【问题讨论】:

    标签: php facebook facebook-messenger facebook-messenger-bot


    【解决方案1】:
    173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"
    

    您的服务器使用 301 重定向响应 POST 请求,因此客户端 (Facebook) 必须接下来发出 GET 请求,因此您看不到更多 POST 数据。

    看到第一个 URL 是 /webhook,并被重定向到 /webhook/,这很可能只是一个自动尾斜杠重定向,一旦您在应用设置中指定带有尾斜杠的正确 URL 后应该修复开始。

    【讨论】:

    • 谢谢!你救了我的一天!我修改了https://mydomain/webhook/index.php 中的 webhook,现在我可以阅读消息了
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2016-09-06
    相关资源
    最近更新 更多