generated from rodellison/alexa-slick-dealer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
104 lines (90 loc) · 3.37 KB
/
main.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
package main
import (
"encoding/json"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rodellison/gomusicman/alexa"
"github.com/rodellison/gomusicman/handlers"
"github.com/rodellison/gomusicman/models"
"os"
)
var (
//These var definitions to help with Mock testing
StopCancelHandler, HelpHandler, LaunchHandler, NoHandler func(alexa.Request) alexa.Response
ArtistHandler, VenueHandler, LocationHandler func(alexa.Request, bool, models.SessionData) alexa.Response
)
func init() {
//Assign the real handler functions here, but override when testing..
StopCancelHandler = handlers.HandleStopIntent
HelpHandler = handlers.HandleHelpIntent
LaunchHandler = handlers.HandleLaunchIntent
ArtistHandler = handlers.HandleArtistIntent
VenueHandler = handlers.HandleVenueIntent
LocationHandler = handlers.HandleLocationIntent
NoHandler = handlers.HandleNoIntent
}
//Centralized function to steer incoming alexa requests to the appropriate handler function
func IntentDispatcher(request alexa.Request) alexa.Response {
var response alexa.Response
var sessionData models.SessionData
if request.Body.Type == "LaunchRequest" {
return LaunchHandler(request)
}
switch request.Body.Intent.Name {
case "ArtistIntent":
response = ArtistHandler(request, false, sessionData)
case "VenueIntent":
response = VenueHandler(request, false, sessionData)
case "LocationIntent":
response = LocationHandler(request, false, sessionData)
case alexa.YesIntent:
incomingSessionAttrs := request.Session.Attributes
incomingData, _ := json.Marshal(incomingSessionAttrs["dataToSave"])
json.Unmarshal(incomingData, &sessionData)
if len(sessionData.Eventdata) == 0 {
response = HelpHandler(request)
} else {
//because we've unmashalled the session data already, rather than do it again inside the respective handlers,
//just pass it along as a parm.
switch sessionData.Intent {
case "ArtistIntent":
response = ArtistHandler(request, true, sessionData)
case "VenueIntent":
response = VenueHandler(request, true, sessionData)
case "LocationIntent":
response = LocationHandler(request, true, sessionData)
}
}
case alexa.NoIntent:
response = NoHandler(request)
case alexa.StopIntent:
response = StopCancelHandler(request)
case alexa.CancelIntent:
response = StopCancelHandler(request)
case alexa.FallbackIntent:
response = HelpHandler(request)
case alexa.HelpIntent:
response = HelpHandler(request)
}
return response
}
//handler() is the first call from the lambda handler, first checking if the caller is coming from an expected 'Alexa Skill ARN.
//if so, proceed, if not - send not auth response
func Handler(request alexa.Request) (alexa.Response, error) {
//Ensure this lambda function/code is invoked through the associated Alexa Skill, and not called directly
if request.Session.Application.ApplicationID != os.Getenv("AppARN") {
var primarybuilder alexa.SSMLBuilder
primarybuilder.Say("Sorry, not authorized. Please enable and use this skill through an approved Alexa device.")
sessAttrData := make(map[string]interface{})
return alexa.NewSimpleTellResponse("Not authorized",
primarybuilder.Build(),
"Not authorized, Please enable and use this skill through an approved Alexa device.",
true,
&sessAttrData), nil
} else {
return IntentDispatcher(request), nil
}
}
//main() is the entry point for the lambda handler
func main() {
lambda.Start(Handler)
}