-
Notifications
You must be signed in to change notification settings - Fork 22
/
alexa.php
170 lines (128 loc) · 3.78 KB
/
alexa.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
class AlexaSession {
private $session;
public function __construct($session) {
$this->setSession($session);
}
private function setSession($session) {
$this->session = $session;
}
public function isNewSession() {
return($this->session->new);
}
public function getSessionID() {
return($this->session->sessionId);
}
public function getApplicationID() {
return($this->session->application->applicationId);
}
public function getUserID() {
return($this->session->user->userId);
}
public function getUserAccessToken() {
return($this->session->user->accessToken);
}
}
class AlexaRequest {
private $request;
public function __construct($request) {
$this->setRequest($request);
}
private function setRequest($request) {
$this->request = $request;
}
public function getType() {
return($this->request->type);
}
public function getRequestID() {
return($this->request->requestId);
}
public function getTimestamp() {
return($this->request->timestamp);
}
public function getLocale() {
return($this->request->locale);
}
public function getIntent() {
return($this->request->intent->name);
}
}
class Alexa {
private $input;
private $session;
private $request;
private $application_id = "";
private $card = "";
private $reprompt = "";
private $outputspeech = "";
public function __construct() {
$this->getInput();
}
private function getInput() {
$this->input = json_decode(file_get_contents("php://input"));
if(isset($this->input->session)) {
$this->session = new AlexaSession($this->input->session);
}
if(isset($this->input->request)) {
$this->request = new AlexaRequest($this->input->request);
}
}
public function setApplicationID($application_id) {
$this->application_id = $application_id;
}
public function auth() {
if($this->input != "") {
if($this->application_id == $this->getSession()->getApplicationID()) {
return(true);
}
}
return(false);
}
public function getVersion() {
return($this->input->version);
}
public function getSession() {
return($this->session);
}
public function getRequest() {
return($this->request);
}
public function setApplicationName($app_name) {
$this->app_name = $app_name;
}
public function setCard($card) {
$this->card = $card;
}
public function setReprompt($reprompt) {
$this->reprompt = $reprompt;
}
public function setOutputSpeech($outputspeech) {
$this->outputspeech = $outputspeech;
}
public function displayOutput() {
?>
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "<?php print($this->outputspeech); ?>"
},
"card": {
"type": "Simple",
"title": "<?php print($this->app_name); ?>",
"content": "<?php print($this->card); ?>"
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "<?php print($this->reprompt); ?>"
}
},
"shouldEndSession": true
}
}
<?php
}
}
?>