-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·643 lines (536 loc) · 20.1 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
#!/usr/bin/env node
// vim: set ts=4
const Telegraf = require('telegraf')
const { /* Extra, */ Markup } = require('telegraf')
const SocksAgent = require('socks5-https-client/lib/Agent');
// const HttpsAgent = require('https-proxy-agent')
const { spawn } = require('child_process')
const setTimeoutAsync = require('./src/set-timeout-async')
const {
token,
exec,
config,
consts,
UserError,
getOkIcon,
getIntro,
openRpi3,
setAsyncInterval,
} = require('./src/common')
const socksAgent = new SocksAgent({
socksHost: config.proxy.host,
socksPort: config.proxy.port,
socksUsername: config.proxy.login,
socksPassword: config.proxy.psswd,
})
// how to setup https proxy server:
// 1. apt-get install tinyproxy
// 2. setup tinyproxy (allow 0.0.0.0)
// 3. wrap into nginx proxy_pass
// 4. setup letsencrypt on it
// const httpsAgent = new HttpsAgent(config.httpsProxy)
const app = new Telegraf(token, {
telegram: {
agent: socksAgent,
// https://habr.com/sandbox/115306/
apiRoot: 'https://tg-api.invntrm.ru', // https://api.telegram.org
},
})
app.telegram.getMe().then((botInfo) => {
app.options.username = botInfo.username
})
app.use(Telegraf.log())
const jobs = require('./src/jobs')
const joker = require('./src/joker')()
const {
edit,
del,
typing,
sendMsgDefaultChat,
sendMsgStderrChat,
} = require('./src/tg-helpers')({ app, config })
const Commands = require('./src/commands')
const homeCmd = require('./commands/home')({ config })
const lightCmd = require('./commands/light')()
const musicCmd = require('./commands/music')()
const torrentsCmd = require('./commands/torrents')()
const volCmd = require('./commands/vol')()
const weatherCmd = require('./commands/weather')()
const fixerCmd = require('./commands/fixer')()
// eslint-disable-next-line
const cl = (...comments) => (fn) => (...args) => { const res = fn(...args); console.info(...comments, ': (', ...args, ') -->', res); return res; };
/**
* speech & voice over
*/
// TODO: move to plugins
let isVoiceVerboseMode = false
// const _isIn1wordAnsExpecting = false
// const isIn1wordAnsExpecting = () => {
// return _isIn1wordAnsExpecting ? (Date.now() - _isIn1wordAnsExpecting < 1000 * consts.ANS_EXP) : false
// }
//
// commands declaration
//
const commandsConfig = {
last: Commands.lastCommand,
list: {
voice: {
voice_over: [null, () => { isVoiceVerboseMode = true }, 'I`ll say everything you post'],
voice_over_stop: [null, () => { isVoiceVerboseMode = false }, 'I`ll be quiet'],
say: ['long_wait_msg', (ctx, [text]) => say(text, ctx)],
},
home: { presense: ['long_wait_msg', async () => ({ resMsg: await homeCmd.format() })] },
music: {
stop: [null, () => musicCmd.stop(), 'ok, music stopped'],
pause: [null, () => musicCmd.pause(), 'ok, music paused'],
resume: [null, () => musicCmd.resume(), 'ok, music resumed'],
play: ['ok, I`ll try', (_, [link]) => musicCmd.play(link)],
podcast: () => ['long_wait_msg', spawn('music-podcast')],
},
vol: {
louder: [null, () => volCmd.delta(+10), 'ok, volume increased'],
quieter: [null, () => volCmd.delta(-10), 'ok, volume decreased'],
upTo: [null, (_, [vol_]) => volCmd.upTo(vol_)],
downTo: [null, (_, [vol_]) => volCmd.downTo(vol_)],
get: async () => ({ resMsg: await volCmd.get() }),
},
light: {
on: () => lightCmd.on(),
off: () => lightCmd.off(),
status: async () => ({ resMsg: await lightCmd.status() ? '🌖 on' : '🌘 off' }),
},
weather: { forecast: ['long_wait_msg', (ctx) => weatherForecast(ctx)] },
misc: { print: (_, [text]) => sendMsgDefaultChat(text) },
jokes: {
joke: async () => ({ resMsg: await joker.next() }),
update: () => joker._loadNewPage(),
},
fixes: {
airplay: () => fixerCmd.airplay(),
rpi3: () => fixerCmd.rpi3(),
},
torrents: {
search: ['wait_msg', async (ctx, args) => {
const res = await searchTorrent(ctx, args.join(' ').trim()) // responds with a buttons
if (res === false) return { resMsg: 'Ничего не нашлось :(' }
}],
download: ['start downloading…', (_, [id]) => exec(`download-rutracker ${ id }`)],
status: ['long_wait_msg', ({ reply }) => torrentsStatus({ reply })],
},
fileReactions: {
audio: [null, (_, [link]) => playAudioLink(link), 'Музон в ваши уши'],
voice: (_, [link]) => exec(`wget -O /tmp/tg-bot-voice.oga "${ link }"`) /* exec(`asr /tmp/tg-bot-voice.oga`) */,
link: [null, (_, [sudo, link]) => openLinkRpi3(link, { sudo }), 'Ссылка открыта на станции'],
picture: [null, (_, [name, link]) => openPictureRpi3(link, name), 'Картинка открыта на станции'],
torrent: [null, ({ reply }, [link]) => openTorrentRpi3({ link, reply }), 'Поставлено на закачку'],
},
delivery: {
water: () => exec('send-tg-msg @makemetired "воды б"'),
},
search: {
google: [null, (_, [query]) => google(query), 'Загуглено на станции'],
},
},
}
const commands = Commands({
list: commandsConfig.list,
getOkIcon,
UserError,
consts,
homeCmd,
del,
typing,
sendMsgDefaultChat,
sendMsgStderrChat,
})
//
// listeners
//
// TODO: use intent system api.io etc
// TODO: use word2vel
// TODO: use phrase examples instead of RegExps
//
/* eslint-disable max-len */
/*
voice
*/
app.hears(/^(?:(?:читай|зачитывай)\s+((входящие\s+)?сообшения|ч[ая]т)|read\s+(?:chat|messages))/i, (ctx) => {
commands.run('voice', 'voice_over', ctx)
})
app.hears(/^(?:не\s+(читай|зачитывай)\s+((входящие\s+)?сообшения|ч[ая]т)|перестань\s+читать\s+ч[ая]т|no\s+read\s+(chat|messages))/i, (ctx) => {
commands.run('voice', 'voice_over_stop', ctx)
})
app.hears(/^(?:(?:say|скажи)\s+((?:.|\n)+))/im, (ctx) => {
commands.run('voice', 'say', ctx)
})
/*
home
*/
app.hears(/^(?:who\s+(?:is\s+)?at\+home\??|(?:есть\s)?(?:все|кто)\s+(?:ли\s+)?(?:дома|здесь)\??)/i, (ctx) => {
commands.run('home', 'presense', ctx)
})
/*
light
*/
app.hears(/^(?:turn\s+light\s+on|включи\s+свет)/i, (ctx) => {
commands.run('light', 'on', ctx)
})
app.hears(/^(?:turn\s+light\s+off|выключи\s+свет)/i, (ctx) => {
commands.run('light', 'off', ctx)
})
app.hears(/^(?:is\s+light\s+on|light\s+status|включен(\s+ли)?\s+свет|свет\s+включен\??)/i, (ctx) => {
commands.run('light', 'status', ctx)
})
/*
music
*/
app.hears(/^(?:(?:(?:сы|и)грай|воспроизведи|play))?(megapolis|music\s+podcast)/i, (ctx) => {
commands.run('music', 'podcast', ctx)
})
app.hears(/^(?:(выключи|останови|выруби|убери)\s+(?:музыку|звук|воспроизведение)|не\s+играй|stop\s+playing|stop\s+music)/i, (ctx) => {
commands.run('music', 'stop', ctx)
})
app.hears(/^(?:поставь\s+на\s+паузу|пауза$|pause(,\s+please!?)?)/i, (ctx) => {
commands.run('music', 'pause', ctx)
})
app.hears(/^(?:продолж(и|ай)\s+(воспроизведение|играть)|resume\s+playing)/i, (ctx) => {
commands.run('music', 'resume', ctx)
})
app.hears(/^(?:(?:(?:сы|и)грай|воспроизведи|play)\s+(\w+))/i, (ctx) => {
commands.run('music', 'play', ctx)
})
app.hears(/^\.\.$/, (ctx) => {
commands.run('music', 'stop', ctx)
})
app.hears(/^\|\|$/i, (ctx) => {
commands.run('music', 'pause', ctx)
})
app.hears(/^>>$/i, (ctx) => {
commands.run('music', 'resume', ctx)
})
app.hears(/^>>\s+(\w+)/i, (ctx) => {
commands.run('music', 'play', ctx)
})
/*
vol
*/
app.hears(/^(?:(?:сделай\s+)?(?:по)?громче|make(?:\s+(?:sound|music))?\s+louder)\s+(?:до|up\s*to)\s+(\d+)/i, (ctx) => {
commands.run('vol', 'upTo', ctx)
})
app.hears(/^(?:(?:сделай\s+)?(?:по)?тише|make(?:\s+(?:sound|music))?\s+quieter)\s+(?:до|down\s*to)\s+(\d+)/i, (ctx) => {
commands.run('vol', 'downTo', ctx)
})
app.hears(/^(?:(?:сделай\s+)?(?:по)?тише|make(?:\s+(?:sound|music))?\s+quieter)/i, (ctx) => {
commands.run('vol', 'quieter', ctx)
})
app.hears(/^(?:(?:сделай\s+)?(?:по)?громче|make(?:\s+(?:sound|music))?\s+louder)/i, (ctx) => {
commands.run('vol', 'louder', ctx)
})
app.hears(/^--$/, (ctx) => {
commands.run('vol', 'quieter', ctx)
})
app.hears(/^\+\+$/, (ctx) => {
commands.run('vol', 'louder', ctx)
})
/*
misc
*/
app.hears(/\b(wi-fi|wifi|lan|router|роутер)\b/i, (ctx) => {
ctx.reply('Нате вам: https://docs.google.com/document/d/1eXEPilIdV6Bxgbqi3l__uC04goAzYPhfVD-rz1Ixrv4/edit')
})
app.hears(/(умный дом|умном доме|умного дома|домашняя автоматизация|smarthome|smart home)/i, (ctx) => {
ctx.reply('Нате вам: https://www.notion.so/mxtnr/SmartHome-8db72c9f45ac4a3eb499dc603bc7cd04')
})
app.hears(/^(?:(?:какая\s+)?погода|чт?о\s+(там\s+)?с\s+погодой\??|чт?о\s+обещают\??|чт?о\s+с\s+погодой\??|(?:(?:(?:say|get|read)\s+)?(?:a\s+)?weather)|с погодой чт?о)/i, (ctx) => {
commands.run('weather', 'forecast', ctx)
})
app.hears(/^(?:text|print|напиши|наречатай)\s+((?:.|\n)+)$/im, (ctx) => {
commands.run('misc', 'print', ctx)
})
app.hears(/^(?:(?:(?:get|tell|next)\s+)?joke|(?:(?:(?:расскажи|давай)\s+)?(?:шутку|анекдот)|пошути|шуткуй))/i, (ctx) => {
commands.run('jokes', 'joke', ctx)
})
app.hears(/fix\s+airplay/i, (ctx) => {
commands.run('fixes', 'airplay', ctx)
})
app.hears(/fix\s+rpi3/i, (ctx) => {
commands.run('fixes', 'rpi3', ctx)
})
app.hears(/(?:(?:find|search|look up) (?:torrent|rutracker|serial|film)|(?:поищи|ищи|найди|искать|ищи) (?:торрент|на рутрекере|на rutracker|фильм|сериал))(.+)/i, (ctx) => {
commands.run('torrents', 'search', ctx)
})
app.hears(/(?:(?:status |get |check )?(?:torrent|rutracker|serial|film)s?|(?:проверь|чт?о (там )?с|как там|статус) (?:торрент(ы|ами)?|рутрекер(ом|а)?|на rutracker|фильм(ы|ами)?|сериал(ы|ами)?|закачк(а|и|ами)))|скачалось\?|торренты/i, (ctx) => {
commands.run('torrents', 'status', ctx)
})
app.hears(/([^ ]+\.torrent)/, (ctx) => {
commands.run('fileReactions', 'torrent', ctx)
})
app.hears(/([^ ]+\.(?:jpg|png))/, (ctx) => {
commands.run('fileReactions', 'picture', ctx, `from-chat-link${ new Date().getTime() }`)
})
app.hears(/([^ ]+\.mp3)/, (ctx) => {
commands.run('fileReactions', 'audio', ctx)
})
// todo: link detect
app.hears(/((?:sudo,?\s*)?)((?:https?:\S+)|(?:\S*(?:\.com\/|youtu\.be\/|\.ru\/)\S+))/, (ctx) => {
commands.run('fileReactions', 'link', ctx)
})
app.hears(/^(?:google|yandex|search|загугли|найди)\s+(.+)/i, (ctx) => {
commands.run('search', 'google', ctx)
})
app.on('audio', async (ctx) => {
const link = await app.telegram.getFileLink(ctx.message.audio.file_id)
commands.run('fileReactions', 'audio', ctx, link)
})
/* eslint-enable max-len */
// torrent
app.on('document', async (ctx) => {
if (!ctx.message.document || !ctx.message.document.file_name.endsWith('.torrent')) return
const torrentLink = await app.telegram.getFileLink(ctx.message.document.file_id)
commands.run('fileReactions', 'torrent', ctx, torrentLink)
})
app.on('photo', async (ctx) => {
const data = ctx.message.photo && ctx.message.photo[ctx.message.photo.length - 1]
if (!data) return
const imageLink = await app.telegram.getFileLink(data.file_id)
commands.run('fileReactions', 'picture', ctx, [data.file_id, imageLink])
})
app.on('voice', async (ctx) => {
if (!ctx.message.voice) return
const voiceLink = await app.telegram.getFileLink(ctx.message.voice.file_id)
commands.run('fileReactions', 'voice', ctx, voiceLink)
})
/*
/commands
*/
const withArgs = fn => ctx => {
const args = ctx.update.message.text.split(/\s+/).slice(1).join(' ')
fn(ctx, args)
}
const call = withArgs
// app.on('inline_query', (props) => {
// const { inlineQuery } = props
// console.log('aa?', props)
// //props.replyWithMarkdown('Hey there!')
// //answerInputTextMessageContent([{message_text:'Hey there!'}])
// })
// app.command('start', (props) => {
// const { from, reply } = props
// console.log('start', from, props)
// return reply('Welcome!')
// })
// voice over
app.command('voice_over', call((ctx, [cmd]) => {
if (['off', 'stop'].includes(cmd)) commands.run('voice', 'voice_over_stop', ctx)
commands.run('voice', 'voice_over', ctx)
}))
app.command('voice_over_stop', call((ctx) => {
commands.run('voice', 'voice_over_stop', ctx)
}))
// voice
app.command('say', call((ctx, args) => commands.run('voice', 'say', ctx, args)))
// volume
app.command('vol', call((ctx, [cmd]) => commands.run('vol', cmd, ctx)))
app.command('louder', call((ctx) => commands.run('vol', 'louder', ctx)))
app.command('quieter', call((ctx) => commands.run('vol', 'quieter', ctx)))
// music
app.command('music', call((ctx, [cmd]) => commands.run('music', cmd, ctx)))
app.command('pause', call((ctx) => commands.run('music', 'pause', ctx)))
app.command('resume', call((ctx) => commands.run('music', 'resume', ctx)))
app.command('stop', call((ctx) => commands.run('music', 'stop', ctx)))
// home
app.command('home', call((ctx) => commands.run('home', 'presense', ctx)))
// light
app.command('light', call((ctx, args) => commands.run('light', args, ctx)))
// weather
app.command('weath', call((ctx) => commands.run('weather', 'forecast', ctx)))
// joker
app.command('joke', call((ctx) => commands.run('jokes', 'joke', ctx)))
/*
universal
*/
//
// todo: repeat last command
//
// const lastQuestion = {
// _question: null,
// set: function (command) {
// this._question = (isYes) => isYes && command.repeat();
// },
// answer: function (isYes) {
// if (!this._question) { console.error('hm, there is not question'); return; }
// _isIn1wordAnsExpecting = false;
// this._question(isYes);
// }
// };
//
// app.hears(/^(?:повтори|((и|повтори)\s+)?ещё(\s+раз)?|(one\s+more\s+time|more|repeat)(,\s+please)?)$/i, (ctx) => {
// if (!commands.last.has()) return;
// switch (commands.last.type) {
// // change the entity
// case ('put'):
// break;
// // get the/an entity (see cacheControl)
// case ('get'):
// if (commands.last.cacheControl === 'no-cache') {
// commands.last.repeat()
// } else {
// ctx.reply('no changes');
// }
// break;
// // create the entity
// case ('post'):
// ctx.reply('are you sure, you want to repeat?')
// lastQuestion.set(commands.last);
// break;
// // delete the/an entity (see cacheControl)
// case ('delete'):
// if (commands.last.cacheControl === 'no-cache') {
// ctx.reply('are you sure, you want to repeat?')
// lastQuestion.set(commands.last);
// } else {
// ctx.reply('already deleted');
// }
// break;
// default: ctx.reply('I`m not sure about last command'); break;
// }
// });
// app.hears(/^(?:yep|yes|да|Y)/i, (ctx) => {
// if (isIn1wordAnsExpecting()) {
// _isIn1wordAnsExpecting = false;
// lastQuestion.answer(true);
// }
// });
// app.hears(/^(?:no|nope|N|нет|не-а)/i, (ctx) => {
// if (isIn1wordAnsExpecting()) {
// lastQuestion.answer(false);
// }
// });
app.hears(/^(hi|hey|ping)$/i, ({ reply }) => reply('Hey there!'))
app.hears(/./, (ctx) => {
if (!isVoiceVerboseMode) return
const name = ctx.update.message.from.first_name
say(`говорит ${ homeCmd.get(name, 'name') || name }: ${ ctx.match.input }`, ctx, true)
})
app.action(/.+/, (ctx) => {
const match = ctx.match && ctx.match[0].match(/^torrent download (\d+)/)
if (match) {
commands.run('torrents', 'download', ctx, [match[1]])
}
return ctx.answerCallbackQuery(`Oh, ${ ctx.match[0] }! Great choise`)
})
//
// helpers
//
async function searchTorrent (ctx, query) {
const list = await torrentsCmd.search(query)
if (!list || !list.length) return false
list.forEach(torrent => {
ctx.replyWithHTML(
torrentsCmd.printable(torrent),
Markup.inlineKeyboard([Markup.callbackButton('Download', `torrent download ${ torrent.id }`)]).extra(),
)
})
}
async function weatherForecast (ctx) {
const formattedWeather = await weatherCmd.forecast()
if ((new Date()).getHours() >= 9) say(formattedWeather, ctx, true, true)
return { resMsg: formattedWeather }
}
// TODO: move to commands
async function say (text, ctx, isQuiet, noIntro) {
if (!text) { console.info('тут и говорить нечего'); return }
console.info('>>', text.trim().replace(/\n/g, ' '))
const stdout = await exec(`tts "${ noIntro ? '' : getIntro() }, ${ text.replace(/\n/g, ' ') }"`)
console.info('say stdout', stdout)
if (!isQuiet) ctx.reply('я всё сказал')
}
/**
* file handlers
* TODO: move to commands
*/
async function openTorrentRpi3 ({ link, reply }) {
const tmpFile = '/tmp/tg-bot.torrent'
await exec(`wget -O ${ tmpFile } "${ link }"`)
// await exec(`scp ${ tmpFile } pi@rpi3:/home/pi/Downloads`)
await exec(`cp ${tmpFile } /home/pi/Downloads`)
torrentsStatus({ reply })
}
// Send torrents progress status
async function torrentsStatus ({ reply }) {
await setTimeoutAsync(3000)
const repCtx = await reply((await torrentsCmd.status()).message)
// update torrents progress status message every 5 second
const editNotify = async () => {
const nextStatus = await torrentsCmd.status()
if (nextStatus.complete) {
reply('✨ Трррррнты скчччлись, мои маленькие пираты!\nСвистать всех наверх!')
return setAsyncInterval.STOP
}
return await edit(repCtx, nextStatus.message)
}
const cycle = setAsyncInterval(editNotify, 1000 * 60)
setTimeout(() => cycle.stop(), 1000 * 60 * 60 * 1)
return { okMsg: false }
}
async function openPictureRpi3 (link, name, { sudo = false } = {}) {
if (!sudo && isNight()) throw new UserError('night. Try with sudo, bro')
const tmpFileName = `tg-bot.${ name }.jpg`
const tmpFilePath = `/tmp/${ tmpFileName }`
const targetFilePath = `/home/pi/Downloads/${ tmpFileName }`
await exec(`wget "${ link }" -O "${ tmpFilePath }"`)
// await exec(`scp "${ tmpFilePath }" "pi@rpi3:${ targetFilePath }"`)
await exec(`cp "${ tmpFilePath }" "${ targetFilePath }"`)
openRpi3(`gpicview ${ targetFilePath }`, { isX11: true, isResident: true })
}
function openLinkRpi3 (link, { sudo = false } = {}) {
if (!sudo && isNight()) throw new UserError('night. Try with sudo, bro')
if (link.includes('youtube') || link.includes('youtu.be')) {
console.info('youtube link', link)
return openYoutubeLinkRpi3(link, { sudo })
}
return openRpi3(`chromium-browser "${ link }"`, { isX11: true, isResident: true })
}
// function openVideoLinkRpi3 (link) {
// // body...
// }
async function openYoutubeLinkRpi3 (link, { sudo = false } = {}) {
const normalizedLink = /^http/.test(link) ? `https://${ link }` : link
const open = () => openRpi3(`/home/pi/bin/kodi-cli -s; /home/pi/bin/kodi-cli -y "${ normalizedLink }"`)
if (!sudo && isNight()) throw new UserError('night. Try with sudo, bro')
try {
await openRpi3('[[ "$(ps aux | grep kodi | grep -v grep)" ]] || (echo "no kodi"; exit 1)')
return await open()
} catch (e) { // in case kodi does not running
console.error(e)
await openRpi3('kodi', { isX11: true, isResident: true })
return new Promise(async (res) => {
setTimeout(async () => {
await open()
res()
}, 15000)
})
}
}
async function playAudioLink (link) {
const ext = link.match(/\w+$/)[0]
const filePath = `/tmp/tg-bot-audio.${ ext }`
await exec(`wget -O ${ filePath } "${ link }"`)
await musicCmd.play(filePath)
}
async function google (query, { sudo = false } = {}) {
return await openLinkRpi3(`https://ya.ru/?q=${ encodeURIComponent(query) }`, { sudo })
}
function isNight () {
const h = (new Date()).getHours()
return h > 23 || h < 8
}
/**
* misc
*/
// move to common.js
//
// run
//
app.startPolling()
jobs({ commands })