PHP中的Fecebook Messenger Bot并不总是响应用户 [英] Fecebook Messenger Bot in PHP doesn't always respond to user

查看:267
本文介绍了PHP中的Fecebook Messenger Bot并不总是响应用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ access_token =xxxxxxx; 
$ 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] ['message'] [0] ['sender'] ['id'];
$ message = $ input ['entry'] [0] ['message'] [0] ['message'] ['text'];

$ url ='https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token。';

$ ch = curl_init($ url);

if($ message ==嗨)
{
$ jsonData ='{
收件人:{
id: '$发件人''
},
message:{
text:hello!
}
}';
}

$ jsonDataEncoded = $ jsonData;

curl_setopt($ ch,CURLOPT_POST,1);

curl_setopt($ ch,CURLOPT_POSTFIELDS,$ jsonDataEncoded);

curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-Type:application / json'));

$ result = curl_exec($ ch);

我的cron工作与开发人员指南相同

  curl -ik -X POSThttps://graph.facebook.com/v2.6/me/subscribed_apps?access_token=xxxxxx

所以基本上,除了连接和一个响应之外什么都没有。
当我个人发送嗨(作为页面所有者和应用程序所有者)时,我的机器人总是正确响应,但是当其他人尝试说Hi-bot有时候会有所回应,有时候不会(通常不是5个机器人)回覆一次)



此外,当我访问我的脚本网址时,它会给我错误:

 <$$${message:(#100)参数收件人是必需的,type:OAuthException,code:100,fbtrace_id:DvrO1UEw5BJ} 

请帮助我设置正确。

解决方案

如果您忘记设置内容类型,也可能会发生这种情况。


I have relatively simple Facebook Messenger bot in php for research purposes:

$access_token = "xxxxxxx";
$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'];

$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token.';

$ch = curl_init($url);

if($message=="hi")
{
        $jsonData = '{
        "recipient":{
                "id":"'.$sender.'"
        },
    "message":{
            "text":"hello!"
    }
    }';
}

$jsonDataEncoded = $jsonData;

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$result = curl_exec($ch);

and my cron job is same as in developer guide

curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=xxxxxx"

So basically, nothing besides connection and one response. When i personally send "hi" (as a page owner and app owner), my bot always responds correctly, but when other people try to say hi - bot sometimes responds, sometimes not (usually not, in 5 cases bot responded once)

Additionally, when i visit my script url, it gives me error:

{"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"DvrO1UEw5BJ"}}

Please help me to set this right.

解决方案

This might also happen if you forget to set the content-type as well.

这篇关于PHP中的Fecebook Messenger Bot并不总是响应用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆