forked from cosmosis14/custom-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
927 lines (821 loc) · 32 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
const alexa = require('ask-sdk-core')
// eslint-disable-next-line no-unused-vars
const AWS = require('aws-sdk')
const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter')
const persistenceAdapter = new DynamoDbPersistenceAdapter({
tableName: 'CustomYoutubeSettings',
createTable: true
})
const axios = require('axios')
const ytdl = require('ytdl-core')
// used to stop errors caused by db creation race conditions
let dbInitializing = false
const DbInitializingHandler = {
// eslint-disable-next-line no-unused-vars
canHandle(handlerInput) {
return dbInitializing
},
handle(handlerInput) {
dbInitializing = false
const speakOutput = 'On first use, this skill takes time to initialize. Please wait ten seconds and then try the skill again. Thank you for your patience'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
}
}
// Check compatibility of device
const CheckAudioInterfaceHandler = {
async canHandle(handlerInput) {
const audioPlayerInterface = ((((handlerInput.requestEnvelope.context || {}).System || {}).device || {}).supportedInterfaces || {}).AudioPlayer
return audioPlayerInterface === undefined
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('Sorry, this skill is not supported on this device')
.withShouldEndSession(true)
.getResponse()
}
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'
},
async handle(handlerInput) {
console.log('!!! Handling Launch !!!')
const appSettings = await getAppSettings(handlerInput)
let message
let reprompt
if (!appSettings.hasPreviousPlaybackSession) {
message = 'Welcome to Custom Youtube, say "play", followed by a song name'
reprompt = 'You can say "play", followed by a song name to hear music'
} else {
const playbackInfo = await getPlaybackInfo(handlerInput)
message = `You were listening to ${playbackInfo.title}, would you like to resume?`
reprompt = 'You can say "yes" to resume, or "no" to request a different song'
}
return handlerInput.responseBuilder
.speak(ssmlClean(message))
.reprompt(ssmlClean(reprompt))
.getResponse()
}
}
const StartAudioIntentHandler = {
async canHandle(handlerInput) {
let appSettings = await getAppSettings(handlerInput)
const request = handlerInput.requestEnvelope.request
if (!appSettings.inPlaybackSession) {
return request.type === 'IntentRequest' &&
request.intent.name === 'StartAudioIntent' &&
request.intent.slots.songName.value
}
if (request.type === 'IntentRequest') {
return request.intent.name === 'StartAudioIntent' &&
request.intent.slots.songName.value
}
},
async handle(handlerInput) {
console.log('!!! Handling StartAudioIntent !!!')
const request = handlerInput.requestEnvelope.request
let appSettings = await getAppSettings(handlerInput)
let playbackInfo = await getPlaybackInfo(handlerInput)
const songName = request.intent.slots.songName.value
console.log(songName)
const videoIds = await axiosGetVideoIds(songName)
const songInfo = await ytdlGetSong(videoIds[0])
appSettings.videoIds = videoIds
playbackInfo.index = 0
playbackInfo.url = songInfo.url
playbackInfo.title = songInfo.title
playbackInfo.token = videoIds[0]
playbackInfo.offsetInMilliseconds = 0
return controller.play(handlerInput)
}
}
// Designed to answer LaunchRequest prompts, but can be used to resume play as well
const YesIntentHandler = {
async canHandle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
const request = handlerInput.requestEnvelope.request
return !appSettings.inPlaybackSession &&
appSettings.hasPreviousPlaybackSession &&
request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.YesIntent'
},
handle(handlerInput) {
console.log('!!! Handling YesIntent !!!')
return controller.play(handlerInput)
}
}
// Designed to answer LaunchRequest prompts, but can be used to start a new search as well
const NoIntentHandler = {
async canHandle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
const request = handlerInput.requestEnvelope.request
return !appSettings.inPlaybackSession &&
request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.NoIntent'
},
async handle(handlerInput) {
console.log('!!! Handling NoIntent !!!')
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
// Playback Set
playbackInfo.index = 0
playbackInfo.url = ''
playbackInfo.title = ''
playbackInfo.offsetInMilliseconds = 0
playbackInfo.token = ''
appSettings.nextStreamEnqueued = false
appSettings.hasPreviousPlaybackSession = false
const message = 'What song would you like to hear? Say "play", followed by a song name'
const reprompt = 'You can say "play", followed by a song name to hear music'
return handlerInput.responseBuilder
.speak(ssmlClean(message))
.reprompt(ssmlClean(reprompt))
.getResponse()
}
}
const HelpIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.HelpIntent'
},
handle(handlerInput) {
console.log('!!! Handling HelpIntent !!!')
const speakOutput = 'This is an app to play youtube-sourced audio on alexa devices. To play audio from youtube, say alexa, ask custom youtube to play, followed by a song name, or other search term. You may also say next, previous, or start over, to edit the currently playing track. You can also ask custom youtube to repeat this song in order to put the current song on repeat mode, and replay until you tell it to stop. Saying custom youtube repeat off will cancel this. Lastly you can say ask custom youtube what song is this to get the title of the youtube video that the audio was sourced from.'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
}
}
const PausePlaybackHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.CancelIntent' ||
request.intent.name === 'AMAZON.StopIntent' ||
request.intent.name === 'AMAZON.PauseIntent')
},
async handle(handlerInput) {
console.log('!!! Handling PausePlayback (multi intent) !!!')
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
appSettings.inPlaybackSession = false
appSettings.hasPreviousPlaybackSession = true
playbackInfo.offsetInMilliseconds = appSettings.latestOffsetInMilliseconds
return controller.stop(handlerInput)
}
}
const ResumeIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.ResumeIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
if (appSettings.hasPreviousPlaybackSession) {
return controller.play(handlerInput)
} else {
const speakOutput = 'No content to resume, please start a new Custom Youtube session'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
}
}
}
const NextIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.NextIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
if (!appSettings.inPlaybackSession) {
const speakOutput = 'No music is currently playing, cannot play next'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
const playbackInfo = await getPlaybackInfo(handlerInput)
// if nextPlaybackInfo is already populated...
if (appSettings.nextStreamEnqueued && !appSettings.repeatMode) {
const nextPlaybackInfo = await getNextPlaybackInfo(handlerInput)
playbackInfo.index = nextPlaybackInfo.index
playbackInfo.url = nextPlaybackInfo.url
playbackInfo.title = nextPlaybackInfo.title
playbackInfo.token = nextPlaybackInfo.token
playbackInfo.offsetInMilliseconds = 0
// else, we need to populate playbackInfo from source (ytdl)
} else {
// get video id and songInfo after calculating the new index
let newIndex = await getNextIndex(handlerInput)
const newVideoId = appSettings.videoIds[newIndex]
const newSongInfo = await ytdlGetSong(newVideoId)
playbackInfo.index = newIndex
playbackInfo.url = newSongInfo.url
playbackInfo.title = newSongInfo.title
playbackInfo.token = newVideoId
playbackInfo.offsetInMilliseconds = 0
}
return controller.play(handlerInput)
}
}
}
const PreviousIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.PreviousIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
if (!appSettings.inPlaybackSession) {
const speakOutput = 'No music is currently playing, cannot play previous'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else if (playbackInfo.index === 0) {
const speakOutput = 'This is the first song in the search playlist, cannot play previous'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
// get video id and songInfo after calculating the new index
const newIndex = playbackInfo.index - 1
const newVideoId = appSettings.videoIds[newIndex]
const newSongInfo = await ytdlGetSong(newVideoId)
playbackInfo.index = newIndex
playbackInfo.url = newSongInfo.url
playbackInfo.title = newSongInfo.title
playbackInfo.token = newVideoId
playbackInfo.offsetInMilliseconds = 0
return controller.play(handlerInput)
}
}
}
const StartOverIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.StartOverIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
if (!appSettings.inPlaybackSession) {
const speakOutput = 'No music is currently playing, cannot start song over'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
const playbackInfo = await getPlaybackInfo(handlerInput)
playbackInfo.offsetInMilliseconds = 0
return controller.play(handlerInput)
}
}
}
const RepeatOnIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.RepeatIntent' ||
request.intent.name === 'RepeatOnIntent')
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
let speakOutput
if (!appSettings.inPlaybackSession) {
speakOutput = 'No music is currently playing, cannot repeat the song'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
speakOutput = 'Current song will now be played on repeat'
const playbackInfo = await getPlaybackInfo(handlerInput)
const nextPlaybackInfo = await getNextPlaybackInfo(handlerInput)
appSettings.repeatMode = true
const playBehavior = 'REPLACE_ENQUEUED'
nextPlaybackInfo.index = playbackInfo.index
nextPlaybackInfo.url = playbackInfo.url
nextPlaybackInfo.title = playbackInfo.title
nextPlaybackInfo.token = playbackInfo.token
nextPlaybackInfo.offsetInMilliseconds = 0
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.addAudioPlayerPlayDirective(playBehavior, nextPlaybackInfo.url, nextPlaybackInfo.token, nextPlaybackInfo.offsetInMilliseconds)
.withShouldEndSession(true)
.getResponse()
}
}
}
const RepeatOffIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'RepeatOffIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
let speakOutput
if (!appSettings.inPlaybackSession) {
speakOutput = 'No music is currently playing, cannot change repeat mode'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else if (!appSettings.repeatMode) {
speakOutput = 'Repeat mode is already turned off'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
speakOutput = 'Repeat mode is now turned off'
const playbackInfo = await getPlaybackInfo(handlerInput)
const nextPlaybackInfo = await getNextPlaybackInfo(handlerInput)
appSettings.repeatMode = false
// get the info for the next song after calculating next index and videoId
let enqueueVideoIndex = await getNextIndex(handlerInput)
const enqueueVideoId = appSettings.videoIds[enqueueVideoIndex]
let enqueVideoSongInfo
enqueVideoSongInfo = await ytdlGetSong(enqueueVideoId)
enqueVideoSongInfo = {
url: playbackInfo.url,
title: playbackInfo.title
}
// set nextPlaybackInfo and then use those values to queue the next song
const playBehavior = 'REPLACE_ENQUEUED'
nextPlaybackInfo.index = enqueueVideoIndex
nextPlaybackInfo.url = enqueVideoSongInfo.url
nextPlaybackInfo.title = enqueVideoSongInfo.title
nextPlaybackInfo.token = enqueueVideoId.token
nextPlaybackInfo.offsetInMilliseconds = 0
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.addAudioPlayerPlayDirective(playBehavior, nextPlaybackInfo.url, nextPlaybackInfo.token, nextPlaybackInfo.offsetInMilliseconds)
.withShouldEndSession(true)
.getResponse()
}
}
}
const SongInfoIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'SongInfoIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
let speakOutput
if (!appSettings.inPlaybackSession) {
speakOutput = 'No music is currently playing, cannot get song info'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
} else {
speakOutput = `This is ${playbackInfo.title}`
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
}
}
}
const ResetIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' &&
request.intent.name === 'ResetIntent'
},
async handle(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
let speakOutput
if (appSettings.inPlaybackSession) {
speakOutput = 'Cannot reset app data while song is playing. Please tell the app to stop first'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.getResponse()
} else {
resetAttributes(handlerInput)
speakOutput = 'Resetting app data'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.getResponse()
}
}
}
const AudioPlayerEventHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type.startsWith('AudioPlayer.')
},
async handle(handlerInput) {
const request = handlerInput.requestEnvelope.request
const audioPlayerEventName = request.type.split('.')[1]
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
const nextPlaybackInfo = await getNextPlaybackInfo(handlerInput)
switch (audioPlayerEventName) {
case 'PlaybackStarted':
console.log('!!! Handling PlaybackStarted !!!')
appSettings.hasPreviousPlaybackSession = false
// If we are now playing the next enqueued song, update the stored info
if (playbackInfo.token !== request.token && nextPlaybackInfo.token === request.token) {
playbackInfo.index = nextPlaybackInfo.index
playbackInfo.url = nextPlaybackInfo.url
playbackInfo.title = nextPlaybackInfo.title
playbackInfo.token = nextPlaybackInfo.token
playbackInfo.offsetInMilliseconds = nextPlaybackInfo.offsetInMilliseconds
}
break
case 'PlaybackFinished':
appSettings.nextStreamEnqueued = false
break
case 'PlaybackStopped':
// This functionality for playback resume is mostly handled in the PausePlaybackHandler
appSettings.latestOffsetInMilliseconds = getOffsetInMilliseconds(handlerInput)
appSettings.hasPreviousPlaybackSession = true
break
case 'PlaybackNearlyFinished': {
console.log('!!! Handling PlaybackNearlyFinished !!!')
if (appSettings.nextStreamEnqueued) {
break
}
appSettings.nextStreamEnqueued = true
let enqueueVideoIndex
if (appSettings.repeatMode) {
// replay same song
enqueueVideoIndex = playbackInfo.index
} else {
// get next song's index
enqueueVideoIndex = await getNextIndex(handlerInput)
}
const enqueueVideoId = appSettings.videoIds[enqueueVideoIndex]
let enqueVideoSongInfo
if (!appSettings.repeatMode) {
// new song data
// console.log(`index is: ${enqueueVideoIndex}\nvideoId is: ${enqueueVideoId}`)
enqueVideoSongInfo = await ytdlGetSong(enqueueVideoId)
} else {
// current song data (in repeat mode)
enqueVideoSongInfo = {
url: playbackInfo.url,
title: playbackInfo.title
}
}
const playBehavior = 'ENQUEUE'
const enqueueVideoUrl = enqueVideoSongInfo.url
const enqueueToken = enqueueVideoId
const offsetInMilliseconds = 0
const expectedPreviousToken = playbackInfo.token
// set nextPlaybackInfo values
nextPlaybackInfo.index = enqueueVideoIndex
nextPlaybackInfo.title = enqueVideoSongInfo.title
nextPlaybackInfo.url = enqueueVideoUrl
nextPlaybackInfo.token = enqueueToken
nextPlaybackInfo.offsetInMilliseconds = offsetInMilliseconds
handlerInput.responseBuilder.addAudioPlayerPlayDirective(
playBehavior,
enqueueVideoUrl,
enqueueToken,
offsetInMilliseconds,
expectedPreviousToken
)
break
}
case 'PlaybackFailed':
appSettings.hasPreviousPlaybackSession = false
appSettings.inPlaybackSession = false
appSettings.nextStreamEnqueued = false
console.log('Playback Failed: %j', handlerInput.requestEnvelope.request.error)
return
default:
throw new Error('Should never reach default case!')
}
return handlerInput.responseBuilder.getResponse()
}
}
const SystemExceptionHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'System.ExceptionEncountered'
},
handle(handlerInput) {
console.log(`System exception encountered: ${handlerInput.requestEnvelope.request.reason}`)
}
}
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`)
return handlerInput.responseBuilder.getResponse()
}
}
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`)
// const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.'
const speakOutput = 'Sorry, there was an error. Please check logs.'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.getResponse()
}
}
// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
canHandle(handlerInput) {
return alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
},
handle(handlerInput) {
const intentName = alexa.getIntentName(handlerInput.requestEnvelope)
const speakOutput = `You just triggered ${intentName} with no handler`
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse()
}
}
/* INTERCEPTORS */
const LoadPersistentAttributesRequestInterceptor = {
/**
* Set initial persistent attributes data and log request info
* @param {import('ask-sdk-core').HandlerInput} handlerInput
*/
async process(handlerInput) {
console.log('!!! Intercepting Request !!!')
console.log(handlerInput.requestEnvelope.request)
// console.log(handlerInput.requestEnvelope)
let persistentAttributes
try {
persistentAttributes = await handlerInput.attributesManager.getPersistentAttributes()
// Check if user is invoking the skill the first time and initialize values
if (Object.keys(persistentAttributes).length === 0) {
resetAttributes(handlerInput)
}
// Error will throw if the database is not initialized yet
} catch (error) {
console.log(error)
dbInitializing = true
}
}
}
const SavePersistentAttributesResponseInterceptor = {
/**
* Save persistent attributes before sending every response
* @param {import('ask-sdk-core').HandlerInput} handlerInput
*/
async process(handlerInput) {
console.log('!!! Intercepting Response !!!')
await handlerInput.attributesManager.savePersistentAttributes()
}
}
/* HELPER FUNCTIONS: */
/**
* Returns the appSettings object
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {Promise<{hasPreviousPlaybackSession: boolean, inPlaybackSession: boolean, nextStreamEnqueued: boolean, repeatMode: boolean, latestOffsetInMilliseconds: number, videoIds: string[]}>} appSettings - the persistent attribute object that represents skill-wide settings
*/
async function getAppSettings(handlerInput) {
const attributes = await handlerInput.attributesManager.getPersistentAttributes()
return attributes.appSettings
}
/**
* Returns the playbackInfo object
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {Promise<{index: number, offsetInMilliseconds: number, title: string, url: string, token: string}>} playbackInfo - the persistent attribute object that represents characteristics of the currently playing (or paused) audio track
*/
async function getPlaybackInfo(handlerInput) {
const attributes = await handlerInput.attributesManager.getPersistentAttributes()
return attributes.playbackInfo
}
/**
* Returns the nextPlaybackInfo object
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {Promise<{index: number, offsetInMilliseconds: number, title: string, url: string, token: string}>} nextPlaybackInfo - the persistent attribute object that represents characteristics of the enqueued audio track
*/
async function getNextPlaybackInfo(handlerInput) {
const attributes = await handlerInput.attributesManager.getPersistentAttributes()
return attributes.nextPlaybackInfo
}
/**
* Returns the offsetInMilliseconds of the AudioPlayer.Playback* request included in the handlerInput
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {number} offsetInMilliseconds - time offset for the audio track
*/
function getOffsetInMilliseconds(handlerInput) {
return handlerInput.requestEnvelope.request.offsetInMilliseconds
}
/**
* Returns the next index for the videoId to be played (ascending order in the videoId array)
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {Promise<number>} nextIndex - the index of the next videoId to fetch from youtube
*/
async function getNextIndex(handlerInput) {
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
let newIndex
if (playbackInfo.index < appSettings.videoIds.length - 1) {
newIndex = playbackInfo.index + 1
} else {
newIndex = 0
}
return newIndex
}
/**
* Returns an array of youtube video ids from a youtube data api call with the search query param defined by searchParam
* @param {string} searchParam - the search query param for the api call
* @returns {Promise<string[]>} videoIds
*/
async function axiosGetVideoIds(searchParam) {
const ytApiUrlBase = 'https://www.googleapis.com/youtube/v3/search?part=snippet'
const params = {
// default order is 'relevance'
// orderParamBase: '&order=',
// orderParam: 'viewCount',
searchParamBase: '&q=',
searchParam: searchParam,
typeParamBase: '&type=',
typeParam: 'video',
// change length of videoIds here, max is 50
maxResultsParam: '&maxResults=',
maxResults: 20,
keyParamBase: '&key=',
// keyParam: <UNCOMMENT AND INSERT YOUR YOUTUBE DATA API KEY HERE>
}
let query = ytApiUrlBase
// construct query
for (const key in params) {
query += params[key]
}
query = encodeURI(query)
console.log(query)
const response = await axios.get(query)
const responseItems = response.data.items
console.log(responseItems)
const videoIds = []
responseItems.forEach(item => {
videoIds.push(item.id.videoId)
})
return videoIds
}
/**
* Takes a youtube videoId and returns a url where the audio for that video is hosted, as well as the title of the original video
* @param {string} videoId - youtube videoId; e.g https://www.youtube.com/watch?v=[videoId]
* @returns {Promise<{url: string, title: string}>} object with two key-value pairs representing audio 'url' and 'title'
*/
async function ytdlGetSong(videoId) {
const ytInfo = await ytdl.getInfo('https://www.youtube.com/watch?v=' + videoId)
let formats = ytInfo.formats
let title = ytInfo.videoDetails.title
let url
for (const format in formats) {
if (formats[format].mimeType.includes('audio/mp4')) {
url = formats[format].url
break
}
}
return {url, title}
}
/**
* Cleans a string of any characters that could potentially break alexa's SSML interpreter
* @param {string} inString
* @returns {string} outString - cleaned version of inString
*/
function ssmlClean(inString) {
let outString = inString.replace(/&/g, '&')
outString = outString.replace(/"/g, '"')
outString = outString.replace(/'/g, ''')
outString = outString.replace(/</g, '<')
outString = outString.replace(/>/g, '>')
return outString
}
/**
* Resets the persistent attributes in the dynamodb to default values. Used for initialization, debugging, and reference of what persistent attributes should exist in the database.
* @param {import('ask-sdk-core').HandlerInput} handlerInput
*/
function resetAttributes(handlerInput) {
handlerInput.attributesManager.setPersistentAttributes({
appSettings: {
videoIds: '',
nextStreamEnqueued: false,
inPlaybackSession: false,
hasPreviousPlaybackSession: false,
repeatMode: false,
latestOffsetInMilliseconds: 0
},
playbackInfo: {
index: 0,
url: '',
title: '',
offsetInMilliseconds: 0,
token: ''
},
nextPlaybackInfo: {
index: 1,
url: '',
title: '',
offsetInMilliseconds: 0,
token: ''
}
})
console.log('!!! PersistentAttributes reset !!!')
}
const controller = {
/**
* Handles the creation of a response with an AudioPlayerPlayDirective, relying on previously set playbackInfo values. Also updates certain appSettings to maintain correct state of the skill.
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {Promise<import('ask-sdk-model').Response>} alexa response object
*/
async play(handlerInput) {
const responseBuilder = handlerInput.responseBuilder
const appSettings = await getAppSettings(handlerInput)
const playbackInfo = await getPlaybackInfo(handlerInput)
let {
url,
title,
token,
offsetInMilliseconds
} = playbackInfo
const playBehavior = 'REPLACE_ALL'
appSettings.inPlaybackSession = true
appSettings.hasPreviousPlaybackSession = false
appSettings.nextStreamEnqueued = false
appSettings.repeatMode = false
console.log(`playBehavior: ${playBehavior}\ntitle: ${title}\ntoken: ${token}\noffsetInMilliseconds: ${offsetInMilliseconds}\nurl: ${url}`)
responseBuilder
.speak(ssmlClean(`Now playing ${title}`))
.withShouldEndSession(true)
.addAudioPlayerPlayDirective(playBehavior, url, token, offsetInMilliseconds)
// implement canThrowCard if necessary and then use here to set card content
return responseBuilder.getResponse()
},
/**
* Handles the creation of a response with an AudioPlayerStopDirective
* @param {import('ask-sdk-core').HandlerInput} handlerInput
* @returns {import('ask-sdk-model').Response} alexa response object
*/
stop(handlerInput) {
const speakOutput = 'Pausing custom youtube'
return handlerInput.responseBuilder
.speak(ssmlClean(speakOutput))
.withShouldEndSession(true)
.addAudioPlayerStopDirective()
.getResponse()
}
}
// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = alexa.SkillBuilders.custom()
.addRequestHandlers(
DbInitializingHandler,
CheckAudioInterfaceHandler,
LaunchRequestHandler,
StartAudioIntentHandler,
YesIntentHandler,
NoIntentHandler,
HelpIntentHandler,
PausePlaybackHandler,
ResumeIntentHandler,
NextIntentHandler,
PreviousIntentHandler,
StartOverIntentHandler,
RepeatOnIntentHandler,
RepeatOffIntentHandler,
SongInfoIntentHandler,
ResetIntentHandler,
AudioPlayerEventHandler,
SessionEndedRequestHandler,
SystemExceptionHandler,
IntentReflectorHandler // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addRequestInterceptors(LoadPersistentAttributesRequestInterceptor)
.addResponseInterceptors(SavePersistentAttributesResponseInterceptor)
.withPersistenceAdapter(persistenceAdapter)
.addErrorHandlers(
ErrorHandler
)
.lambda()