PHP MySQL Dialogflow

ⅰ亾dé卋堺 提交于 2019-12-01 14:20:59

whenever the webhook gets triggered you need to listen to actions from JSON responses, from the action made the switch case of actions

index.php

<?php
require 'get_enews.php';

function processMessage($input) {
    $action = $input["result"]["action"];
    switch($action){

        case 'getNews':
            $param = $input["result"]["parameters"]["number"];
            getNews($param);
            break;

        default :
            sendMessage(array(
                "source" => "RMC",
                "speech" => "I am not able to understand. what do you want ?",
                "displayText" => "I am not able to understand. what do you want ?",
                "contextOut" => array()
            ));
    }
}
function sendMessage($parameters) {
    header('Content-Type: application/json');
    $data = str_replace('\/','/',json_encode($parameters));
    echo $data;
}
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input["result"]["action"])) {
    processMessage($input);
}
?>

get_enews.php

<?php
function getNews($param){
    require 'config.php';
    $getNews="";
    $Query="SELECT link FROM public.news WHERE year='$param'";
    $Result=pg_query($con,$Query);
    if(isset($Result) && !empty($Result) && pg_num_rows($Result) > 0){
    $row=pg_fetch_assoc($Result);
    $getNews= "Here is details that you require - Link: " . $row["link"];
        $arr=array(
            "source" => "RMC",
            "speech" => $getNews,
            "displayText" => $getNews,
        );
        sendMessage($arr);
    }else{
        $arr=array(
            "source" => "RMC",
            "speech" => "No year matched in database.",
            "displayText" => "No year matched in database.",
        );
        sendMessage($arr);
    }
}
?>

So when the action gets caught it will get executed and goes in the getNews($param); function here I am getting year as a response from the user in my case and I am executing the query in the database and giving back a response from the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!