0

I have created agent in API.AI and I have created a database in Google Cloud Platform. I am able to connect to it using Navicat Premium to update the database of the bot. Also, I am able to use webhook using Heroku with PHP scripting. And I'm able to get response successfully with this code below

<?php 


$method = $_SERVER['REQUEST_METHOD'];

// Process only when method is POST
if($method == 'POST'){

    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);
    $intent = $json->result->metadata->intentName;
    $entertype = $json->result->contexts->entertainmentType.original;

    $response = array(
                      "messages"=> [array(
                                       "type"=> 1,
                                       "platform"=> "facebook",
                                       "title"=> 'MBO Melaka Mall',
                                       "subtitle"=> 'Tingkat 2, Kompleks Melaka Mall, Lebuh Air Keroh, 75450 Melaka',
                                       "imageUrl"=> "http://s-yoolk-images.s3.amazonaws.com/my/gallery_images/medium/1435340336/67323?1435340336",
                                       "buttons"=> [ array(
                                                      "text"=> "Search in Google",
                                                      "postback"=>"https://www.google.com/search?q=mbo+melaka+mall&oq=mbo+melaka+mall&aqs=chrome..69i57j5.6761j0j7&sourceid=chrome&ie=UTF-8"
                                                  )
                                            ]
                                   )]
                        );

    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}

?>

However, after I update this, I'm not able to get any response and I got error code 500 as internal server error. Please help me.

<?php 


$method = $_SERVER['REQUEST_METHOD'];

// Process only when method is POST
if($method == 'POST'){

    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);
    $intent = $json->result->metadata->intentName;
    $entertype = $json->result->contexts->entertainmentType.original;


    $username="root";
    $password="pass";
    $database="qwertytrek";
    $connection=mysql_connect("35.200.241.96",$username,$password);*/
    if(!$connection)
    {
        $speech = "No Connection";
    }
    else 
    {
        $speech = "Connected to Google";
    }
    $selectdb = mysql_select_db($connection,$database) or die("Unable to select database");
    if ($selectdb) 
    {
        //echo = "database selected";
    }


    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}

function cybercafe($user)

{
}
?>
  • 4
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 06 '18 at 22:12
  • 1
    Have you checked your error logs? – Patrick Q Feb 06 '18 at 22:17
  • @JayBlanchard I just did. But I seem to have errors still as below `"status": { "code": 206, "errorType": "partial_content", "errorDetails": "Webhook call failed. Error: Request timeout.", "webhookTimedOut": true },` – Keshvar Enza Feb 06 '18 at 23:18
  • That means your webhook is taking too much time to respond. Dialogflow requires webhooks respond in 5s or less. You need to update your code to respond more quickly. – mattcarrollcode Feb 07 '18 at 17:00
  • @matthewayne is there any method to improve the response speed ? Thank you in advance. – Keshvar Enza Feb 08 '18 at 02:51

0 Answers0