-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dob.php
76 lines (67 loc) · 2.02 KB
/
dob.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return substr($haystack, 0, $length) === $needle;
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) { // if (!$length)
return true;
}
return substr($haystack, -$length) === $needle;
}
function processMessage($json)
{
try {
$action = $json['queryResult']['action'];
$params = $json['queryResult']['parameters'];
if ($action == "checkDateOfBirth") {
$dob = (string)$params["date"];
if (startsWith($dob, 'UUUU')) {
//change context to ask for year
$sessionid = $json["session"];
$contextName = $sessionid . '/contexts/awaiting_year_of_birth';
$contextToDelete = $sessionid . '/contexts/awaiting_patient_name';
sendMessage(array(
"fulfillmentText" => 'What is the year of birth?',
"outputContexts" => array(
array(
"name" => $contextName,
"lifespanCount" => 1
),
array(
"name" => $contextToDelete,
"lifespanCount" => 0
)
)
));
} else {
sendMessage(array(
"fulfillmentText" => "What is the patient's name?"
));
}
}
} catch (Exception $e) {
sendMessage([
'fulfillmentText' => $e->getMessage()
]);
}
}
function sendMessage($parameters)
{
echo json_encode($parameters);
}
try {
$update_response = file_get_contents('php://input');
$update = json_decode($update_response, true);
if (isset($update['queryResult']['action'])) {
processMessage($update);
}
} catch (Exception $e) {
sendMessage([
'fulfillmentText' => 'Unable to parse: ' . $e->getMessage()
]);
}
?>