/
main.js
180 lines (146 loc) · 4.17 KB
/
main.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
require('dotenv').config();
const robot = require("robotjs");
const express = require('express');
const fs = require('fs');
const os = require('os');
const readline = require('readline');
const { Telegraf, Markup, BaseScene, Stage } = require('telegraf');
// #region Constants
const port = 3000;
const availableCommands = [
'audio_mute',
'audio_play',
'audio_pause',
'audio_prev',
'audio_next',
'audio_vol_down',
'audio_vol_up',
];
// #endregion
// #region Functions
function setCommand(app, command) {
app.post(`/commands/${command}`, (req, res) => {
robot.keyTap(command);
res.send({ ok: true });
});
}
function getLocalIpAddresses() {
return Object.entries(os.networkInterfaces())
.reduce((previousValue, currentValue) => previousValue.concat(currentValue[1]), [])
.filter(item => (item.family === 'IPv4' && item.netmask === '255.255.255.0'))
.map(item => item.address);
}
function printInfo() {
console.log('Local IP addresses:');
const localIpAddresses = getLocalIpAddresses();
localIpAddresses.forEach(address => console.log(address));
console.log();
}
// #endregion
function readFileAsync(filename) {
console.log(filename);
return new Promise((resolve, reject) => {
fs.readFile(filename, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
})
});
}
function getFileLoader(filename, contentType) {
return async function(req, res) {
const content = await readFileAsync(filename);
res.set('Content-Type', contentType);
res.status(200);
res.send(content);
}
}
function startExpressVersion() {
printInfo();
const app = express();
// TODO: make normal later
app.get('/', getFileLoader('./index.html', 'text/html'));
app.get('/manifest.json', getFileLoader('./manifest.json', 'application/manifest+json'));
app.get('/pwa_logo.jpg', getFileLoader('./pwa_logo.jpg', 'image/jpg'));
app.get('/sw.js', getFileLoader('./sw.js', 'application/javascript'));
availableCommands.forEach(command => setCommand(app, command));
app.listen(port, () => console.log(`Started on port: ${port}`));
}
function startTelegramVersion() {
const logginedIds = new Set();
const password = Math.random().toFixed(6).substr(-6);
console.log(`Password: ${password}`);
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN)
bot.command('start', ({ reply }) => reply(`/login\n/logout\n\n/plus\n\n/minus\n\n/pause\n\n/mute\n\n/next\n\n/prev`));
bot.command('login', (ctx) => {
// TODO: add logic
logginedIds.add(ctx.chat.id);
return ctx.reply(`done`);
});
bot.command('logout', (ctx) => {
// TODO: add logic
logginedIds.delete(ctx.chat.id);
return ctx.reply(`done`);
});
// TODO: add logic verifcation id logic
bot.command('plus', (ctx) => {
robot.keyTap('audio_vol_up');
ctx.deleteMessage();
});
bot.command('minus', (ctx) => {
robot.keyTap('audio_vol_down');
ctx.deleteMessage();
});
bot.command('pause', (ctx) => {
robot.keyTap('audio_pause');
ctx.deleteMessage();
});
bot.command('mute', (ctx) => {
robot.keyTap('audio_mute');
ctx.deleteMessage();
});
bot.command('next', (ctx) => {
robot.keyTap('audio_next');
ctx.deleteMessage();
});
bot.command('prev', (ctx) => {
robot.keyTap('audio_prev');
ctx.deleteMessage();
});
bot.on('message', async (ctx) => {
console.log(ctx.message?.text);
});
bot.launch();
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
console.log(`Bot started`);
}
function getUserInput(question) {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
});
}
async function main() {
const answer = await getUserInput('1 - telegram mode, 2 - web mode:\n');
const answeerToHandlers = {
'1': startTelegramVersion,
'2': startExpressVersion,
};
const handler = answeerToHandlers[answer];
if (!handler) {
console.log('Incorrect input');
return;
}
handler();
}
main();