-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlambda_go.go
87 lines (81 loc) · 2.37 KB
/
lambda_go.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
package main
import (
"encoding/json"
"github.com/jasonmoo/lambda_proc"
)
// Struct for the Intent Schema
type AlexaSkillEvent struct {
Session struct {
Sessionid string `json:"sessionId"`
Application struct {
Applicationid string `json:"applicationId"`
} `json:"application"`
Attributes interface{} `json:"attributes"`
User struct {
Userid string `json:"userId"`
Accesstoken interface{} `json:"accessToken"`
} `json:"user"`
New bool `json:"new"`
} `json:"session"`
Request struct {
Type string `json:"type"`
Requestid string `json:"requestId"`
Timestamp int64 `json:"timestamp"`
Intent struct {
Name string `json:"name"`
Slots struct {
Action struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"Action"`
Name struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"Name"`
} `json:"slots"`
} `json:"intent"`
Reason interface{} `json:"reason"`
} `json:"request"`
}
// Struct for the resonse to Alexa
type AlexaResponse struct {
Version string `json:"version,omitempty"`
Response struct {
Outputspeech struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"outputSpeech"`
Reprompt interface{} `json:"reprompt,omitempty"`
Shouldendsession bool `json:"shouldEndSession"`
} `json:"response"`
Sessionattributes struct {
} `json:"sessionAttributes,omitempty"`
}
func main() {
// Handle the incoming request from Alexa on Lambda from Node.js
lambda_proc.Run(func(context *lambda_proc.Context, eventJSON json.RawMessage) (interface{}, error) {
event := &AlexaSkillEvent{}
json.Unmarshal(eventJSON, event)
return processRequest(event), nil
})
}
func processRequest(event *AlexaSkillEvent) *AlexaResponse {
text := ""
switch event.Request.Intent.Name {
case "Command":
action := event.Request.Intent.Slots.Action.Value
name := event.Request.Intent.Slots.Name.Value
text = "You told " + name + " to " + action
default:
text = "I have no idea what you just said, could you speak more clearly already"
}
return generateAlexaResponse(text)
}
func generateAlexaResponse(text string) *AlexaResponse {
response := &AlexaResponse{}
response.Version = "1.0"
response.Response.Outputspeech.Type = "PlainText"
response.Response.Shouldendsession = true
response.Response.Outputspeech.Text = text
return response
}