forked from arienmalec/alexa-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
66 lines (59 loc) · 1.97 KB
/
request.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
package alexa
import "strings"
// RequestType represents confirmvarious Request Types
type RequestType int
const (
// RequestTypeUndefined means incoming value incorrect or not supported
RequestTypeUndefined RequestType = iota
// RequestTypeLaunch is constant `LaunchRequest`
RequestTypeLaunch
// RequestTypeIntent is constant `IntentRequest`
RequestTypeIntent
// RequestTypeSessionEnded is constant `SessionEndedRequest`
RequestTypeSessionEnded
// RequestTypeCanFulfillIntent is constant `CanFulfillIntentRequest`
RequestTypeCanFulfillIntent
)
// requestTypeStrings for use outside this module
var requestTypeStrings = [...]string{
"Undefined", // Placeholder - should never be this
"LaunchRequest",
"IntentRequest",
"SessionEndedRequest",
"CanFulfillIntentRequest",
}
// Request is an Alexa skill request
// see https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#request-format
type Request struct {
Version string `json:"version"`
Session Session `json:"session"`
Context Context `json:"context"`
Body struct {
Type RequestType `json:"type"`
RequestID string `json:"requestId"`
Timestamp string `json:"timestamp"`
Locale string `json:"locale"`
Intent Intent `json:"intent,omitempty"`
Reason string `json:"reason,omitempty"`
DialogState string `json:"dialogState,omitempty"`
} `json:"request"`
}
// MarshalJSON Function to handle JSON parsing out
func (r RequestType) MarshalJSON() ([]byte, error) {
j := string(`"` + requestTypeStrings[r] + `"`)
return []byte(j), nil
}
// UnmarshalJSON Function to handle JSON parsing out
func (r *RequestType) UnmarshalJSON(data []byte) error {
rt := RequestTypeUndefined
// Convert to string whilst removing quotes
x := string(data)[1 : len(data)-1]
// Find the type in the range of values
for i, s := range requestTypeStrings {
if strings.ToLower(s) == strings.ToLower(x) {
rt = RequestType(i)
}
}
*r = rt
return nil
}