-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
118 lines (104 loc) · 2.96 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
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
package alexa
import (
"errors"
)
var (
ErrNoSlotFound = errors.New("No slot found")
)
// Request represents the payload object from Alexa.
type Request struct {
Context struct {
AudioPlayer struct {
OffsetInMilliseconds int `json:"offsetInMilliseconds"`
PlayerActivity string `json:"playerActivity"`
Token string `json:"token"`
} `json:"AudioPlayer"`
System struct {
APIAccessToken string `json:"apiAccessToken"`
APIEndpoint string `json:"apiEndpoint"`
Application struct {
ApplicationID string `json:"applicationId"`
} `json:"application"`
Device struct {
DeviceID string `json:"deviceId"`
SupportedInterfaces struct {
AudioPlayer struct{} `json:"AudioPlayer"`
} `json:"supportedInterfaces"`
} `json:"device"`
User struct {
AccessToken string `json:"accessToken"`
Permissions struct {
ConsentToken string `json:"consentToken"`
} `json:"permissions"`
UserID string `json:"userId"`
} `json:"user"`
} `json:"System"`
} `json:"context"`
Session struct {
New bool `json:"new"`
SessionID string `json:"sessionId"`
Attributes struct {
} `json:"attributes"`
User struct {
AccessToken string `json:"accessToken"`
UserID string `json:"userId"`
} `json:"user"`
Application struct {
ApplicationID string `json:"applicationId"`
} `json:"application"`
} `json:"session"`
Request struct {
Locale string `json:"locale"`
Timestamp string `json:"timestamp"`
Type string `json:"type"`
RequestID string `json:"requestId"`
Intent *Intent `json:"intent"`
} `json:"request"`
Version string `json:"version"`
}
// Intent represents a intent object from Alexa.
type Intent struct {
Name string `json:"name"`
Slots map[string]*Slot `json:"slots"`
}
// Slot represents a slot object from Alexa.
type Slot struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ApplicationID returns the application id.
func (r *Request) ApplicationID() string {
return r.Context.System.Application.ApplicationID
}
// APIAccessToken returns the access token for Alexa API.
func (r *Request) APIAccessToken() string {
return r.Context.System.APIAccessToken
}
// APIEndpoint returns Alexa API endpoint.
func (r *Request) APIEndpoint() string {
return r.Context.System.APIEndpoint
}
// DeviceID returns the device id.
func (r *Request) DeviceID() string {
return r.Context.System.Device.DeviceID
}
// IntentName returns the intent name.
func (r *Request) IntentName() string {
return r.Request.Intent.Name
}
// SessionID returns the session id.
func (r *Request) SessionID() string {
return r.Session.SessionID
}
// Slot will find a slot by name if it exists or return a error.
func (r *Request) Slot(name string) (*Slot, error) {
slot, ok := r.Request.Intent.Slots[name]
if !ok {
return nil, ErrNoSlotFound
}
return slot, nil
}
// UserID returns the user id.
func (r *Request) UserID() string {
return r.Session.User.UserID
}