forked from kevin-mitchell/alexa-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlexa.php
166 lines (126 loc) · 3.05 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
<?php namespace Develpr\AlexaApp;
use Develpr\AlexaApp\Contracts\AmazonEchoDevice;
use Develpr\AlexaApp\Contracts\DeviceProvider;
use Develpr\AlexaApp\Request\AlexaRequest;
use Develpr\AlexaApp\Response\AlexaResponse;
use Develpr\AlexaApp\Response\Card;
use Develpr\AlexaApp\Response\Speech;
class Alexa {
/**
* @var \Develpr\AlexaApp\Contracts\AlexaRequest
*/
private $alexaRequest;
/**
* @var array
*/
private $session;
/**
* @var Contracts\DeviceProvider
*/
private $deviceProvider;
/**
* @var array
*/
private $alexaConfig;
/**
* @var AmazonEchoDevice | null
*/
private $device;
/**
* @var bool
*/
private $isAlexaRequest;
public function __construct(AlexaRequest $alexaRequest, DeviceProvider $deviceProvider, array $alexaConfig)
{
$this->alexaRequest = $alexaRequest;
$this->deviceProvider = $deviceProvider;
$this->setupSession();
$this->alexaConfig = $alexaConfig;
$this->isAlexaRequest = $this->alexaRequest->isAlexaRequest();
}
public function isAlexaRequest()
{
return $this->isAlexaRequest;
}
public function requestType()
{
return $this->alexaRequest->getRequestType();
}
public function request()
{
return $this->alexaRequest;
}
public function response()
{
return new AlexaResponse;
}
public function say($statementWords)
{
$response = new AlexaResponse(new Speech($statementWords));
return $response;
}
public function ask($question)
{
$response = new AlexaResponse(new Speech($question));
$response->setIsPrompt(true);
return $response;
}
public function card($title = "", $subtitle = "", $content = "")
{
$response = new AlexaResponse();
$response->setCard(new Card($title, $subtitle, $content));
return $response;
}
public function device($attributes = [])
{
if( ! $this->isAlexaRequest() )
return null;
if( ! is_null($this->device) )
return $this->device;
if( ! array_key_exists($this->alexaConfig['device']['device_identifier'], $attributes))
$attributes[$this->alexaConfig['device']['device_identifier']] = $this->alexaRequest->getUserId();
$result = $this->deviceProvider->retrieveByCredentials($attributes);
if($result instanceof AmazonEchoDevice)
$this->device = $result;
return $result;
}
public function slot($requestedSlot = "")
{
return $this->alexaRequest->slot($requestedSlot);
}
public function slots()
{
return $this->alexaRequest->slots();
}
public function session($key = null, $value = null)
{
if( ! is_null($value) ){
$this->setSession($key, $value);
}
else if( is_null($key) ){
return $this->session;
}
else{
return array_key_exists($key, $this->session) ? $this->session[$key] : null;
}
}
public function setSession($key, $value = null)
{
if( is_array($key) ){
foreach($key as $aKey => $aValue){
$this->session[$aKey] = $aValue;
}
}
else if( ! is_null($key) ) {
$this->session[$key] = $value;
}
}
public function unsetSession($key)
{
unset($this->session[$key]);
}
private function setupSession()
{
$this->session = $this->alexaRequest->getSession();
}
}