对话框流中的动态响应丰富

时间:2018-10-23 08:55:04

标签: php json artificial-intelligence dialogflow actions-on-google

我正在使用带有PHP和JSON的dialogflow构建聊天机器人。我最近在互联网上苦苦挣扎,试图弄清楚如何动态修改丰富的响应。例如,如果用户对TOP 10感兴趣,那么我需要一个包含10个项目的丰富对象,而如果用户需要前5个,则需要5个项目的响应。你们谁知道如何实现这一目标?非常感谢

索引看起来像这样:

<?php


function prepareResponse($incoming)
    {
            $data = $incoming;
            header('Content-Type: application/json');
            $json_data = json_decode($data);
            echo json_encode($json_data);
    }



$method = $_SERVER['REQUEST_METHOD'];
//
// Process only when request method is POST
//
if($method == 'POST')
{
        $requestBody = file_get_contents('php://input');
        $json = json_decode($requestBody);
        $querytype = $json->originalDetectIntentRequest->payload->inputs[0]->rawInputs[0]->query;

        $countryname =$json->queryResult->parameters->{'geo-country'};
        $materialname =$json->queryResult->parameters->material;
        $topwhat=$json->queryResult->parameters->topnumber;
//topnumber stores the number of the top ten wanted by the user
        $intentname=$json->queryResult->intent->displayName;
        $touchoption=$json->queryResult->queryText; 
        $email =$json->queryResult->parameters->emailadress;
        //vardump($json);
        //print_r($json);


       $myfile = fopen("newfile.txt", "w+") or die("Unable to open file!");
        $txt = print_r($json,true);

        //$txt = $json->originalDetectIntentRequest->payload->inputs[0]->rawInputs[0]->query;
        fwrite($myfile, $txt);
        fclose($myfile);


        if (strcasecmp($querytype, "Top number of sold commodities") == 0)
          {
                $outgoing = include 'top.php';
                prepareResponse($outgoing);


          }
}













?>

然后top.php文件应该是这样的json文件

<?php

$db = new PDO('mysql:host=160.153.147.21;dbname=****', '****', '***');


$sqlplantshortname="Select SUBSTRING_INDEX(materialdescription, ' -', 1) as newcategory ,SUM(stock) AS 'STOCKS' from tempcommodityinventoryview where sapname like '$countryname' and wbselementsapname like '$countryname' and SUBSTRING_INDEX(materialdescription, ' -', 1) goup by'$materialname' order by 'STOCKS' limit '$topwhat' ";

$resultplantshortname= $db->prepare($sqlplantshortname);


$resultplantshortname->execute();
        $rowplantshortname = $resultplantshortname->fetch(PDO::FETCH_ASSOC);


$data = <<<DATA

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "richResponse": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": " Top sold commodities of  $countryname is  $nameplantshortname bla bla"
            }
          }
        ]
      }
    }
  }
}
DATA;

return $data;

?>

现在top.php文件是一个简单的响应,而我希望它是类型列表的丰富响应,以显示出最畅销的前十名或前五名的商品

0 个答案:

没有答案