-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
response.go
176 lines (149 loc) · 5.42 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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2023, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package alexa
import (
"encoding/json"
)
// NewEchoResponse will construct a new response instance with the required metadata and an empty speech string.
// By default the response will indicate that the session should be ended. Use the `EndSession(bool)` method if the
// session should be left open.
func NewResponse() *Response {
er := &Response{
Version: "1.0",
Response: RespBody{
ShouldEndSession: true,
},
SessionAttributes: make(map[string]interface{}),
}
return er
}
// OutputSpeech will replace any existing text that should be spoken with this new value. If the output
// needs to be constructed in steps or special speech tags need to be used, see the `SSMLTextBuilder`.
func (r *Response) OutputSpeech(text string) *Response {
r.Response.OutputSpeech = &RespPayload{
Type: "PlainText",
Text: text,
}
return r
}
// Card will add a card to the Server app's response with the provided title and content strings.
func (r *Response) Card(title string, content string) *Response {
return r.SimpleCard(title, content)
}
// OutputSpeechSSML will add the text string provided and indicate the speech type is SSML in the response.
// This should only be used if the text to speech string includes special SSML tags.
func (r *Response) OutputSpeechSSML(text string) *Response {
r.Response.OutputSpeech = &RespPayload{
Type: "SSML",
SSML: text,
}
return r
}
// SimpleCard will indicate that a card should be included in the Server companion app as part of the response.
// The card will be shown with the provided title and content.
func (r *Response) SimpleCard(title string, content string) *Response {
r.Response.Card = &RespPayload{
Type: "Simple",
Title: title,
Content: content,
}
return r
}
// StandardCard will indicate that a card should be shown in the Server companion app as part of the response.
// The card shown will include the provided title and content as well as images loaded from the locations provided
// as remote locations.
func (r *Response) StandardCard(title string, content string, smallImg string, largeImg string) *Response {
r.Response.Card = &RespPayload{
Type: "Standard",
Title: title,
Content: content,
}
if smallImg != "" {
r.Response.Card.Image.SmallImageURL = smallImg
}
if largeImg != "" {
r.Response.Card.Image.LargeImageURL = largeImg
}
return r
}
// LinkAccountCard is used to indicate that account linking still needs to be completed to continue
// using the Server skill. This will force an account linking card to be shown in the user's companion app.
func (r *Response) LinkAccountCard() *Response {
r.Response.Card = &RespPayload{
Type: "LinkAccount",
}
return r
}
// Reprompt will send a prompt back to the user, this could be used to request additional information from the user.
func (r *Response) Reprompt(text string) *Response {
r.Response.Reprompt = &Reprompt{
OutputSpeech: RespPayload{
Type: "PlainText",
Text: text,
},
}
return r
}
// RepromptSSML is similar to the `Reprompt` method but should be used when the prompt
// to the user should include special speech tags.
func (r *Response) RepromptSSML(text string) *Response {
r.Response.Reprompt = &Reprompt{
OutputSpeech: RespPayload{
Type: "SSML",
Text: text,
},
}
return r
}
// EndSession is a convenience method for setting the flag in the response that will
// indicate if the session between the end user's device and the skillserver should be closed.
func (r *Response) EndSession(flag bool) *Response {
r.Response.ShouldEndSession = flag
return r
}
// RespondToIntent is used to Delegate/Elicit/Confirm a dialog or an entire intent with
// user of server. The func takes in name of the dialog, updated intent/intent to confirm
// if any and optional slot value. It prepares a Echo Response to be returned.
// Multiple directives can be returned by calling the method in chain
// (eg. RespondToIntent(...).RespondToIntent(...), each RespondToIntent call appends the
// data to Directives array and will return the same at the end.
func (r *Response) RespondToIntent(name DialogType, intent *Intent, slot *Slot) *Response {
directive := Directive{Type: name}
if intent != nil && name == ConfirmIntent {
directive.IntentToConfirm = intent.Name
} else {
directive.UpdatedIntent = intent
}
if slot != nil {
if name == ElicitSlot {
directive.SlotToElicit = slot.Name
} else if name == ConfirmSlot {
directive.SlotToConfirm = slot.Name
}
}
r.Response.Directives = append(r.Response.Directives, &directive)
return r
}
// String ...
func (r *Response) String() ([]byte, error) {
jsonStr, err := json.Marshal(r)
if err != nil {
return nil, err
}
return jsonStr, nil
}