2016-06-16 7 views
-1

Facebookのメッセンジャーのボットを開発中です。私はテストページ、フェイスブックアプリ、WebHookページをPHPで作成しました。フェイスブックメッセンジャーボット失敗のWebhook

テストページにボットを登録しましたが、結果は肯定的でしたが、ページにメッセージを送信しようとすると、ページにアクセスできない場合のように応答しません。

ここに私のPHPスクリプト:あなたが一緒にそれらすべてを組み合わせる前に

$access_token = "EAAY...ZD"; 
$verify_token = "verifica_token"; 
$hub_verify_token = null; 

if(isset($_REQUEST['hub_challenge'])) { 
    $challenge = $_REQUEST['hub_challenge']; 
    $hub_verify_token = $_REQUEST['hub_verify_token']; 
} 


if ($hub_verify_token === $verify_token) { 
    echo $challenge; 
} 


// handle bot's anwser 
$input = json_decode(file_get_contents('php://input'), true); 
$senderId = $input['entry'][0]['messaging'][0]['sender']['id']; 
$messageText = $input['entry'][0]['messaging'][0]['message']['text']; 
$answer = "I don't understand. Ask me 'hi'."; 
if($messageText == "hi") { 
    $answer = "Hello"; 
} 
$response = [ 
    'recipient' => [ 'id' => $senderId ], 
    'message' => [ 'text' => $answer ] 
]; 
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); 
curl_exec($ch); 
curl_close($ch); 

答えて

0

は独立して、以下のコードを試してみてください。

<?php 

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://graph.facebook.com/v2.6/me/messages?access_token=EAAY...ZD", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "POST", 
    CURLOPT_POSTFIELDS => "{\n recipient: {id:".$senderId."},\n message: {text: \"".$answer."\"}\n}", 
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache", 
    "content-type: application/json" 
), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 
+0

@iownthegameこんにちは、私は、この変数にこのコードを試してみました: $ SENDERID = 100000061458447 //必要があり、私のFacebookのID 私はこのエラー私のリターンを実行します。 { "エラー":{ "メッセージ"、": "(#100)Invalid fbid"、 "type": "OAuthException"、 "code":100、 "fbtrace_id": "HKdGEATQe78"}} –

+0

あなたのfbidで試してみることはできません。 bot – iownthegame

+0

okにメッセージを送信したときにボットが受信したが、ボットにメッセージを書き込むと、WebHookはメッセージを受信しません。 –

関連する問題