-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
executable file
·119 lines (111 loc) · 3.94 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
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
'use strict';
const ALEXA_APP_ID = 'amzn1.ask.skill.app.your-skill-id';
const APIAI_DEVELOPER_ACCESS_TOKEN = 'your-apiai-developer-access-token';
var AlexaSdk = require('alexa-sdk');
var ApiAiSdk = require('apiai');
var ApiAi = ApiAiSdk(APIAI_DEVELOPER_ACCESS_TOKEN);
var alexaSessionId;
exports.handler = function (event, context) {
var alexa = AlexaSdk.handler(event, context);
alexa.appId = ALEXA_APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest': function () {
var self = this;
setAlexaSessionId(self.event.session.sessionId);
ApiAi.eventRequest({name: 'WELCOME'}, {sessionId: alexaSessionId})
.on('response', function (response) {
var speech = response.result.fulfillment.speech;
self.emit(':ask', speech, speech);
})
.on('error', function (error) {
console.error(error.message);
self.emit(':tell', error);
})
.end();
},
'ApiIntent': function () {
var self = this;
var text = self.event.request.intent.slots.Text.value;
setAlexaSessionId(self.event.session.sessionId);
if (text) {
ApiAi.textRequest(text, {sessionId: alexaSessionId})
.on('response', function (response) {
var speech = response.result.fulfillment.speech;
if (isResponseIncompleted(response)) {
self.emit(':ask', speech, speech);
} else {
self.emit(':tell', speech);
}
})
.on('error', function (error) {
console.error(error.message);
self.emit(':tell', error.message);
})
.end();
} else {
self.emit('Unhandled');
}
},
'AMAZON.CancelIntent': function () {
this.emit('AMAZON.StopIntent');
},
'AMAZON.HelpIntent': function () {
var self = this;
ApiAi.eventRequest({name: 'HELP'}, {sessionId: alexaSessionId})
.on('response', function (response) {
var speech = response.result.fulfillment.speech;
self.emit(':ask', speech, speech);
})
.on('error', function (error) {
console.error(error.message);
self.emit(':tell', error.message);
})
.end();
},
'AMAZON.StopIntent': function () {
var self = this;
ApiAi.eventRequest({name: 'BYE'}, {sessionId: alexaSessionId})
.on('response', function (response) {
self.emit(':tell', response.result.fulfillment.speech);
})
.on('error', function (error) {
console.error(error.message);
self.emit(':tell', error.message);
})
.end();
},
'Unhandled': function () {
var self = this;
ApiAi.eventRequest({name: 'FALLBACK'}, {sessionId: alexaSessionId})
.on('response', function (response) {
var speech = response.result.fulfillment.speech;
self.emit(':ask', speech, speech);
})
.on('error', function (error) {
console.error(error.message);
self.emit(':tell', error.message);
})
.end();
}
};
function isResponseIncompleted(response) {
if (response.result.actionIncomplete) {
return true;
}
for (var i = 0; i < response.result.contexts.length; i++) {
if (response.result.contexts[i].lifespan > 1) {
return true;
}
}
return false;
}
function setAlexaSessionId(sessionId) {
if (sessionId.indexOf("amzn1.echo-api.session.") != -1) {
alexaSessionId = sessionId.split('amzn1.echo-api.session.').pop();
} else {
alexaSessionId = sessionId.split('SessionId.').pop();
}
}