2016-10-26 3 views
0

다음 두 가이드 (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입니다. undefine index result image

이 두 개의 "undefined index" 문제로 인해 봇이 회신하지 못할 수도 있습니다.

me/messages?$url에서 page id 또는 다른 ID로 변경해야합니까?

이미 봇이 응답하지 않는 문제에 대해 많은 게시물을 읽었으며 제대로 작동하지 않습니다. 처음에는 봇과 함께하기 때문에 어떤 부분이 잘못 됐는지 모르겠습니다.

모든 제안에 대해 매우 감사드립니다.

+0

오류 메시지에서 줄 번호를 제거하지 마십시오! 이 스크립트의 맨 위에있는'$ _GET [ 'hub_verify_token']'만을 사용하고 있으며 그것은'isset' 문에 싸여 있습니다 - 그래서 그 코드로'Undefined index : hub_verify_token' 오류를 얻는 것은 불가능합니다 . – CBroe

+0

@CBroe 나는'isset()'으로 확인하지 않고 echo하려고 할 때,'undefined index' 에러가 발생했다. 나는 그것의 투표로 자격이 없다고 생각한다. – Cloud

+0

@CBroe 이미 내 질문을 업데이트했습니다. 다시 확인해 주시겠습니까? – Cloud

답변

0

마지막으로 해결책을 찾았습니다. 주된 문제는 SSL 문제입니다. SSL 인증서를 사용하지 않으면 코드가 올바르더라도 봇이 아무것도 응답하지 않습니다. 그리고 SSL이 없습니다. 그래서, 나는 이상한 문제에 갇혔다.

이제 Heroku을 사용하여 코드 저장소를 업로드하고 URL이 Heroku 인 webhooks를 다시 설정했습니다. Heroku을 사용하면 SSL에 대해 걱정할 필요가 없습니다. 자, 다 괜찮아.

link은 메신저 봇을 만드는 데 매우 유용합니다.

답변이 도움이되기를 바랍니다.

관련 문제