-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (147 loc) · 4.97 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
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
package main
import (
"net/http"
"os"
"time"
"github.com/kpango/glg"
"github.com/rking788/go-alexa/skillserver"
"github.com/rking788/twitch-box/alexa"
"github.com/rking788/twitch-box/twitch"
)
// AlexaHandler is the type of function that should be used to respond to a specific intent.
type AlexaHandler func(*skillserver.EchoRequest) *skillserver.EchoResponse
// AlexaHandlers are the handler functions mapped by the intent name that they should handle.
var (
AlexaHandlers = map[string]AlexaHandler{
"StartAudioStream": alexa.StartAudioStream,
"StartVideoStream": alexa.StartVideoStream,
"AMAZON.NextIntent": alexa.StartAudioStream,
"AMAZON.PreviousIntent": alexa.StartAudioStream,
"AMAZON.ResumeIntent": alexa.StartAudioStream,
}
)
// Applications is a definition of the Alexa applications running on this server.
var applications map[string]interface{}
const (
FATAL uint = iota
ERROR
WARNING
INFO
DEBUG
ALL
)
// InitEnv is responsible for initializing all components (including sub-packages) that
// depend on a specific deployment environment configuration.
func InitEnv() {
applications = map[string]interface{}{
"/echo/twitch-box": skillserver.EchoApplication{ // Route
AppID: os.Getenv("ALEXA_APP_ID"), // Echo App ID from Amazon Dashboard
OnIntent: EchoIntentHandler,
OnLaunch: EchoIntentHandler,
OnSessionEnded: EchoSessionEndedHandler,
},
"/health": skillserver.StdApplication{
Methods: "GET",
Handler: healthHandler,
},
}
// Configure logging
logger := glg.Get()
level, ok := map[string]uint{"FATAL": FATAL, "ERROR": ERROR,
"WARNING": WARNING, "INFO": INFO, "DEBUG": DEBUG,
"ALL": ALL}[os.Getenv("TWITCH_BOX_LOG_LEVEL")]
if !ok {
level = WARNING
}
if level < DEBUG {
logger.SetLevelMode(glg.DEBG, glg.NONE)
}
if level < INFO {
logger.SetLevelMode(glg.INFO, glg.NONE)
}
if level < WARNING {
logger.SetLevelMode(glg.WARN, glg.NONE)
}
if level < ERROR {
logger.SetLevelMode(glg.ERR, glg.NONE)
}
}
func main() {
// flag.Parse()
// config = loadConfig(configPath)
// glg.Infof("Loaded config : %+v\n", config)
twitch.InitEnv(os.Getenv("REDIS_URL"))
InitEnv()
// defer CloseLogger()
glg.Printf("Version=%s, BuildDate=%v", Version, BuildDate)
// writeHeapProfile()
// if config.Environment == "production" {
// port := ":443"
// err := skillserver.RunSSL(applications, port, config.SSLCertPath, config.SSLKeyPath)
// if err != nil {
// glg.Errorf("Error starting the application! : %s", err.Error())
// }
// } else {
// Heroku makes us read a random port from the environment and our app is a
// subdomain of theirs so we get SSL for free
port := os.Getenv("PORT")
skillserver.Run(applications, port)
//}
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Up"))
}
// Alexa skill related functions
// EchoSessionEndedHandler is responsible for cleaning up an open session since the
// user has quit the session.
func EchoSessionEndedHandler(echoRequest *skillserver.EchoRequest,
echoResponse *skillserver.EchoResponse) {
*echoResponse = *skillserver.NewEchoResponse()
//alexa.ClearSession(echoRequest.GetSessionID())
}
// EchoIntentHandler is a handler method that is responsible for receiving the
// call from a Alexa command and returning the correct speech or cards.
func EchoIntentHandler(echoRequest *skillserver.EchoRequest, echoResponse *skillserver.EchoResponse) {
// Time the intent handler to determine if it is taking longer than normal
startTime := time.Now()
defer func(start time.Time) {
glg.Infof("IntentHandler execution time: %v", time.Since(start))
}(startTime)
var response *skillserver.EchoResponse
intentName := echoRequest.GetIntentName()
glg.Infof("RequestType: %s, IntentName: %s", echoRequest.GetRequestType(), intentName)
// During this time, users can invoke the following built-in playback control intents without using your skill’s invocation name:
// AMAZON.CancelIntent
// AMAZON.LoopOffIntent
// AMAZON.LoopOnIntent
// AMAZON.RepeatIntent
// AMAZON.ResumeIntent
// AMAZON.ShuffleOffIntent
// AMAZON.ShuffleOnIntent
// AMAZON.StartOverIntent
handler, ok := AlexaHandlers[intentName]
if echoRequest.GetRequestType() == "LaunchRequest" {
response = alexa.WelcomePrompt(echoRequest)
} else if intentName == "AMAZON.StopIntent" {
response = skillserver.NewEchoResponse()
} else if intentName == "AMAZON.CancelIntent" {
response = skillserver.NewEchoResponse()
} else if intentName == "AMAZON.PauseIntent" {
// Send stop directive
response = alexa.StopAudioDirective()
} else if ok {
response = handler(echoRequest)
} else {
response = skillserver.NewEchoResponse()
response.OutputSpeech("Sorry Guardian, I did not understand your request.")
}
*echoResponse = *response
}
// func dumpRequest(ctx *gin.Context) {
// data, err := httputil.DumpRequest(ctx.Request, true)
// if err != nil {
// glg.Errorf("Failed to dump the request: %s", err.Error())
// return
// }
// glg.Debug(string(data))
// }