-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.php
128 lines (93 loc) · 2.72 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
<?php
$data = file_get_contents("php://input");
$query = json_decode( $data );
# error_log( print_r( $query, 1 ) );
$me = array(
'version' => '0.2',
'name' => 'OpenStack'
);
$guid = '5c33db4b-91b8-4e40-8765-b8f849de6b68';
$userid = 'AFPPR46VI4HFCERSD2ENKTJBTCGHF6J6ERFIWCEI7GP4YDXFRBEJI';
$help = "Help Message Goes Here";
include('../validate-echo-request-php/valid_request.php');
$valid = validate_request( $guid, $userid );
if ( ! $valid['success'] ) {
error_log( 'Request failed: ' . $valid['message'] );
die();
}
if ( $query ) {
$action = $query->request->intent->name;
if ( $action == "RandomProject" ) {
$response = randomproject();
}
elseif ( $action == "GetProject" ) {
$response = getproject( $query );
}
elseif ( $action = 'GetHelp' ) {
$response = $help;
}
else {
$response = $help;
}
sendresponse( $response, $me );
} else {
sendresponse( $help, $me );
}
/*
'Alexa, ask openstack to tell me about a random project'
Returns info about a random openstack project
*/
function randomproject() {
# Yes, I'm reading the file twice. Sue me.
$yaml = file('./governance/reference/projects.yaml');
$projects = array();
foreach ($yaml as $line) {
if ( preg_match( '/^\w/', $line ) ) {
$projects[] = $line;
}
}
$project = $projects[ array_rand( $projects ) ];
$project = preg_replace( '/:\n/s', '', $project );
return projectinfo( $project );
}
/*
Returns info about specifed project
'Alexa, tell me about OpenStack Manilla'
*/
function getproject( $query ) {
$project = $query->request->intent->slots->Project->value;
return projectinfo( $project );
}
/*
Slurps info about project $project from the yaml file.
*/
function projectinfo( $project ) {
$yaml = file_get_contents('./governance/reference/projects.yaml');
# Yes, I know this is the wrong way to parse YAML. The various
# extensions are giving me fits.
$yaml = preg_replace('/^.*\n' . $project . ':.+?mission: >/is', '', $yaml );
$yaml = preg_replace('/\n url:.*$/is', '', $yaml );
return $project . 's mission is ' . $yaml;
}
/*
Format and return the response back to Alexa
*/
function sendresponse( $response, $me ) {
$response = array (
"version" => $me['version'],
'response' => array (
'outputSpeech' => array (
'type' => 'PlainText',
'text' => $response
),
'card' => array (
'type' => 'Simple',
'title' => $me['name'],
'content' => $response
),
'shouldEndSession' => 'true'
),
);
echo json_encode($response);
}
?>