-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
391 lines (366 loc) · 13.6 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const name = 'Devotional Raga';
const introPrompt = 'Here is todays re-mastered raga';
const data = [{
id: 1,
title: "Om Mantra",
description: "Om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 2,
title: "Buddha Om Mantra",
description: "Buddha om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_buddha.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 3,
title: "Ganesha Om Mantra",
description: "Ganesha om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_ganesha.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 4,
title: "Narayana Om Mantra",
description: "Narayana om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_narayana.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 5,
title: "Sai Om Mantra",
description: "Sai om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_sai.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 6,
title: "Durga Om Mantra",
description: "Durga om mantra remastered version",
album: "Devotional Raga",
url: "https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_durga.mp3",
image: "https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png"
},
{
id: 7,
title: 'Gayatri Om Mantra',
description: 'Gayatri om mantra remastered version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_gayatri.mp3'
}, {
id: 8,
title: 'Shiv Om Mantra',
description: 'Shiv mantra remastered version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_shiv.mp3'
}, {
id: 9,
title: 'Krishna Om Mantra',
description: 'Krishna om mantra remastered version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_krishna.mp3'
}, {
id: 10,
title: 'Buddha Tibetan Om Mantra',
description: 'Buddha tibetan om mantra remastered version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_tibetan_buddha.mp3'
},
{
id: 11,
title: 'Om Mantra Remix',
description: 'Om mantra remastered remix version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_remix.mp3'
},
{
id: 12,
title: 'Ganesha Om Mantra Remix',
description: 'Ganesha om mantra remastered remix version',
image: 'https://smartassistants.s3-eu-west-1.amazonaws.com/images/logo/devotion_raga_logo.png',
url: 'https://smartassistants.s3-eu-west-1.amazonaws.com/media/audio/devotional_raga/om_mantra_ganesha_remix.mp3'
}
];
// Configuration to let the player keep playing tracks:
const supportsMore = true;
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
// https://github.com/actions-on-google/actions-on-google-nodejs
const {
dialogflow,
Suggestions,
MediaObject,
Image
} = require('actions-on-google');
// Import the firebase-functions package for Cloud Functions for Firebase fulfillment.
const functions = require('firebase-functions');
// Node util module used for creating dynamic strings
const util = require('util');
// Instantiate the Dialogflow client with debug logging enabled.
const app = dialogflow({
debug: true
});
// Do common tasks for each intent invocation
app.middleware((conv, framework) => {
console.log(`Intent=${conv.intent}`);
console.log(`Type=${conv.input.type}`);
// Determine if the user input is by voice
conv.voice = conv.input.type === 'VOICE';
if (!(conv.intent === 'Default Fallback Intent' || conv.intent === 'No-input')) {
// Reset the fallback counter for error handling
conv.data.fallbackCount = 0;
}
});
// Prompts used for responding to the user.
// Each prompt type can have multiple alternatives. A prompt is selected
// randomly to make the conversation more natural.
const prompts = {
'welcome': [
`Welcome to ${name}.`,
`Hi! It's time for ${name}.`
],
'welcome_back': [
`Welcome back to ${name}.`,
`Hi again. Welcome back to ${name}.`
],
'intro': [
'Here we go.',
`Let's get started.`
],
'confirmation': [
'Sure.',
'OK.',
'Okay.',
'Sure thing.',
'Alright.'
],
'quit': [
'Bye for now. Hope to see you soon.',
'OK. Come back soon.',
`Okay, let's try this again later.`,
'OK. Hope to talk to you again soon.'
],
'no_input1': [
'Sorry, what was that?',
`Sorry, I didn't hear that.`,
`If you said something, I didn't hear it.`
],
'no_input2': [
`Sorry, I didn't catch that. Could you repeat yourself?`,
`If you're still there, say that again.`
],
'no_input3': [
`Okay let's try this again later.`,
'We can stop here. See you soon.'
],
'fallback1': [
`I didn't quite get that. What do you want to do?`,
`I didn't understand that. What do you want to do?`
],
'fallback2': [
`Hmmm. Since I'm still having trouble, I'll stop here. Let's play again soon.`,
`Since I'm still having trouble, I'll stop here. Try again in a few minutes.`,
`Since I'm still having trouble, I'll stop here. Bye for now.`
],
'help': [
'You can ask to repeat the last track, go to the next track, or quit. What do you want to do?',
'You can ask for the track to be repeated or you can ask for the next track. What do you want to do now?'
],
'repeat': [
'Here it is again: ',
'Let me repeat that: '
],
'error': [
'Oops! Something went wrong. Please try again later.'
],
'end': [
'Hope to see you soon.',
'Come back soon.',
`Let's try this again later.`,
'Hope to talk to you again soon.'
],
'next': [
`Next up: '%s' by '%s' from the album '%s'.`
]
};
// Overwrite the default intro prompts with configured prompts above.
if (introPrompt) {
if (Array.isArray(introPrompt)) {
prompts.intro = introPrompt;
} else {
prompts.intro = [introPrompt];
}
}
// Suggestion chips to let the user pick options on screens
// https://developers.google.com/actions/assistant/responses#suggestion_chip
const suggestions1 = new Suggestions('Next', 'Exit');
const suggestions2 = new Suggestions('Next', 'Previous', 'Exit');
const suggestions3 = new Suggestions('Exit');
// Utility to get a random item from an array
const getRandomItem = (array) => {
return array[Math.floor(Math.random() * (array.length))];
};
// Utility to get a random prompt without sequential repeats
const getRandomPrompt = (conv, prompt) => {
let availablePrompts = prompts[prompt];
// Select a new prompt by avoiding prompts used previously in the session
if (conv.data.prompts) {
if (typeof(conv.data.prompts[prompt]) !== 'undefined') {
availablePrompts = availablePrompts.filter(word => word !== conv.data.prompts[prompt]);
}
} else {
conv.data.prompts = {};
}
// Persist selected prompt in session storage
if (availablePrompts.length > 0) {
conv.data.prompts[prompt] = getRandomItem(availablePrompts);
} else {
conv.data.prompts[prompt] = prompts[prompt][0];
}
return conv.data.prompts[prompt];
};
// Select the next track to play from the data
const nextTrack = (conv, intro, backwards) => {
console.log(`nextTrack: ${conv.user.storage.track}`);
let track = data[0];
// Persist the selected track in user storage
// https://developers.google.com/actions/assistant/save-data#save_data_across_conversations
if (conv.user.storage.track) {
conv.user.storage.track = parseInt(conv.user.storage.track, 10);
if (backwards) {
conv.user.storage.track--;
} else {
conv.user.storage.track++;
}
if (conv.user.storage.track === 0) {
conv.user.storage.track = data.length;
} else if (conv.user.storage.track > data.length) {
// Loop the tracks
conv.user.storage.track = 1;
}
track = data[conv.user.storage.track - 1];
} else {
conv.user.storage.track = 1;
}
// Add a prompt intro
if (!intro) {
const nextPrompt = util.format(getRandomPrompt(conv, 'next'), track.title, track.description, track.album);
if (conv.voice) {
conv.ask(`<speak><prosody volume="silent">${nextPrompt}</prosody></speak>`);
} else {
conv.ask(nextPrompt);
}
}
// Create a media response
// https://developers.google.com/actions/assistant/responses#media_responses
conv.ask(new MediaObject({
name: track.title,
url: track.url,
description: track.description,
icon: new Image({
url: track.image,
alt: 'Media icon'
})
}));
// Add suggestions to continue the conversation
if (supportsMore) {
// Set the context to allow matching agent intents
conv.contexts.set('more', 5);
conv.ask(conv.user.storage.track === 1 ? suggestions1 : suggestions2);
} else {
conv.ask(suggestions3);
}
};
// Default intent for handling the start of the action
app.intent('Default Welcome Intent', (conv) => {
console.log(`Default Welcome: ${conv.user.last.seen}`);
// Check if the device supports media playback
if (!conv.surface.capabilities.has('actions.capability.MEDIA_RESPONSE_AUDIO')) {
conv.close('Sorry, this device does not support audio playback.');
return;
}
conv.ask(conv.user.last.seen ? getRandomItem(prompts.welcome_back) : getRandomItem(prompts.welcome));
conv.ask(getRandomPrompt(conv, 'intro'));
nextTrack(conv, true);
});
app.intent('Direct Welcome Intent', (conv) => {
console.log(`Direct Welcome: ${conv.user.last.seen}`);
// Check if the device supports media playback
if (!conv.surface.capabilities.has('actions.capability.MEDIA_RESPONSE_AUDIO')) {
conv.close('Sorry, this device does not support audio playback.');
return;
}
conv.ask(conv.user.last.seen ? getRandomItem(prompts.welcome_back) : getRandomItem(prompts.welcome));
conv.ask(getRandomPrompt(conv, 'intro'));
nextTrack(conv, true);
});
// Fallback intent to handle user responses that aren't handled by other intents
app.intent('Default Fallback Intent', (conv) => {
console.log(`Fallback: fallbackCount=${conv.data.fallbackCount}`);
console.log(`Fallback: raw=${conv.input.raw}`);
// Track the fallback count in session storage
conv.data.fallbackCount = parseInt(conv.data.fallbackCount, 10);
conv.data.fallbackCount++;
// Try to recover the conversation twice before ending the action
if (conv.data.fallbackCount === 1) {
return conv.ask(getRandomPrompt(conv, 'fallback1'));
}
conv.close(getRandomPrompt(conv, 'fallback2'));
});
// Handle the More/Yes/Next intents
app.intent(['More', 'Yes', 'Next'], (conv) => {
console.log(`More: fallbackCount=${conv.data.fallbackCount}`);
nextTrack(conv, false);
});
// Handle the Repeat/Previous intent
app.intent(['Repeat', 'Previous'], (conv) => {
console.log(`Repeat: ${conv.user.storage.track}`);
nextTrack(conv, false, true);
});
// Handle the No/Cancel/Don't know intents by closing the conversation
app.intent(['No', 'Cancel', `Don't know`], (conv) => {
conv.close(getRandomPrompt(conv, 'quit'));
});
// Handle the Help intent
app.intent('Help', (conv) => {
conv.ask(`${getRandomPrompt(conv, 'help')}`);
});
// Handle no-inputs from the user
// https://developers.google.com/actions/assistant/reprompts
app.intent('No-input', (conv) => {
const repromptCount = parseInt(conv.arguments.get('REPROMPT_COUNT'));
console.log(`No-input: repromptCount=${repromptCount}`);
if (repromptCount === 0) {
conv.ask(getRandomPrompt(conv, 'no_input1'));
} else if (repromptCount === 1) {
conv.ask(getRandomPrompt(conv, 'no_input2'));
} else if (conv.arguments.get('IS_FINAL_REPROMPT')) {
// Last no-input allowed; close conversation
conv.close(getRandomPrompt(conv, 'no_input3'));
}
});
// Handling callback after media playback completion
// https://developers.google.com/actions/assistant/responses#media_responses
app.intent('Media Status', (conv) => {
const mediaStatus = conv.arguments.get('MEDIA_STATUS');
if (mediaStatus && mediaStatus.status === 'FINISHED') {
console.log(`track finished: ${conv.user.storage.track}`);
if (supportsMore) {
// Automatically start playing the next track
nextTrack(conv, false);
} else {
conv.close(getRandomPrompt(conv, 'end'));
}
} else {
console.log('Unknown media status received.');
conv.close(getRandomPrompt(conv, 'error'));
}
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);