다음 두 가이드 (http://blog.adnansiddiqi.me/develop-your-first-facebook-messenger-bot-in-php/ 및 https://medium.com/@nadeem.manzoor0/facebook-messenger-platform-web-hook-setup-in-php-893ead06746b#.lcpp0jh9o)를 따라 PHP로 메신저 봇을 만들려고합니다.Messenger Bot에서 hub_verify_index 및 hub_challenge 오류가 발생했습니다.
그리고 메신저 봇의 localhost
코드를 사용하기 위해 nGrok v2.1.18
을 사용했습니다. 내 localhost
에 이미 xampp control panel v3.2.1
을 설치했습니다.
<?php
/* validate verify token needed for setting up web hook */
if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'here_is_my_token') {
echo $_GET['hub_challenge'];
return;
} else {
echo 'Invalid Verify Token';
return;
}
} else {
echo $_GET['hub_verify_token'];
echo $_GET['hub_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'];
/**
* Some Basic rules to validate incoming messages
*/
if(preg_match('[time|current time|now]', strtolower($message))) {
// Make request to Time API
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
$result = file_get_contents("http://www.timeapi.org/utc/now?format=%25a%20%25b%20%25d%20%25I:%25M:%25S%20%25Y");
if($result != '') {
$message_to_reply = $result;
}
} else {
$message_to_reply = 'Huh! what do you mean?';
}
print $message_to_reply;
//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":"'.$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);
}
?>
그리고 나는 이미이 같은 내 페이스 북 앱 페이지에 URL을 webhooks 설정 :
여기 내 webhook.php
입니다 https://903....ngrok.io/FunBot/webhook.php
또한 토큰 확인 설정합니다. 문제 없습니다.
내 페이지에서 메시지를 보내면 nGrok
의 답글을 200 OK
으로 볼 수 있습니다. 그러나 messeger 봇에서는 아무것도 응답하지 않습니다.
따라서 json_decode(file_get_contents('php://input'), true)
에서 로그하려고하면 오류가 없습니다.
그러나 $_GET['hub_verify_token']
및 $_GET['hub_challenge']
을 인쇄하려고하면 "Undefined index: hub_challenge in C:\xampp\htdocs\FunBot\webhook.php on line ....."
및 "Undefined index: hub_verify_token in ......."
오류가 발생합니다.
undefined index
오류 결과는 nGrok
입니다.
이 두 개의 "undefined index"
문제로 인해 봇이 회신하지 못할 수도 있습니다.
me/messages?
을 $url
에서 page id
또는 다른 ID로 변경해야합니까?
이미 봇이 응답하지 않는 문제에 대해 많은 게시물을 읽었으며 제대로 작동하지 않습니다. 처음에는 봇과 함께하기 때문에 어떤 부분이 잘못 됐는지 모르겠습니다.
모든 제안에 대해 매우 감사드립니다.
오류 메시지에서 줄 번호를 제거하지 마십시오! 이 스크립트의 맨 위에있는'$ _GET [ 'hub_verify_token']'만을 사용하고 있으며 그것은'isset' 문에 싸여 있습니다 - 그래서 그 코드로'Undefined index : hub_verify_token' 오류를 얻는 것은 불가능합니다 . – CBroe
@CBroe 나는'isset()'으로 확인하지 않고 echo하려고 할 때,'undefined index' 에러가 발생했다. 나는 그것의 투표로 자격이 없다고 생각한다. – Cloud
@CBroe 이미 내 질문을 업데이트했습니다. 다시 확인해 주시겠습니까? – Cloud