[英]php chatbot postback payload not working when using elseif
I'm making a Facebook chatbot and try to use postback payload but it didn't work , I have also checked in my log. 我正在制作一个Facebook聊天机器人,并尝试使用回发负载,但是它没有用,我还检查了我的日志。
Everything prints the right value but it didn't enter my else if block. 一切都输出正确的值,但如果输入阻塞,则没有输入我的else值。
My Code 我的密码
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = '';
$payload = $input['entry'][0]['messaging'][0]['postback']['payload'];
$fp = file_put_contents( 'request.log', date('Y-m-d H:i:s')."PAYLOAD = ".$payload, FILE_APPEND);
if (strpos($message, 'hi') != '') {
$message_to_reply = 'hi';
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"'.$message_to_reply.'",
},
}';
} elseif(strpos($payload, 'test') != '') {
$message_to_reply = 'PAYLOAD DIDNT WORK';
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"'.$message_to_reply.'",
},
}';
} else {
$message_to_reply= 'CHOOSE MENU';
$jsonData = '{
"recipient":{
"id":"'. $sender.'"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"'.$message_to_reply.'",
"buttons":[
{
"type":"web_url",
"url":"https://petersapparel.parseapp.com",
"title":"PROMOTION"
},
{
"type":"postback",
"title":"test",
"payload":"test"
}
]
}
}
}
}';
}
My if and else block works fine but when I click on button test, I get value $payload = 'test'
but I don't know why it didn't enter the elseif block. 我的if and else块工作正常,但是当我单击按钮测试时,我得到了
$payload = 'test'
但我不知道为什么它没有进入elseif块。
Please help me on this. 请帮我。
You need to change your condition like this: 您需要像这样更改条件:
if (strpos($message, 'hi') !== false)
elseif(strpos($payload, 'test') !== false) // As 0th position is considered as '' in if condition which you are comparing.
I saw you commenting in another question. 我看到您在另一个问题中发表评论。 Maybe my answer solves your problem as well?
也许我的答案也可以解决您的问题? It is not about detecting, but trigger the sending of a message containing a postback.
它与检测无关,而是触发包含回发的消息的发送。
See here: Facebook Chatbot postback not working 请参阅此处: Facebook Chatbot回发不起作用
In addition, make sure, you have postbacks checked in the webhook section of your app. 此外,请确保在应用程序的webhook部分中选中了回发。
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.