forked from mikeflynn/go-alexa
/
echo.go
270 lines (218 loc) · 6.44 KB
/
echo.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
package skillserver
import (
"encoding/json"
"errors"
"time"
)
// Request Functions
func (this *EchoRequest) VerifyTimestamp() bool {
reqTimestamp, _ := time.Parse("2006-01-02T15:04:05Z", this.Request.Timestamp)
if time.Since(reqTimestamp) < time.Duration(150)*time.Second {
return true
}
return false
}
func (this *EchoRequest) VerifyAppID(myAppID string) bool {
if this.Session.Application.ApplicationID == myAppID ||
this.Context.System.Application.ApplicationID == myAppID {
return true
}
return false
}
func (this *EchoRequest) GetSessionID() string {
return this.Session.SessionID
}
func (this *EchoRequest) GetUserID() string {
return this.Session.User.UserID
}
func (this *EchoRequest) GetRequestType() string {
return this.Request.Type
}
func (this *EchoRequest) GetIntentName() string {
if this.GetRequestType() == "IntentRequest" {
return this.Request.Intent.Name
}
return this.GetRequestType()
}
func (this *EchoRequest) GetSlotValue(slotName string) (string, error) {
if _, ok := this.Request.Intent.Slots[slotName]; ok {
return this.Request.Intent.Slots[slotName].Value, nil
}
return "", errors.New("Slot name not found.")
}
func (this *EchoRequest) AllSlots() map[string]EchoSlot {
return this.Request.Intent.Slots
}
// Response Functions
func NewEchoResponse() *EchoResponse {
er := &EchoResponse{
Version: "1.0",
Response: EchoRespBody{
ShouldEndSession: true,
},
SessionAttributes: make(map[string]interface{}),
}
return er
}
func (this *EchoResponse) OutputSpeech(text string) *EchoResponse {
this.Response.OutputSpeech = &EchoRespPayload{
Type: "PlainText",
Text: text,
}
return this
}
func (this *EchoResponse) Card(title string, content string) *EchoResponse {
return this.SimpleCard(title, content)
}
func (this *EchoResponse) OutputSpeechSSML(text string) *EchoResponse {
this.Response.OutputSpeech = &EchoRespPayload{
Type: "SSML",
SSML: text,
}
return this
}
func (this *EchoResponse) SimpleCard(title string, content string) *EchoResponse {
this.Response.Card = &EchoRespPayload{
Type: "Simple",
Title: title,
Content: content,
}
return this
}
func (this *EchoResponse) StandardCard(title string, content string, smallImg string, largeImg string) *EchoResponse {
this.Response.Card = &EchoRespPayload{
Type: "Standard",
Title: title,
Content: content,
}
if smallImg != "" {
this.Response.Card.Image.SmallImageURL = smallImg
}
if largeImg != "" {
this.Response.Card.Image.LargeImageURL = largeImg
}
return this
}
func (this *EchoResponse) LinkAccountCard() *EchoResponse {
this.Response.Card = &EchoRespPayload{
Type: "LinkAccount",
}
return this
}
func (this *EchoResponse) Reprompt(text string) *EchoResponse {
this.Response.Reprompt = &EchoReprompt{
OutputSpeech: EchoRespPayload{
Type: "PlainText",
Text: text,
},
}
return this
}
func (this *EchoResponse) RepromptSSML(text string) *EchoResponse {
this.Response.Reprompt = &EchoReprompt{
OutputSpeech: EchoRespPayload{
Type: "SSML",
Text: text,
},
}
return this
}
func (this *EchoResponse) EndSession(flag bool) *EchoResponse {
this.Response.ShouldEndSession = flag
return this
}
func (this *EchoResponse) String() ([]byte, error) {
jsonStr, err := json.Marshal(this)
if err != nil {
return nil, err
}
return jsonStr, nil
}
// Request Types
type EchoRequest struct {
Version string `json:"version"`
Session EchoSession `json:"session"`
Request EchoReqBody `json:"request"`
Context EchoContext `json:"context"`
}
type EchoSession struct {
New bool `json:"new"`
SessionID string `json:"sessionId"`
Application struct {
ApplicationID string `json:"applicationId"`
} `json:"application"`
Attributes map[string]interface{} `json:"attributes"`
User struct {
UserID string `json:"userId"`
AccessToken string `json:"accessToken,omitempty"`
} `json:"user"`
}
type EchoContext struct {
System struct {
Device struct {
DeviceId string `json:"deviceId,omitempty"`
} `json:"device,omitempty"`
Application struct {
ApplicationID string `json:"applicationId,omitempty"`
} `json:"application,omitempty"`
} `json:"System,omitempty"`
}
type EchoReqBody struct {
Type string `json:"type"`
RequestID string `json:"requestId"`
Timestamp string `json:"timestamp"`
Intent EchoIntent `json:"intent,omitempty"`
Reason string `json:"reason,omitempty"`
Token string `json:"token,omitempty"`
}
type EchoIntent struct {
Name string `json:"name"`
Slots map[string]EchoSlot `json:"slots"`
}
type EchoSlot struct {
Name string `json:"name"`
Value string `json:"value"`
}
// Response Types
type EchoResponse struct {
Version string `json:"version"`
SessionAttributes map[string]interface{} `json:"sessionAttributes,omitempty"`
Response EchoRespBody `json:"response"`
}
type EchoRespBody struct {
OutputSpeech *EchoRespPayload `json:"outputSpeech,omitempty"`
Card *EchoRespPayload `json:"card,omitempty"`
Reprompt *EchoReprompt `json:"reprompt,omitempty"` // Pointer so it's dropped if empty in JSON response.
Directives []interface{} `json:"directives,omitempty"`
ShouldEndSession bool `json:"shouldEndSession"`
}
type EchoReprompt struct {
OutputSpeech EchoRespPayload `json:"outputSpeech,omitempty"`
}
type EchoRespImage struct {
SmallImageURL string `json:"smallImageUrl,omitempty"`
LargeImageURL string `json:"largeImageUrl,omitempty"`
}
type EchoRespPayload struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
SSML string `json:"ssml,omitempty"`
Content string `json:"content,omitempty"`
Image EchoRespImage `json:"image,omitempty"`
}
type EchoDirective struct {
Type string `json:"type,omitempty"`
PlayBehavior string `json:"playBehavior,omitempty"`
ClearBehavior string `json:"clearBehavior,omitempty"`
AudioItem *EchoAudioItem `json:"audioItem,omitempty"`
}
type EchoAudioItem struct {
Stream EchoStream `json:"stream,omitempty"`
}
type EchoStream struct {
Url string `json:"url,omitempty"`
Token string `json:"token,omitempty"`
ExpectedPreviousToken string `json:"expectedPreviousToken,omitempty"`
OffsetInMilliseconds int64 `json:"offsetInMilliseconds"`
}