我正在开发一个facebook聊天机器人。我正面临一个我无法解决的问题。我在laravel开发这个。在这里,我无法获得回发有效负载。这是我的代码
public function index(Request $request) { if ($request->hub_verify_token === $this->verifyToken) { echo $request->hub_challenge; exit; } $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']; $pageId = $input['entry'][0]['id']; $accessToken = "access_token_that_got_from_fb"; //set Message if($messageText != "") { $answer = "Howdy! User"; } if($messageText == "hello") { $answer = 'Testing hello from bot'; } foreach ($input['entry'][0]['messaging'] as $message) { // When bot receive message from user if (!empty($message['postback'])) { $answer = 'got it tada'; } } //send message to facebook bot $response = [ 'recipient' => [ 'id' => $senderId ], 'message' => [ 'text' => $answer ] //json_encode($request->getContent()) ]; $ch = curl_init('https://graph.facebook.com/v2.11/me/messages?access_token='.$accessToken); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $result = curl_exec($ch); curl_close($ch); }
我的路线是
<div class="wrapper">
<div class="ct-title">Title
<p>This is some text in a paragraph.</p>
</div>
<div class="ct-content-1">6</div>
<div class="ct-content-2">8</div>
<div class="ct-content-3">12</div>
</div>
这里的消息正在发挥作用。但是回发有效负载请求不会转到服务器。另一方面,在普通的php文件中使用相同的代码我能够获得回发paylod。
$hubVerifyToken = 'chatbot'; $accessToken = "access_token"; // check token at setup if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) { echo $_REQUEST['hub_challenge']; exit; } // 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']; $postback = isset($input['entry'][0]['messaging'][0]['postback']['payload']) ? $input['entry'][0]['messaging'][0]['postback']['payload'] : '' ; //set Message if($messageText == "hi") { $answer = "Hello"; } if($messageText == "hello") { $answer = "Hello there, welcome to Chatleads"; } if($postback) { $answer = "postback TADA"; } //send message to facebook bot $response = [ 'recipient' => [ 'id' => $senderId ], 'message' => [ 'text' => $answer ] ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.11/me/messages?access_token=$accessToken"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response)); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch);
如何解决这个问题?有人能告诉我一条路吗?
答案 0 :(得分:0)
通过检查laravel.log文件来解决它。
SUMIF
messageText应该是这样的。这就是它导致错误的原因。