-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·134 lines (112 loc) · 3.5 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
var fs = require('fs');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const killProcess = require('./utils/').killProcess;
const CONFIG = JSON.parse(fs.readFileSync('./config.json'));
var TelegramBot = require('node-telegram-bot-api');
var token = CONFIG['BOT_TOKEN'];
// Setup polling way
var bot = new TelegramBot(token, {
polling: {
interval: 5000
}
});
var audiosArr = [];
onAudiosArrayUpdate();
fs.watch('./audios', {}, onAudiosArrayUpdate);
function onAudiosArrayUpdate() {
audiosArr = fs.readdirSync('./audios/').filter(track => track.indexOf('.mp3') > -1);
}
var ls;
var currentIndex = -1;
bot.on('message', function (msg) {
var chatId = msg.chat.id;
if (msg.text === '/photo') {
exec('sh ./capture_photo.sh', function (error, stdout, stderr) {
if (error) {
console.error('exec error:', error);
return;
}
console.log(stderr);
console.log(stdout);
var path = String(stdout).trim();
bot.sendPhoto(chatId, path);
});
bot.sendChatAction(chatId, 'upload_photo');
} else if (msg.text === '/video') {
exec('sh ./capture_video.sh', function (error, stdout, stderr) {
if (error) {
console.error('exec error:', error);
return;
}
console.log(stderr);
console.log(stdout);
var path = String(stdout).trim();
bot.sendVideo(chatId, path);
});
bot.sendChatAction(chatId, 'upload_video');
} else if (msg.text === '/audio') {
sendAudiosAvailable(chatId);
} else if (!isNaN(msg.text.substr(1))) {
play(Number(msg.text.substr(1)) - 1, chatId);
} else if (msg.text == 'ПРЕДЫДУЩАЯ') {
play(currentIndex - 1, chatId);
}
else if (msg.text == 'СЛЕДУЮЩАЯ') {
play(currentIndex + 1, chatId);
} else if (msg.text == 'СТОП') {
if (ls) {
killProcess(ls.pid);
}
sendAudiosAvailable(chatId);
}
});
function sendAudiosAvailable(chatId) {
var commands = [];
var keyboard = [];
var row = [];
var message = '';
audiosArr.forEach((track, index) => {
const trackName = track.replace(/[0-9]+\-/, '').replace('.mp3', '');
var command = '/' + (index + 1);
commands.push(command);
message += command + ' ' + trackName + '\n';
})
commands.forEach(function (command) {
row.push(command);
if (row.length == 10) {
keyboard.push(row);
row = [];
}
})
keyboard.push(row);
bot.sendMessage(chatId, message, {parse_mode: 'HTML', reply_markup: {keyboard: keyboard}});
}
function play(index, chatId) {
if (index < 0) {
currentIndex = -1;
return;
}
if (index >= audiosArr.length) {
currentIndex = audiosArr.length;
return;
}
const track = audiosArr[index];
if (!track) {
return;
}
currentIndex = index;
const trackName = track.replace(/[0-9]+\-/, '').replace('.mp3', '');
const nav = [];
if (currentIndex > 0) {
nav.push('ПРЕДЫДУЩАЯ');
}
if (currentIndex < audiosArr.length - 1) {
nav.push('СЛЕДУЮЩАЯ');
}
bot.sendMessage(chatId, trackName, {reply_markup: {keyboard: [nav, ['СТОП']]}});
if (ls) {
killProcess(ls.pid);
}
ls = spawn('/usr/bin/omxplayer', ['./audios/' + track]);
}