56 lines
No EOL
1.3 KiB
JavaScript
56 lines
No EOL
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const {spawn} = require('child_process');
|
|
const TelegramBot = require('node-telegram-bot-api');
|
|
const APP_NAME = 'telme';
|
|
class TaskMessage {
|
|
static async run(token, userId, doneMessage) {
|
|
const bot = new TelegramBot(token);
|
|
|
|
const command = process.argv[2];
|
|
const args = process.argv.slice(3);
|
|
|
|
const exec = spawn(command, args);
|
|
const errors = await promisifyExec(exec);
|
|
let msg = doneMessage.replace('%cmd%', ` $ ${command} ${args.join(' ')}`)
|
|
.replace('%errors%', errors);
|
|
try {
|
|
await bot.sendMessage(userId, msg, {parse_mode: 'Markdown'});
|
|
console.log(`[${APP_NAME}]: Told ya!`);
|
|
} catch (e) {
|
|
errors = e.message;
|
|
console.error(`[${APP_NAME}]: An error occurred. Error: ${e.message}`);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function promisifyExec(exec) {
|
|
let errors = null;
|
|
return new Promise((resolve) => {
|
|
exec.stdout.on('data', (data) => {
|
|
console.log(String(data));
|
|
});
|
|
|
|
exec.on('error', (error) => {
|
|
errors = error.message;
|
|
resolve(errors);
|
|
});
|
|
|
|
exec.stderr.on('data', (data) => {
|
|
console.error(String(data));
|
|
if (!errors)
|
|
errors = data;
|
|
else
|
|
errors += `\n${data}`;
|
|
});
|
|
|
|
exec.on('close', async (code) => {
|
|
resolve(errors);
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
|
|
module.exports = TaskMessage; |