Dialogflow:我有关于webhook的问题,该问题可通过LINE(社交媒体)发送回服务器状态并向用户报告

时间:2019-02-22 09:36:08

标签: php dialogflow actions-on-google

我想创建一个可以在线(社交媒体)报告服务器状态(在线/离线)的聊天机器人,我使用Heroku将对话流链接为webhook(实现)。

但是我有一个问题,履约是这样的:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /webhook was not found on this server.</p>
</body></html>

这是我在Heroku上推送的代码:

<?php

$API_URL = 'XXXXXXXXX';
$ACCESS_TOKEN = 'YYYYYYYYYYY'; 
$POST_HEADER = array('Content-Type: application/json', 'Authorization: Bearer ' . $ACCESS_TOKEN);

$request = file_get_contents('php://input');  
$request_array = json_decode($request, true);  

if ( sizeof($request_array['events']) > 0 )
{

 foreach ($request_array['events'] as $event)
 {
  $reply_message = '';
  $reply_token = $event['replyToken'];

  if( $result['parameters'] == 'website'){
    $text = $result['parameters']['website'];
    $reply_message = 'I will check website status right now ('.$text.') ';
    $ip = gethostbyname($text);
    if (!$socket = @fsockopen($ip, 80, $errno, $errstr, 30))
    {
      //echo "Offline!";
      $reply_message = 'Offline';
    }
    else 
    {
      //echo "Online!";
      $reply_message = 'Online';
      fclose($socket);
    }

  }

  if( strlen($reply_message) > 0 )
  {
   //$reply_message = iconv("tis-620","utf-8",$reply_message);
   $data = [
    'replyToken' => $reply_token,
    'messages' => [['type' => 'text', 'text' => $reply_message]]
   ];
   $post_body = json_encode($data, JSON_UNESCAPED_UNICODE);

   $send_result = send_reply_message($API_URL, $POST_HEADER, $post_body);
   echo "Result: ".$send_result."\r\n";
  }
 }
}

function send_reply_message($url, $post_header, $post_body)
{
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 $result = curl_exec($ch);
 curl_close($ch);

 return $result;
}

?>

我想使用此代码获取在线状态并发送回Dialog flow,然后发送给Line(社交媒体)。

0 个答案:

没有答案