-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolexa.go
188 lines (165 loc) · 5.22 KB
/
golexa.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
package golexa
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
//AlexaRequest defines the json passed from Alexa when the skill is called
type AlexaRequest struct {
Version string `json:"version"`
Session *Session `json:"session"`
Request *Request `json:"request"`
}
//AlexaResponse defines the json passed to Alexa
type AlexaResponse struct {
Version string `json:"version"`
SessionAttributes map[string]string `json:"sessionAttributes,omitempty"`
Response *Response `json:"response"`
}
//Session contains the session data of the request
type Session struct {
New bool `json:"new"`
SessionID string `json:"sessionId"`
Attributes struct {
String map[string]interface{} `json:"string"`
} `json:"attributes"`
User struct {
UserID string `json:"userId"`
AccessToken string `json:"accessToken"`
} `json:"user"`
Application struct {
ApplicationID string `json:"applicationId"`
} `json:"application"`
}
//Request contains main data of the AlexaRequest
type Request struct {
Locale string `json:"locale"`
Type string `json:"type"`
Time string `json:"timestamp"`
RequestID string `json:"requestId"`
DialogState string `json:"dialogState"`
Intent Intent `json:"intent"`
Name string `json:"name"`
}
//Response contains main data of the AlexaResponse
type Response struct {
OutputSpeech *OutputSpeech `json:"outputSpeech,omitempty"`
Reprompt *Reprompt `json:"reprompt,omitempty"`
Card *Card `json:"card,omitempty"`
Directives []interface{} `json:"directives,omitempty"`
ShouldEndSession bool `json:"shouldEndSession,omitempty"`
}
//Intent contained the intent detected by Alexa
type Intent struct {
Name string `json:"name"`
ConfirmationStatus string `json:"confirmationstatus"`
Slots map[string]IntentSlot `json:"slots"`
}
//OutputSpeech contains what Alexa says to the user
type OutputSpeech struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
SSML string `json:"ssml,omitempty"`
}
//Reprompt containes what Alexa asks to the user
type Reprompt struct {
OutputSpeech OutputSpeech `json:"outputSpeech"`
}
//IntentSlot contains an Alexa Slot
type IntentSlot struct {
Name string `json:"name"`
ConfirmationStatus string `json:"confirmationStatus,omitempty"`
Value string `json:"value"`
Resolutions *Resolutions `json:"resolutions,omitempty"`
}
//Resolutions contain extra properties of a slot
type Resolutions struct {
ResolutionsPerAuthority []struct {
Authority string `json:"authority"`
Status struct {
Code string `json:"code"`
} `json:"status"`
Values []struct {
Value struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"value"`
} `json:"values"`
} `json:"resolutionsPerAuthority"`
}
//Card contains data displayed by Alexa
type Card struct {
Type string `json:"type"`
Title string `json:"title,omitempty"`
Content string `json:"content,omitempty"`
Text string `json:"text,omitempty"`
Image struct {
SmallImageURL string `json:"smallImageUrl,omitempty"`
LargeImageURL string `json:"largeImageUrl,omitempty"`
} `json:"image,omitempty"`
}
//Triggerable contains the definition of the functions triggered by Handle()
type Triggerable interface {
OnLaunch(ctx context.Context, req AlexaRequest, resp *AlexaResponse) error
OnIntent(ctx context.Context, req AlexaRequest, resp *AlexaResponse) error
}
//Golexa is the container that implements the triggerable functions
type Golexa struct {
Triggerable Triggerable
AlexaRequest *AlexaRequest
TranslationMap map[string]map[string]string
}
//Handle triggers the triggerable function by looking at the request
func (golexa *Golexa) Handle(ctx context.Context, req *AlexaRequest) (*AlexaResponse, error) {
golexa.AlexaRequest = req
resp := CreateResponse()
switch req.Request.Type {
case "LaunchRequest":
err := golexa.Triggerable.OnLaunch(ctx, *req, resp)
if err != nil {
fmt.Println("Error on launch: " + err.Error())
}
return resp, err
case "IntentRequest":
err := golexa.Triggerable.OnIntent(ctx, *req, resp)
if err != nil {
fmt.Println("Error on launch: " + err.Error())
}
return resp, err
default:
return resp, errors.New("Request type not recognized")
}
}
func (golexa *Golexa) Translate(key string) string {
language := golexa.AlexaRequest.Request.Locale
return golexa.TranslationMap[language][key]
}
//CreateResponse initializes the Alexa Response
func CreateResponse() *AlexaResponse {
var resp AlexaResponse
resp.Version = "2.0"
resp.Response = &Response{}
return &resp
}
//Say makes Alexa say something to the user
func (resp *AlexaResponse) Say(text string) {
resp.Response.OutputSpeech = &OutputSpeech{
Type: "PlainText",
Text: text,
}
}
//Ask makes Alexa ask something to the user
func (resp *AlexaResponse) Ask(text string) {
resp.Response.Reprompt = &Reprompt{
OutputSpeech: OutputSpeech{
Type: "PlainText",
Text: text,
},
}
resp.Response.ShouldEndSession = false
}
//LambdaStart passes the function to be triggered to lambda
(golexa *Golexa) LambdaStart() {
lambda.Start(golexa.Handle)
}