-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
44 lines (32 loc) · 1.34 KB
/
process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\TextInput;
require __DIR__.'/vendor/autoload.php';
if(isset($_POST['submit'])) {
$userquery = $_POST['message'];
// create a new session
$credential = array('credentials' => "client-secret.json");
$sessionClient = new SessionsClient($credential);
try {
// replace the project id, since it is "fake"
// 12345 is the session id, which should not be changed within an entire conversation with a single user
$session = $sessionClient->sessionName("weatheragent-abcdef", 123456 ?: uniqid());
// prepare a new query input...
$queryInput = new QueryInput();
// ... and the inner text
$textInput = new TextInput();
$textInput->setText($userquery);
$textInput->setLanguageCode("en-US");
$queryInput->setText($textInput);
// send the request and get the response
$response = $sessionClient->detectIntent($session, $queryInput);
$queryResult = $response->getQueryResult();
// the textual response is in "fullfilment"
$fulfillmentText = $queryResult->getFulfillmentText();
// finally, respond to the ajax request
echo $fulfillmentText;
} finally {
$sessionClient->close();
}
}