-
Notifications
You must be signed in to change notification settings - Fork 1
/
ask.go
167 lines (138 loc) · 3.59 KB
/
ask.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
package ask
import (
"encoding/json"
"io"
)
type Session 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"`
} `json:"user"`
}
type Intent struct {
Name string `json:"name"`
Slots map[string]Slot `json:"slots"`
}
type Request struct {
Version string `json:"version"`
Session *Session `json:"session"`
Body struct {
Type string `json:"type"`
RequestID string `json:"requestId"`
Timestamp string `json:"timestamp"`
Intent Intent `json:"intent,omitempty"`
Reason string `json:"reason,omitempty"`
} `json:"request"`
}
type ResponseBody struct {
OutputSpeech *OutputSpeech `json:"outputSpeech,omitempty"`
Card *Card `json:"card,omitempty"`
Reprompt *Reprompt `json:"reprompt,omitempty"`
ShouldEndSession bool `json:"shouldEndSession"`
}
type Response struct {
Version string `json:"version"`
SessionAttributes map[string]interface{} `json:"sessionAttributes,omitempty"`
Body *ResponseBody `json:"response"`
}
type OutputSpeech struct {
Type string `json:"type"`
Text string `json:"text"`
}
type Card struct {
Type string `json:"type"`
Title string `json:"title,omitempty"`
Content string `json:"content,omitempty"`
}
type Slot struct {
Name string `json:"name"`
Value string `json:"value"`
}
type Reprompt struct {
OutputSpeech *OutputSpeech `json:"outputSpeech,omitempty"`
}
// Card methods
func NewCard(title string, content string) *Card {
return &Card{
// As of v1.0 of the ASK, "Simple" is the only supported Card type.
Type: "Simple",
Title: title,
Content: content,
}
}
// OutputSpeech methods
func NewOutputSpeech(text string) *OutputSpeech {
return &OutputSpeech{
// As of v1.0 of the ASK, "PlainText" is the only supported OutputSpeech type.
Type: "PlainText",
Text: text,
}
}
// Reprompt methods
func NewReprompt(text string) *Reprompt {
return &Reprompt{
OutputSpeech: NewOutputSpeech(text),
}
}
// Request methods
func NewRequestFromJSON(reader io.Reader) (*Request, error) {
var req *Request
if err := json.NewDecoder(reader).Decode(&req); err != nil {
return nil, err
}
return req, nil
}
func (r *Request) IsLaunchRequest() bool {
return r.Body.Type == "LaunchRequest"
}
func (r *Request) IsIntentRequest() bool {
return r.Body.Type == "IntentRequest"
}
func (r *Request) IsSessionEndedRequest() bool {
return r.Body.Type == "SessionEndedRequest"
}
func (r *Request) SessionTerminationWasUserInitiated() bool {
return r.Body.Reason == "USER_INITIATED"
}
func (r *Request) SessionTerminationIsDueToError() bool {
return r.Body.Reason == "ERROR"
}
func (r *Request) SessionTerminationIsDueToMaxRepromptLimitExceeded() bool {
return r.Body.Reason == "EXCEEDED_MAX_REPROMPTS"
}
func (r *Request) String() (string, error) {
bytes, err := json.Marshal(r)
if err != nil {
return "", err
}
return string(bytes), nil
}
// Response methods
func NewResponse(req *Request) *Response {
return &Response{
Version: "1.0",
SessionAttributes: req.Session.Attributes,
Body: &ResponseBody{
ShouldEndSession: true,
},
}
}
func (r *Response) String() (string, error) {
bytes, err := json.Marshal(r)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (r *Response) Bytes() ([]byte, error) {
bytes, err := json.Marshal(r)
if err != nil {
return []byte{}, err
}
return bytes, nil
}