I created a chatbot that informs the user about the members of my (extended) family and where they live. I created a small MySQL database that stores this data, and I retrieve it using a PHP script when appropriate, depending on the user interaction with the chatbot.
My chatbot contains two intentions in addition to Default Fallback Intentand Default Welcome Intent:
The first intention ( Names) is learned in phrases such as "Who is John Smith?" and has an output context (called contextwith a duration of 10 questions). A possible answer to this question is: "John is my uncle." The second intention ( Location_context) is learned in phrases such as "Where does he live?". and has an input context (from Names). A possible answer to this question is: "John lives in New York."
The ledge Namescontains two parameters:
- parameter names: first name, last name.
- : @ sys.given-name, @ sys.last-name.
- value: $ given-name, $ last-name.
These two parameters represent the fully qualified name specified by the user. The hold Location_contextdoes not contain any parameters.
The PHP script looks like this:
<?php
$dbServername = '******************';
$dbUsername = '******************';
$dbPassword = '******************';
$dbName = '******************';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if($method == 'POST'){
$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody);
$action = $json->result->action;
$first_name = $json->result->contexts[0]->parameters->{'given-name'};
$last_name = $json->result->contexts[0]->parameters->{'last-name'};
$lifespan = $json->result->contexts[0]->lifespan;
$sql = "SELECT * FROM family WHERE name LIKE '%$first_name%$last_name%';";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$person = $row;
}
switch ($action) {
case 'Name':
$speech= "$first_name is my" . $person["name"] . ".";
break;
case 'Location':
$speech = "$first_name is living in {$person["location"]}.";
break;
default:
$speech = "Please ask me something more relevant to my family";
break;
}
}
else {
$speech = "Sorry, $first_name $last_name is not a member of my family.";
}
$response = new \stdClass();
$response->speech = $speech;
$response->displayText = $speech;
$response->source = "agent";
echo json_encode($response);
}
else
{
echo "Method not allowed";
}
?>
Dialogflow, , . " ?" : " - ". : " ?" : " -".
json Dialogflow :
{
"id": "*****************************",
"timestamp": "2018-04-04T08:26:39.993Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Where is he living"
"action": "Location",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "context",
"parameters": {
"given-name.original": "John",
"last-name.original": "Smith",
"given-name": "John",
"last-name": "Smith"
},
"lifespan": 9
}
],
"metadata": {
"intentId": "*****************************",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 93,
"intentName": "Location_context"
},
"fulfillment": {
"speech": "John is living in New York.",
"displayText": "John is living in New York.",
"messages": [
{
"type": 0,
"speech": "John is living in New York."
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "*****************************"
}
, ( Talk to my test app) Google, , " -". . . -, $first_name ( ) "-" - , . , $first_name $last_name ( ) mysql - .
, json Google Assistant, Dialogflow. , Google Assistant $lifespan 0 ( , ) $first_name $last_name json- , Dialoglow , , , json, . Google Assistant actions_capability_screen_output $json->result->contexts[0]->name , , , Dialogflow $json->result->contexts[0]->name context ( ).
, json contexts Google Assistant :
"contexts": [
{
"name": "actions_capability_screen_output",
"parameters": {},
"lifespan": 0
}
]
, , json DIalogflow contexts:
"contexts": [
{
"name": "context",
"parameters": {
"given-name.original": "John",
"last-name.original": "Smith",
"given-name": "John",
"last-name": "Smith"
},
"lifespan": 9
}
]
Google Assistant context json, Dialoglow?
json Google Assistant, Dialogflow?