-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
301 lines (255 loc) · 8.15 KB
/
response.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package alexa
import (
"encoding/json"
)
const (
version = "1.0"
plainTextOutputSpeechType = "PlainText"
ssmlOutputSpeechType = "SSML"
simpleCardType = "Simple"
standardCardType = "Standard"
linkAccountCardType = "LinkAccount"
)
// A Response allows a handler to construct a valid response to return to
// the Alexa service.
type Response interface {
LinkAccountCard()
PlainText(text string)
SSML(ssml string)
RepromptPlainText(text string)
RepromptSSML(ssml string)
ShouldEndSession(value bool)
SimpleCard(title, content string)
StandardCard(title, text, smallImageURL, largeImageURL string)
AudioPlayerStopperQueueClearer
}
// An AudioPlayer allows a handler to enqueue or replace the audio playing on
// a device.
type AudioPlayer interface {
ReplaceAllAudio(token, url string, offsetInMilliseconds int)
EnqueueAudio(token, url, expectedPreviousToken string, offsetInMilliseconds int)
ReplacedEnqueuedAudio(token, url string, offsetInMilliseconds int)
}
// An AudioStopper allows a handler to stop the playback of audio on a device.
type AudioStopper interface {
StopAudio()
}
// An AudioQueueClearer allows a handler to clear the current audio queue for
// a device.
type AudioQueueClearer interface {
ClearEnqueuedAudio()
ClearAllAudio()
}
// An AudioStopperQueueClearer allows a handler to stop the currently playing
// audio for a device and clear any queued audio.
type AudioStopperQueueClearer interface {
AudioStopper
AudioQueueClearer
}
// An AudioPlayerStopperQueueClearer allows a handler to play or enqueue audio
// to be played, stop any currently playing audio, or clear any queued audio on
// a device.
type AudioPlayerStopperQueueClearer interface {
AudioPlayer
AudioStopper
AudioQueueClearer
}
type playDirective struct {
Type string `json:"type"`
PlayBehavior string `json:"playBehavior"`
AudioItem playDirectiveAudioItem `json:"audioItem"`
}
type playDirectiveAudioItem struct {
Stream playDirectiveAudioStream `json:"stream"`
}
type playDirectiveAudioStream struct {
URL string `json:"url"`
Token string `json:"token"`
ExpectedPreviousToken *string `json:"expectedPreviousToken,omitempty"`
OffsetInMilliseconds int `json:"offsetInMilliseconds"`
}
type stopDirective struct {
Type string `json:"type"`
}
type clearAudioQueueDirective struct {
Type string `json:"type"`
ClearBehavior string `json:"clearBehavior"`
}
type outputSpeech struct {
SSML *string `json:"ssml,omitempty"`
Text *string `json:"text,omitempty"`
Type string `json:"type"`
}
type image struct {
LargeImageURL string `json:"largeImageUrl"`
SmallImageURL string `json:"smallImageUrl"`
}
type card struct {
Content *string `json:"content,omitempty"`
Image *image `json:"image,omitempty"`
Text *string `json:"text,omitempty"`
Title *string `json:"title,omitempty"`
Type string `json:"type"`
}
// Response passes data back to Alexa.
type responseBuilder struct {
Version string `json:"version"`
Response *response `json:"response"`
}
type response struct {
Card *card `json:"card,omitempty"`
OutputSpeech *outputSpeech `json:"outputSpeech,omitempty"`
Reprompt *reprompt `json:"reprompt,omitempty"`
Directives *responseDirectives `json:"directives,omitempty"`
ShouldEndSession *bool `json:"shouldEndSession,omitempty"`
}
type responseDirectives struct {
playDirective *playDirective
stopAudioDirective *stopDirective
clearAudioQueueDirective *clearAudioQueueDirective
}
func (d responseDirectives) MarshalJSON() ([]byte, error) {
ds := []interface{}{}
if d.playDirective != nil {
ds = append(ds, d.playDirective)
}
if d.stopAudioDirective != nil {
ds = append(ds, d.stopAudioDirective)
}
if d.clearAudioQueueDirective != nil {
ds = append(ds, d.clearAudioQueueDirective)
}
return json.Marshal(ds)
}
type reprompt struct {
OutputSpeech *outputSpeech `json:"outputSpeech"`
}
func (b *responseBuilder) PlainText(text string) {
if b.Response.OutputSpeech == nil {
b.Response.OutputSpeech = &outputSpeech{}
}
b.Response.OutputSpeech.SSML = nil
b.Response.OutputSpeech.Text = &text
b.Response.OutputSpeech.Type = plainTextOutputSpeechType
}
func (b *responseBuilder) SSML(ssml string) {
if b.Response.OutputSpeech == nil {
b.Response.OutputSpeech = &outputSpeech{}
}
b.Response.OutputSpeech.SSML = &ssml
b.Response.OutputSpeech.Text = nil
b.Response.OutputSpeech.Type = ssmlOutputSpeechType
}
func (b *responseBuilder) SimpleCard(title, content string) {
if b.Response.Card == nil {
b.Response.Card = &card{}
}
b.Response.Card.Content = &content
b.Response.Card.Image = nil
b.Response.Card.Text = nil
b.Response.Card.Title = &title
b.Response.Card.Type = simpleCardType
}
func (b *responseBuilder) StandardCard(title, text, smallImageURL, largeImageURL string) {
if b.Response.Card == nil {
b.Response.Card = &card{}
}
b.Response.Card.Content = nil
b.Response.Card.Image = &image{smallImageURL, largeImageURL}
b.Response.Card.Text = &text
b.Response.Card.Title = &title
b.Response.Card.Type = standardCardType
}
func (b *responseBuilder) LinkAccountCard() {
if b.Response.Card == nil {
b.Response.Card = &card{}
}
b.Response.Card.Content = nil
b.Response.Card.Image = nil
b.Response.Card.Text = nil
b.Response.Card.Title = nil
b.Response.Card.Type = linkAccountCardType
}
func (b *responseBuilder) RepromptPlainText(text string) {
if b.Response.Reprompt == nil {
b.Response.Reprompt = &reprompt{&outputSpeech{}}
}
b.Response.Reprompt.OutputSpeech.SSML = nil
b.Response.Reprompt.OutputSpeech.Text = &text
b.Response.Reprompt.OutputSpeech.Type = plainTextOutputSpeechType
}
func (b *responseBuilder) RepromptSSML(ssml string) {
if b.Response.Reprompt == nil {
b.Response.Reprompt = &reprompt{&outputSpeech{}}
}
b.Response.Reprompt.OutputSpeech.SSML = &ssml
b.Response.Reprompt.OutputSpeech.Text = nil
b.Response.Reprompt.OutputSpeech.Type = ssmlOutputSpeechType
}
func (b *responseBuilder) ShouldEndSession(value bool) {
b.Response.ShouldEndSession = &value
}
func (b *responseBuilder) ReplaceAllAudio(token, url string, offsetInMilliseconds int) {
if b.Response.Directives == nil {
b.Response.Directives = &responseDirectives{}
}
b.Response.Directives.playDirective = &playDirective{
Type: "AudioPlayer.Play",
PlayBehavior: "REPLACE_ALL",
AudioItem: playDirectiveAudioItem{
playDirectiveAudioStream{
OffsetInMilliseconds: offsetInMilliseconds,
Token: token,
URL: url,
},
},
}
}
func (b *responseBuilder) EnqueueAudio(token, expectedPreviousToken, url string, offsetInMilliseconds int) {
if b.Response.Directives == nil {
b.Response.Directives = &responseDirectives{}
}
b.Response.Directives.playDirective = &playDirective{
Type: "AudioPlayer.Play",
PlayBehavior: "ENQUEUE",
AudioItem: playDirectiveAudioItem{
playDirectiveAudioStream{
ExpectedPreviousToken: &expectedPreviousToken,
OffsetInMilliseconds: offsetInMilliseconds,
Token: token,
URL: url,
},
},
}
}
func (b *responseBuilder) ReplacedEnqueuedAudio(token, url string, offsetInMilliseconds int) {
if b.Response.Directives == nil {
b.Response.Directives = &responseDirectives{}
}
b.Response.Directives.playDirective = &playDirective{
Type: "AudioPlayer.Play",
PlayBehavior: "REPLACE_ENQUEUED",
AudioItem: playDirectiveAudioItem{
playDirectiveAudioStream{
OffsetInMilliseconds: offsetInMilliseconds,
Token: token,
URL: url,
},
},
}
}
func (b responseBuilder) StopAudio() {
b.Response.Directives.stopAudioDirective = &stopDirective{Type: "AudioPlayer.Stop"}
}
func (b responseBuilder) ClearEnqueuedAudio() {
b.Response.Directives.clearAudioQueueDirective = &clearAudioQueueDirective{
Type: "AudioPlayer.ClearQueue",
ClearBehavior: "CLEAR_ENQUEUED",
}
}
func (b responseBuilder) ClearAllAudio() {
b.Response.Directives.clearAudioQueueDirective = &clearAudioQueueDirective{
Type: "AudioPlayer.ClearQueue",
ClearBehavior: "CLEAR_ALL",
}
}