forked from saiedmethal/MMM-AssistantMk2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-AssistantMk2.js
898 lines (812 loc) · 25.4 KB
/
MMM-AssistantMk2.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
//
// Module : MMM-AssistantMk2
//
var ytp
Module.register("MMM-AssistantMk2", {
defaults: {
verbose:true,
projectId: "", // Google Assistant ProjectId (Required only when you use gAction.)
useGactionCLI: false,
startChime: "connection.mp3",
noChimeOnSay: false,
deviceModelId: "", // It should be described in your config.json
deviceInstanceId: "", // It should be described in your config.json
deviceLocation: { // (optional)
coordinates: { // set the latitude and longitude of the device (rf. mygeoposition.com)
latitude: 51.5033640, // -90.0 - +90.0
longitude: -0.1276250, // -180.0 - +180.0
},
},
auth: {
keyFilePath: "./credentials.json"
},
defaultProfile: "default",
profiles: {
"default" : {
profileFile: "default.json",
lang: "en-US"
//currently available (estimation, not all tested):
// de-DE, en-AU, en-CA, en-GB, en-US, en-IN
// fr-CA, fr-FR, it-IT, ja-JP, es-ES, es-MX, ko-KR, pt-BR
// https://developers.google.com/assistant/sdk/reference/rpc/languages
},
},
transcriptionHook: {
"HIDE_ALL_MODULES": {
pattern: "hide all",
command: "HIDEMODULES"
},
"SHOW_ALL_MODULES": {
pattern: "show all",
command: "SHOWMODULES"
},
"SCREEN_ON": {
pattern: "wake up",
command: "SCREENON"
},
"SCREEN_OFF": {
pattern: "go to sleep",
command: "SCREENOFF"
},
"REBOOT": {
pattern: "reboot yourself",
command: "REBOOT"
},
"SHUTDOWN": {
pattern: "shutdown yourself",
command: "SHUTDOWN"
}
},
action: {},
command: {
"HIDEMODULES": {
moduleExec: {
module:()=>{
return []
},
exec: (module, params, key) => {
module.hide(1000, null, {lockString:"AMK2"})
}
}
},
"SHOWMODULES": {
moduleExec: {
module:[],
exec: (module, params, key) => {
module.show(1000, null, {lockString:"AMK2"})
}
}
},
"SCREENON": {
shellExec: {
exec: (params, key) => {
return "~/MagicMirror/modules/MMM-AssistantMk2/scripts/screenon.sh"
//return "ls -al"
},
options: (params, key)=> {
return ""
},
}
},
"SCREENOFF": {
shellExec: {
exec: "~/MagicMirror/modules/MMM-AssistantMk2/scripts/screenoff.sh",
options: null,
}
},
"REBOOT": {
/*
notificationExec: {
notification: "SHOW_ALERT",
payload: {
message: "You've ordered REBOOT. I'm showing just alert, but you can modify config.js to reboot really.",
timer: 5000,
}
},
*/
shellExec: {
exec: "sudo reboot now"
}
},
"SHUTDOWN": {
/*
notificationExec: {
notification: (params, key) => {
return "SHOW_ALERT"
},
payload: (params, key)=> {
return {
message: "You've ordered SHUTDOWN. I'm showing just alert, but you can modify config.js to reboot really.",
timer: 5000,
}
}
},
*/
shellExec: {
exec: "sudo shutdown now"
}
},
},
responseVoice: true, // If available, Assistant will response with her voice.
responseScreen: true, // If available, Assistant will response with some rendered HTML
responseAlert: true, // If available, Assistant will response with Alert module of MM
// Sometimes, any response could not be returned.
ignoreNoVoiceError: true, //To avoid some annoying youtube stop bug.
screenZoom: "80%",
screenDuration: 0, //If you set 0, Screen Output will be closed after Response speech finishes.
youtubeAutoplay: true,
spotifyAutoplay: true,
pauseOnYoutube:true,
youtubePlayerVars: { // You can set youtube playerVars for your purpose, but should be careful.
"controls": 0,
"loop": 1,
"rel": 0,
},
youtubePlayQuality: "default", //small, medium, large, hd720, hd1080, highres or default
alertError: true,
useWelcomeMessage: "",
record: {
sampleRate : 16000, // audio sample rate
threshold : 0.5, // silence threshold (rec only)
thresholdStart: null, // silence threshold to start recording, overrides threshold (rec only)
thresholdEnd : null, // silence threshold to end recording, overrides threshold (rec only)
silence : 1.0, // seconds of silence before ending
verbose : false, // log info to the console
recordProgram : "arecord", // Defaults to "arecord" - also supports "rec" and "sox"
device : null // recording device (e.g.: "plughw:1")
},
play: {
encodingOut: "MP3", //'MP3' or 'WAV' is available, but you might not need to modify this.
sampleRateOut: 24000,
playProgram: "mpg321", //Your prefer sound play program. By example, if you are running this on OSX, `afplay` could be available.
playOption: [], // If you need additional options to use playProgram, describe here. (except filename)
// e.g: ["-d", "", "-t", "100"]
},
onIdle: {
timer: 1000*60*30, // if you don't want to use this feature, just set timer as `0` or command as ""
command: "HIDEMODULES"
},
onActivate: {
timer: 0,
command: "SHOWMODULES"
},
notifications: {
ASSISTANT_ACTIVATE: "ASSISTANT_ACTIVATE",
ASSISTANT_DEACTIVATE: "ASSISTANT_CLEAR",
ASSISTANT_ACTIVATED: "ASSISTANT_ACTIVATED",
ASSISTANT_DEACTIVATED: "ASSISTANT_DEACTIVATED",
ASSISTANT_ACTION: "ASSISTANT_ACTION",
ASSISTANT_UNDERSTOOD: "ASSISTANT_UNDERSTOOD",
ASSISTANT_RESPONSE_END: "ASSISTANT_RESPONSE_END",
DEFAULT_HOOK_NOTIFICATION: "ASSISTANT_HOOK",
TEXT_QUERY: "ASSISTANT_QUERY",
SAY_TEXT: "ASSISTANT_SAY"
},
//magicQueryToSay: "", // set as your language e.g) "Repeat as me : '%TEXT%'",
},
magicQueryToSay: {
"de" : "Sprich mir nach : '%TEXT%'",
"en" : "Repeat after me : '%TEXT%'",
"fr" : "Répétez après moi : '%TEXT%'",
"it" : "Ripeti dopo di me : '%TEXT%'",
"ja" : "Repeat after me : '%TEXT%'", // I cannot find proper query for Japanese
"es" : "Repite despues de mi : '%TEXT%'",
"ko" : "Repeat after me : '%TEXT%'", // I cannot find proper query for Korean
"pt" : "Repita depois de mim : '%TEXT%'"
},
getStyles: function () {
return ["MMM-AssistantMk2.css"]
},
getCommands: function () {
return [
{
command: "q",
callback: "telegramCommand",
description: "You can command `AssistantMk2` by text with this command."
},
{
command: "s",
callback: "telegramCommand",
description: "You can make `MMM-AssistantMk2` to say some text with this command."
}
]
},
telegramCommand: function(command, handler) {
if (command == "q" && handler.args) {
handler.reply("TEXT", "AssistantMk2 will be activated")
this.notificationReceived(this.config.notifications.TEXT_QUERY, handler.args, "MMM-TelegramBot")
}
if (command == "s" && handler.args) {
handler.reply("TEXT", "AssistantMk2 will repeat your text: " + handler.args)
this.notificationReceived(this.config.notifications.SAY_TEXT, handler.args, "MMM-TelegramBot")
}
},
start: function () {
this.config = this.configAssignment({}, this.defaults, this.config)
this.sendSocketNotification("INIT", this.config)
var assistant = new AssistantHelper(this.config)
assistant.setProfile(this.config.profiles[this.config.defaultProfile])
assistant.registerHelper("sendNotification" , (noti, payload)=> {
this.sendNotification(noti, payload)
})
assistant.registerHelper("sendSocketNotification" , (noti, payload)=> {
this.sendSocketNotification(noti, payload)
})
this.assistant = assistant
},
getDom : function() {
return this.assistant.drawDom()
},
notificationReceived: function (notification, payload, sender) {
switch(notification) {
case "DOM_OBJECTS_CREATED":
this.assistant.initializeAfterLoading(this.config)
break
case this.config.notifications.ASSISTANT_ACTIVATE:
var profileKey = ""
if (payload.hasOwnProperty("profile") && payload.profile in this.config.profiles) {
profileKey = payload.profile
} else {
profileKey = this.config.defaultProfile
}
this.currentProfile = this.config.profiles[profileKey]
this.assistant.activate(this.currentProfile)
break
case this.config.notifications.ASSISTANT_DEACTIVATE:
this.assistant.clearResponse()
this.assistant.deactivate()
//this.hideScreen()
break
case this.config.notifications.SAY_TEXT:
if (typeof sender == "object") {
sender = sender.name
}
var profile = this.config.profiles[this.config.defaultProfile]
var text = (typeof payload == "string") ? payload : payload.text
profile.lang = (typeof payload == "object") ? payload.lang : profile.lang
var langCode = profile.lang.slice(0, 2)
var magicQuery = (langCode && langCode in this.magicQueryToSay) ? this.magicQueryToSay[langCode] : this.magicQueryToSay["en"]
if (magicQuery) {
this.assistant.quietRequest = true
magicQuery = magicQuery.replace("%TEXT%", payload)
this.assistant.activate(profile, magicQuery, sender)
} else {
console.log("[AMK2] magciQueryToSay is not set.")
}
break
case this.config.notifications.TEXT_QUERY:
if (typeof sender == "object") {
sender = sender.name
}
this.currentProfile = this.config.profiles[this.config.defaultProfile]
this.assistant.activate(this.currentProfile, payload, sender, true /* sayMode */)
break
}
},
socketNotificationReceived: function (notification, payload) {
switch(notification) {
case "INITIALIZED":
if (this.config.useWelcomeMessage) {
this.assistant.activate(this.config.profiles[this.config.defaultProfile], this.config.useWelcomeMessage)
this.config.useWelcomeMessage = ""
}
break
case "ASSISTANT_READY":
//do nothing
break
case "MIC_ON": //necessary?????
this.assistant.micStatus(true)
break
case "MIC_OFF":
this.assistant.micStatus(false)
case "SPEAKER_ON": //necessary?????
this.assistant.speakerStatus(true)
break
case "SPEAKER_OFF":
this.assistant.speakerStatus(false)
break
case "TRANSCRIPTION":
this.assistant.transcription(payload)
break
case "RESPONSE_START":
this.assistant.responseStart(payload)
break
case "RESPONSE_END":
this.sendNotification(this.config.notifications.ASSISTANT_RESPONSE_END)
break
case "CONVERSATION_END":
this.assistant.conversationEnd(payload)
break
case "CONVERSATION_ERROR":
case "ASSISTANT_ERROR":
this.assistant.onError(notification)
break
}
},
configAssignment : function (result) {
var stack = Array.prototype.slice.call(arguments, 1)
var item
var key
while (stack.length) {
item = stack.shift()
for (key in item) {
if (item.hasOwnProperty(key)) {
if (
typeof result[key] === "object"
&& result[key]
&& Object.prototype.toString.call(result[key]) !== "[object Array]"
) {
if (typeof item[key] === "object" && item[key] !== null) {
result[key] = this.configAssignment({}, result[key], item[key])
} else {
result[key] = item[key]
}
} else {
result[key] = item[key]
}
}
}
}
return result
}
})
class AssistantHelper {
constructor(config) {
this.config = config
this.helper = {}
this.events = {}
this.locked = false
this.profile = null
this.subdom = {
mic: null,
message: null,
screen: null,
youtube: null,
wrapper: null
}
this.dom = this.prepareDom()
this.status = "STANDBY" //STANDBY, READY, UNDERSTANDING, RESPONSING,
this.nextQuery = ""
this.screenTimer = null
this.youtubePlaying = false
this.idleTimer = null
this.quietRequest = false
this.isYoutubeReady = false
}
registerHelper(name, cb) {
this.helper[name] = cb
}
log (text) {
if(this.config.verbose) {
console.log("[AMK2] ", text)
}
}
setProfile(profile) {
this.profile = profile
}
configure(config) {
this.config = config
}
initializeAfterLoading() {
window.addEventListener("message", (e)=>{
this.screenMessage(e.data)
}, false)
this.prepareYoutube()
}
prepareYoutube() {
var tag = document.createElement("script")
tag.src = "https://www.youtube.com/iframe_api"
var firstScriptTag = document.getElementsByTagName("script")[0]
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
window.onYouTubeIframeAPIReady = () => {
this.isYoutubeReady = true
this.log("YouTube iframe API is ready.")
}
}
prepareDom() {
var wrapper = document.createElement("div")
wrapper.id = "ASSISTANT"
wrapper.className = "STANDBY"
var micImg = document.createElement("div")
micImg.id = "ASSISTANT_MIC"
micImg.onclick = (e)=> {
e.stopPropagation()
this.activate(this.profile)
}
wrapper.appendChild(micImg)
var message = document.createElement("div")
message.id = "ASSISTANT_MESSAGE"
wrapper.appendChild(message)
var screenOutput = document.createElement("iframe")
screenOutput.id = "ASSISTANT_SCREEN"
wrapper.appendChild(screenOutput)
var ytOutput = document.createElement("div")
ytOutput.id = "ASSISTANT_YOUTUBE"
wrapper.appendChild(ytOutput)
this.subdom.mic = micImg
this.subdom.message = message
this.subdom.screen = screenOutput
this.subdom.youtube = ytOutput
this.subdom.wrapper = wrapper
return wrapper
}
on(eventName, callback=()=>{}) {
this.events[eventName] = callback
}
off(eventName) {
if (this.events.hasOwnProperty(eventName)) {
delete this.events[eventName]
}
}
emit(eventName, payload) {
if (this.events.hasOwnProperty(eventName)) {
var fn = this.events[eventName]
if (typeof fn == "function") {
fn(payload)
}
}
}
transcription(payload) {
if (this.quietRequest) {
return
}
this.changeStatus((payload.done) ? "UNDERSTANDING" : null)
this.subdom.message.innerHTML = "<p>" + payload.transcription + "</p>"
}
changeStatus(key) {
if (key) {
this.status = key
this.subdom.wrapper.className = key
}
if (key == "STANDBY") {
this.subdom.mic.className = ""
}
if (key == "UNDERSTANDING") {
this.sendNotification(this.config.notifications.ASSISTANT_UNDERSTOOD)
}
}
getStatus() {
return this.status
}
isLocked() {
return this.locked
}
drawDom() {
return this.dom
}
sendSocketNotification(noti, payload) {
this.helper["sendSocketNotification"](noti, payload)
}
sendNotification(noti, payload) {
this.helper["sendNotification"](noti, payload)
}
onError(error) {
this.changeStatus("ERROR")
this.subdom.message.innerHTML = "<p>" + error + "</p>"
setTimeout(()=>{
this.clearResponse()
this.deactivate()
}, 3000)
}
activate(profile, textQuery=null, sender=null, id=null, sayMode=false) {
if (this.youtubePlaying && this.config.pauseOnYoutube) {
this.log("Assistant will not work during Youtube playing.")
return false
}
if (this.status == "STANDBY" || this.status == "UNDERSTANDING" || this.status == "RESPONSING") {
this.clearResponse()
//this.deactivate()
this.changeStatus("READY")
this.sendNotification(this.config.notifications.ASSISTANT_ACTIVATED)
this.sendSocketNotification("START", {profile:profile, textQuery:textQuery, sender:sender, id:id, sayMode:sayMode})
if (this.config.onActivate) {
setTimeout(()=>{
this.doCommand(this.config.onActivate, "onActivate", "onActivate")
}, this.config.onActivate.timer)
}
if (this.config.onIdle && this.config.onIdle.timer > 0) {
clearTimeout(this.idleTimer)
this.idleTimer = setTimeout(()=>{
this.doCommand(this.config.onIdle, "onIdle", "onIdle")
}, this.config.onIdle.timer)
}
return true
} else {
this.log("Assistant is busy.")
return false
}
}
deactivate(cb=()=>{}) {
//this.clearResponse()
this.changeStatus("STANDBY")
this.sendNotification(this.config.notifications.ASSISTANT_DEACTIVATED)
this.quietRequest = false
cb()
}
clearResponse() {
this.subdom.message.innerHTML = ""
// this.subdom.youtube.innerHTML = ""
// this.subdom.youtube.style.display = "none"
// this.youtubePlaying = false
//this.sendSocketNotification(this.config.notifications.ASSISTANT_DEACTIVATED)
}
micStatus(bool) {
this.subdom.mic.className = (bool) ? "MIC" : ""
}
speakerStatus(bool) {
this.subdom.mic.className = (bool) ? "SPEAKER" : ""
}
responseStart(payload) {
if (this.quietRequest) {
return
}
if (this.config.responseScreen && payload.screenOutput) {
this.subdom.screen.src = "/modules/MMM-AssistantMk2/tmp/temp.html"
clearTimeout(this.screenTimer)
setTimeout(()=>{
this.subdom.screen.className = "show"
},10)
}
if (!this.config.responseScreen && payload.foundTextResponse) {
this.alert(payload.foundTextResponse)
}
}
responseEnd(after=()=>{}, force=false) {
if (this.config.responseScreen) {
if (this.config.screenDuration > 0 && !force) {
this.screenTimer = setTimeout(()=>{
this.subdom.screen.className = "hide"
after()
}, this.config.screenDuration)
} else {
this.subdom.screen.className = "hide"
after()
}
} else {
this.sendNotification("HIDE_ALERT")
after()
}
}
alert(message) {
if(this.config.responseAlert) {
this.log(message)
var timer = (this.config.screenDuration > 3000) ? this.config.screenDuration : 3000
this.sendNotification("SHOW_ALERT", {
title: "MMM-AssistanMk2",
message: message,
timer: timer
})
}
}
foundError(error) {
if (error) {
var message = ""
if (typeof error == "string") {
message = error
} else {
message = error.toString()
}
if (this.config.alertError) {
this.onError(message)
this.alert(message)
}
this.log("Error:" + message)
}
}
doCommand (hooked, param, key) {
var hook
if (this.config.command.hasOwnProperty(hooked.command)) {
hook = this.config.command[hooked.command]
} else {
return
}
if (hook.hasOwnProperty("notificationExec")) {
var ne = hook.notificationExec
var notification = (ne.notification) ? ne.notification : this.config.notifications.DEFAULT_HOOK_NOTIFICATION
//var fn = (typeof notification == "function") ? notification(hook.payload, key) : notification
var fn = (typeof notification == "function") ? notification(param, key) : notification
var payload = (ne.payload) ? ne.payload : hook.payload
var fp
if (typeof payload == "function") {
fp = payload(param, key)
} else if (typeof payload == "object") {
fp = Object.assign({}, payload)
} else {
fp = payload
}
this.sendNotification(fn, fp)
}
if (hook.hasOwnProperty("shellExec")) {
var se = hook.shellExec
if (se.exec) {
var fs = (typeof se.exec == "function") ? se.exec(param, key) : se.exec
var options = (se.options) ? se.options : null
var fo = (typeof options == "function") ? options(param, key) : options
this.sendSocketNotification("SHELLEXEC", {command:fs, options:fo})
}
}
if (hook.hasOwnProperty("moduleExec")) {
var me = hook.moduleExec
var m = me.module
if (typeof me.module == 'function') {
m = me.module(param)
}
if (Array.isArray(m)) {
MM.getModules().enumerate((module)=>{
if (m.length == 0 || (m.indexOf(module.name) >=0)) {
var payload = Object.assign({}, param)
me.exec(module, payload, key)
}
})
}
}
}
foundHook (foundHook) {
if (foundHook.length > 0) {
for(var i in foundHook) {
var res = foundHook[i]
var hook = this.config.transcriptionHook[res.key]
this.doCommand(hook, res.match, res.key)
}
}
}
foundAction(foundAction) {
if (foundAction) {
if (this.config.action.hasOwnProperty(foundAction.command)) {
var action = this.config.action[foundAction.command]
this.doCommand(action, foundAction.params, foundAction.command)
}
}
}
conversationEnd(payload) {
this.foundError(payload.error)
this.foundAction(payload.foundAction)
this.foundHook(payload.foundHook)
if (payload.foundOpenSpotify) {
this.sendNotification("PLAY_SPOTIFY", {
url: payload.foundOpenSpotify
})
};
if (payload.foundVideo || payload.foundVideoList) {
if (this.config.youtubeAutoplay) {
var after = ()=>{}
if (this.config.pauseOnYoutube) {
after = ()=>{
this.clearResponse()
this.deactivate()
}
}
if (payload.foundVideo) {
this.playYoutubeVideo("video", payload.foundVideo, after)
}
if (payload.foundVideoList) {
this.playYoutubeVideo("videolist", payload.foundVideoList, after)
}
if (!this.config.pauseOnYoutube) {
this.deactivate()
}
}
if (this.config.spotifyAutoplay) {
var after = ()=>{}
if (this.config.pauseOnYoutube) {
after = ()=>{
this.clearResponse()
this.deactivate()
}
}
}
} else {
var thenAfter
if(payload.continueConversation) {
thenAfter = ()=> {this.activate(this.profile)}
this.responseEnd(thenAfter, true)
} else {
thenAfter = () => {
this.clearResponse()
this.deactivate()
}
this.responseEnd(thenAfter)
}
}
}
playYoutubeVideo(type, id, cb=()=>{}) {
if (!this.isYoutubeReady) {
this.log("Youtube API is not ready. Check erros on frontend.")
}
this.responseEnd(()=>{}, true)
var onClose = (holder, cb=()=>{}) => {
this.youtubePlaying = false
holder.style.display = "none"
holder.innerHTML = ""
cb()
}
var holder = this.subdom.youtube
holder.innerHTML = ""
holder.style.display = "block"
var yt = document.createElement("div")
yt.id = "YOUTUBE_" + id
holder.appendChild(yt)
var close = document.createElement("div")
close.className = "button button_close"
close.onclick = (e)=>{
e.stopPropagation()
ytp.stopVideo()
onClose(holder, cb)
}
holder.appendChild(close)
this.youtubePlaying = true
var quality = this.config.youtubePlayQuality
var playerVars = this.config.youtubePlayerVars
var index = 0
var pl = []
var onReady = (event) => {
console.log("Youtube player: on ready.")
if (type == "video") {
event.target.loadVideoById(id)
} else {
event.target.loadPlaylist({
listType: "playlist",
list:id
})
}
event.target.playVideo()
event.target.setPlaybackQuality(quality)
}
var currentIndex = 0
var onStateChange = (event)=>{
var playlist = event.target.getPlaylist()
if (event.data == YT.PlayerState.PLAYING) {
currentIndex = event.target.getPlaylistIndex();
}
if (event.data == YT.PlayerState.ENDED) {
if (Array.isArray(playlist)) {
if (currentIndex == (playlist.length - 1)) {
console.log("Youtube player: All list ends")
setTimeout(()=>{
onClose(holder, cb)
}, 100)
} else {
console.log("Youtube player: Next Video")
event.target.nextVideo()
}
} else {
console.log("Youtube player: Video ends")
setTimeout(()=>{
onClose(holder, cb)
}, 100)
}
} else {
console.log("youtube status:", event.data)
}
}
ytp = new YT.Player(yt.id, {
playerVars: playerVars,
events: {
"onReady": onReady,
"onStateChange": onStateChange,
"onError": (event)=> {
console.log("youtube error:", id, event)
}
}
})
}
screenMessage(obj) {
const ytVideoPattern = /youtube\.com\/watch\?v=([a-zA-Z0-9-_]*)$/ig
if (obj.hasOwnProperty("url")) {
var re = ytVideoPattern.exec(obj.url.href)
if (re.length > 0) {
this.playYoutubeVideo(re[1])
} else {
//external link
}
}
if (obj.hasOwnProperty("query")) {
if(obj.query.queryText) {
//this.nextQuery = obj.query.queryText
this.deactivate(()=>{
this.activate(this.profile, obj.query.queryText)
})
}
}
}
}