/
echo.php
145 lines (110 loc) · 3.06 KB
/
echo.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
$data = file_get_contents("php://input");
$query = json_decode( $data );
error_log( print_r( $query, 1 ) );
$me = array(
'version' => '0.2',
'name' => 'Fajita'
);
$guid = '179cac3e-f062-48e6-ba41-109a728d01fe';
$userid = 'AERVD6MDLOMTUHVRFQ2JMWVPXZSHPHCNYUCOEEXB43QOFQL22M36Q';
$help = "Hi. . I'm Fajita. . Ask me what's for dinner.";
try {
$db = new PDO('mysql:host=localhost;dbname=fajita;charset=utf8mb4', 'fajita', 'fajita');
}
catch(PDOException $e) {
error_log( $e->getMessage() );
}
include('../validate-echo-request-php/valid_request.php');
$valid = validate_request( $guid, $userid );
if ( ! $valid['success'] ) {
error_log( 'Request failed: ' . $valid['message'] );
die();
}
$vegetarian = 0;
if ( $query ) {
$action = $query->request->intent->name;
if ( $action == "GetVegetarian" ) {
$vegetarian = 1;
}
# Get a suggestion for dinner
if ( $action == 'GetVegetarian' || $action == 'GetMenu' ) {
$response = getmenu( $db, $vegetarian );
}
# Add an item to the menu
elseif ( $action == 'AddMenu' ) {
$response = addmenu( $db, $query );
}
# Read me the recipe
elseif ( $action == 'GetRecipe' ) {
$response = getrecipe( $db, $query );
}
# Help
else {
# if ( $action == 'AMAZON.HelpIntent' ) {
$response = $help;
}
}
else {
$response = $help;
}
sendresponse( $response, $me );
echo json_encode($response);
/*
Get a menu recommendation
getmenu( $db, $vegetarian );
*/
function getmenu( $db, $vegetarian = 0 ) {
# various ways to say it
$verb = array(
'Have',
'How about',
'Try',
'What about',
'You could have',
);
$dbq = "SELECT name FROM menu ";
# Vegetarian?
if ( $vegetarian ) {
$dbq = $dbq . " WHERE vegetarian IS TRUE ";
}
# Random
$dbq = $dbq . " ORDER BY RAND() LIMIT 0,1 ";
$sth = $db->prepare( $dbq );
$sth->execute();
$result = $sth->fetch();
$meal = $result['name'];
$recommend = $verb[ array_rand( $verb )] . ' '
. $meal . ' for dinner';
return( $recommend );
}
/*
Add an item to the menu
addmenu( $db, $query );
*/
function addmenu( $db, $query ) {
$menu = $query->request->intent->slots->Menu->value;
$sth = $db->prepare( "INSERT INTO menu (name)
values ( ? ) ");
$sth->execute( array( $menu ) );
$insertid = $db->lastInsertId();
$response = 'I added ' . $menu . ' to your menu options.';
return( $response );
}
/*
Read me the recipe
getrecipe( $db, $query );
*/
function getrecipe( $db, $query ) {
$menu = $query->request->intent->slots->Menu->value;
$sth = $db->prepare( "SELECT name, recipe
FROM menu
WHERE name = ?" );
$sth->execute( array( $menu ));
$result = $sth->fetch();
$recipe = isset( $result['recipe'] ) ?
$result['recipe'] :
"Either I can't find that menu item, or there is no defined recipe for it. . Look at fajita dot t m three dot org for a full listing.";
return( $recipe );
}
?>