-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.js
More file actions
164 lines (136 loc) · 3.73 KB
/
Handler.js
File metadata and controls
164 lines (136 loc) · 3.73 KB
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
/**
* Handler class that checks whether an intent can handle a request.
*
* Create an instance of the class, chain the necessary checks, then call
* canHandle() to perform validation.
*/
class Handler {
constructor(handlerInput) {
const { request } = handlerInput.requestEnvelope;
const attributes = handlerInput.attributesManager.getSessionAttributes();
const {
supportedInterfaces
} = handlerInput.requestEnvelope.context.System.device;
this.request = request;
this.attributes = attributes;
this.supportedInterfaces = supportedInterfaces;
// Default to true, if at any point the handler fails, set to false.
this.valid = [true];
this.branch = 0;
}
setValid(valid) {
if (!valid) {
this.valid[this.branch] = false;
}
return this;
}
or() {
this.branch++;
// Default to true for new or branch.
this.valid[this.branch] = true;
return this;
}
canHandle() {
return this.valid.includes(true);
}
/**
* Checks where the request is of a certain type.
*
* E.g. 'IntentRequest'
*/
isType(type) {
const valid = this.request.type === type;
return this.setValid(valid);
}
/**
* Check whether a request contains a certain intent.
*
* Passed intent can be a single intent, or an array.
* If the intent matches any in the array, it passes validation.
*/
isIntent(intents) {
if (!intents.constructor === Array) {
intents = [intents];
}
const valid =
this.request.type === "IntentRequest" &&
intents.includes(this.request.intent.name);
return this.setValid(valid);
}
/**
* Checks if the request is of type 'Display.ElementSelected',
* and the passed token matches that of the request.
*
* Tokens take the format of tokenName-usersChoice
*/
isListSelect(token) {
const valid =
this.request.type === "Display.ElementSelected" &&
this.request.token &&
this.request.token.split("-")[0] === token;
return this.setValid(valid);
}
/**
* Checks whether the request contains all of the
* given attributes.
*/
hasAttributes(attributes) {
for (let attribute of attributes) {
if (!this.attributes.hasOwnProperty(attribute)) {
return this.setValid(false);
}
}
return this;
}
/**
* Checks whether the request has the given slots, and all
* slots have a value.
*/
hasSlots(slots) {
const { intent } = this.request;
if (!intent || !intent.slots) {
return this.setValid(false);
}
for (let slot of slots) {
if (!intent.slots[slot]) {
return this.setValid(false);
} else if (!intent.slots[slot].value) {
return this.setValid(false);
}
}
return this;
}
/**
* Checks whether the alexa device being used, supports the given display(s).
*
* The display interfaces can be Display, AudioPlayer or VideoApp.
*/
doesSupport(displayInterfaces) {
if (displayInterfaces.constructor !== Array) {
displayInterfaces = [displayInterfaces];
}
for (let displayInterface of displayInterfaces) {
if (!(displayInterface in this.supportedInterfaces)) {
return this.setValid(false);
}
}
return this;
}
/**
* Checks whether the alexa device being used, does not support the given display(s).
*
* The display interfaces can be Display, AudioPlayer or VideoApp.
*/
doesNotSupport(displayInterfaces) {
if (displayInterfaces.constructor !== Array) {
displayInterfaces = [displayInterfaces];
}
for (let displayInterface of displayInterfaces) {
if (displayInterface in this.supportedInterfaces) {
return this.setValid(false);
}
}
return this;
}
}
module.exports = Handler;