This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
211 lines (185 loc) · 8.54 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- coding: utf-8 -*- */
/*
Copyright 2016-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in
compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific
language governing permissions and limitations under the License.
*/
/*
The Alexa ASK Intent Validator is designed for medium-complex Intent Schema validation. Need to quickly
try many different combinations of your utterances ON your devices, this is the tool for you.
Currently supports English, German, French, Italian and Spanish.
**/
'use strict';
// Use the new Alexa SDK
const Alexa = require('alexa-sdk');
// UPDATEME: Does your skill use Dialog Directives? If so, update this to true.
const DIALOG_DIRECTIVE_SUPPORT = true;
// Series of strings for language tokenization
const LANGUAGE_STRINGS = {
'en-US': {
'launchRequestResponse': 'Launch Request for dialog mode, the session will remain open until you say, exit',
'exit': 'Goodbye.',
'received_with': ' received with ',
'slot': ' slot. ',
'slots': ' slots. ',
'still_listening': "I'm still listening, Please try another intent or say, stop",
'received_slots_are': 'Received slots are ',
'card_title': 'Reflected Intent'
},
'en-GB': {
'launchRequestResponse': 'Launch Request for dialog mode, the session will remain open until you say, exit',
'exit': 'Goodbye.',
'received_with': ' received with ',
'slot': ' slot. ',
'slots': ' slots. ',
'still_listening': "I'm still listening, Please try another intent or say, stop",
'received_slots_are': 'Received slots are ',
'card_title': 'Reflected Intent'
},
'en-CA': {
'launchRequestResponse': 'Launch Request for dialog mode, the session will remain open until you say, exit',
'exit': 'Goodbye.',
'received_with': ' received with ',
'slot': ' slot. ',
'slots': ' slots. ',
'still_listening': "I'm still listening, Please try another intent or say, stop",
'received_slots_are': 'Received slots are ',
'card_title': 'Reflected Intent'
},
'en-IN': {
'launchRequestResponse': 'Launch Request for dialog mode, the session will remain open until you say, exit',
'exit': 'Goodbye.',
'received_with': ' received with ',
'slot': ' slot. ',
'slots': ' slots. ',
'still_listening': "I'm still listening, Please try another intent or say, stop",
'received_slots_are': 'Received slots are ',
'card_title': 'Reflected Intent'
},
'de-DE': {
'launchRequestResponse': 'Launch Request. Gib den nächsten Befehl oder sage Abbruch',
'exit': 'Auf Wiedersehen.',
'received_with': ' empfangen mit ',
'slot': ' Slot. ',
'slots': ' Slots. ',
'still_listening': "Ich lausche noch immer. Bitte gebe einen neuen Befehl oder sage Stop.",
'received_slots_are': 'Empfangene Slots sind ',
'card_title': 'Reflektierte Absicht'
},
'fr-FR': {
'launchRequestResponse': "On passe en mode dialogue, la session restera ouverte jusqu'à ce que vous disiez, arrête",
'exit': 'Au revoir.',
'received_with': ' reçu avec ',
'slot': ' slotte. ',
'slots': ' slottes. ',
'still_listening': "J'écoute encore, Veuillez réessayer ou dites, arrête",
'received_slots_are': 'Les slottes reçus sont ',
'card_title': 'Intent '
},
'es-ES': {
'launchRequestResponse': "Petición de Lanzamiento. Pasando a modo diálogo. La sesión permanecerá abierta hasta que digas para.",
'exit': 'Adiós.',
'received_with': ' recibido con ',
'slot': ' parametro. ',
'slots': ' parametros. ',
'still_listening': "Sigo aquí. Qué quieres que repita?",
'received_slots_are': 'Los parametros recibidos son ',
'card_title': 'Intent '
},
'it-IT': {
'launchRequestResponse': "Richiesta di lancio. Passando alla modalità di dialogo, la sessione rimarrà aperta finché non dirai, esci",
'exit': 'Addio.',
'received_with': ' ricevuto con ',
'slot': ' parametro. ',
'slots': ' parametri. ',
'still_listening': "Sono ancora. Qui Cosa vuoi che ripeta?",
'received_slots_are': 'I parametri ricevuti sono ',
'card_title': 'Intent '
}
}
// The various handlers for interpreting the interaction model from the skill
const handlers = {
// Launch request - "open skillName" - keep the session open until the user requests to exit
'LaunchRequest': function () {
let LANGUAGE = LANGUAGE_STRINGS[this.event.request.locale] ? LANGUAGE_STRINGS[this.event.request.locale] : LANGUAGE_STRINGS['en-US'];
this.attributes['dialogSession'] = true;
this.emit(':ask', LANGUAGE.launchRequestResponse, LANGUAGE.launchRequestResponse);
},
// End the Session
'SessionEndedRequest': function () {
this.emit('AMAZON.StopIntent');
},
'AMAZON.StopIntent': function () {
let LANGUAGE = LANGUAGE_STRINGS[this.event.request.locale] ? LANGUAGE_STRINGS[this.event.request.locale] : LANGUAGE_STRINGS['en-US'];
this.emit(':tell', LANGUAGE.exit);
},
'AMAZON.ExitIntent': function () {
this.emit('AMAZON.StopIntent');
},
// The main handler - here we simply take the inbound Alexa request, parse out the intent and slots, then return back
// to the user, either as a dialog
'Unhandled': function () {
let request = this.event.request;
let intentInfo = parseIntentsAndSlotsFromEvent(request);
// If dialog directive support is enabled AND it exists and it is not in "completed" status, delegate back to the interaction model
if (DIALOG_DIRECTIVE_SUPPORT && request.dialogState && request.dialogState !== 'COMPLETED') {
this.emit(':delegate');
} else {
this.attributes['intentOutput'] = intentInfo.cardInfo;
let LANGUAGE = LANGUAGE_STRINGS[request.locale] ? LANGUAGE_STRINGS[request.locale] : LANGUAGE_STRINGS['en-US'];
// Determine if we are going to end the session or keep it in dialog mode. When used in dialog mode we "ask"
// as we are expecting another question to come through. When used in OneShot mode we "tell" and end the session.
if (this.attributes['dialogSession']) {
this.emit(':askWithCard', intentInfo.response, LANGUAGE.still_listening, LANGUAGE.card_title, intentInfo.cardInfo);
} else {
this.emit(':tellWithCard', intentInfo.response, LANGUAGE.card_title, intentInfo.cardInfo);
}
}
}
};
/**
* Parses the collected event info from Alexa into a friendlier TTS response and
* creates a card response with Intent/Slot info
*/
function parseIntentsAndSlotsFromEvent(request) {
//Cleanse the request intent name
let intentName = request.intent.name.replace(/[^a-zA-Z0-9]/g, " ");
let numSlots = 0;
let slots = request.intent.slots;
let filledInSlots = {};
if (slots) {
for (let i in slots) {
//Check if there is a value in a given slot
if (slots[i].value) {
filledInSlots[slots[i].name] = slots[i].value;
numSlots++;
}
}
}
let LANGUAGE = LANGUAGE_STRINGS[request.locale] ? LANGUAGE_STRINGS[request.locale] : LANGUAGE_STRINGS['en-US'];
let responseText = `${intentName} ${LANGUAGE.received_with} ${numSlots} ${LANGUAGE.slots}`;
let cardInfo = `${intentName} ${LANGUAGE.received_with} ${numSlots} ${LANGUAGE.slots}`;
if (filledInSlots > 0) {
responseText += LANGUAGE.received_slots_are;
}
for (let slotName in filledInSlots) {
responseText += ` ${slotName}, ${filledInSlots[slotName]}. `;
cardInfo += `\n${slotName}: ${filledInSlots[slotName]}`;
}
return {
response: responseText,
cardInfo: cardInfo
};
}
exports.handler = (event, context) => {
console.log("********** REQUEST **********");
console.log(JSON.stringify(event, null, 2));
//console.log(JSON.stringify(context, null, 2));
const alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};