-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
76 lines (72 loc) · 2.01 KB
/
index.js
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
'use strict';
exports.launchRequest = function (req) {
this.version = req.version;
this.sessionId = req.session.sessionId;
this.userId = req.session.user.userId || '';
this.types = 'LAUNCH_REQUEST';
};
exports.intentRequest = function (req) {
this.sessionId = req.session.sessionId;
this.attributes = req.session.attributes;
this.userId = req.session.user.userId || '';
this.intentName = req.request.intent.name;
this.slots = req.request.intent.slots;
this.types = 'INTENT_REQUEST';
};
exports.sessionEndedRequest = function (req) {
this.sessionId = req.session.new ? req.session.sessionId : this.sessionId;
this.attributes = req.session.attributes;
this.userId = req.session.user.userId || '';
this.endReason = req.request.reason;
this.types = 'SESSION_ENDED_REQUEST';
};
exports.response = function (speech, card, endSession, callback) {
if(this.types === 'SESSION_ENDED_REQUEST') {
callback = speech;
return callback(null, {version: this.version});
}
if(!speech) {
return callback('Missing speech text.');
}
if(typeof(speech) === 'number') {
speech = speech.toString();
}
if(typeof(speech) !== 'string') {
return callback('Invalid speech text.');
}
if(!card) {
return callback('Missing card data.');
}
if(typeof(card) !== 'object') {
return callback('Invalid card object.');
}
if(!card.title) {
return callback('Missing card title.');
}
if(!card.subtitle) {
return callback('Missing card subtitle');
}
if(!card.content) {
return callback('Missing card content');
}
if(typeof(endSession) !== 'boolean') {
return callback('Invalid end session value.');
}
return callback(null, {
version: this.version,
sessionAttributes: this.attributes || {},
response: {
outputSpeech: {
type: 'PlainText',
text: speech
},
card: {
type: 'Simple',
title: card.title,
subtitle: card.subtitle,
content: card.content
},
shouldEndSession: endSession
}
});
};