Skip to content

Instantly share code, notes, and snippets.

@nesffer
Created November 3, 2016 05:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesffer/b469218a6daa9ab624cfe61be64cd98e to your computer and use it in GitHub Desktop.
Save nesffer/b469218a6daa9ab624cfe61be64cd98e to your computer and use it in GitHub Desktop.
Telegram Bot Shell
const shell = require('./modules/shell');
var shellRegex = new RegExp('^/shell(' + BOTNAME + ')?$', 'i');
bot.onText(shellRegex, (msg, match) => {
var messageId = msg.message_id;
var chatId = msg.chat.id;
var fromId = msg.from.id;
var opts = {
reply_markup: JSON.stringify({force_reply: true, selective: true}),
reply_to_message_id: messageId
};
if (fromId === ADMINID) {
bot.sendMessage(chatId, 'Shell 명령을 입력하세요.', opts)
.then((sended) => {
var chatId = sended.chat.id;
var messageId = sended.message_id;
bot.onReplyToMessage(chatId, messageId, (message) => {
shell(message.text, (error, data) => {
var output = error || data;
if (output) {
if (output.length <= 4096) {
bot.sendMessage(chatId, output, {reply_to_message_id: message.message_id});
} else {
bot.sendMessage(chatId, 'Error! 너무 깁니다!', {reply_to_message_id: message.message_id});
}
} else {
bot.sendMessage(chatId, '출력값이 없습니다!', {reply_to_message_id: message.message_id});
}
});
});
});
}
});
var shellArgRegex = new RegExp('^/shell(' + BOTNAME + ')? (.+)', 'i');
bot.onText(shellArgRegex, (msg, match) => {
var messageId = msg.message_id;
var chatId = msg.chat.id;
var fromId = msg.from.id;
if (fromId === ADMINID) {
shell(match[2], (error, data) => {
var output = error || data;
if (output) {
if (output.length <= 4096) {
bot.sendMessage(chatId, output, {reply_to_message_id: messageId});
} else {
bot.sendMessage(chatId, 'Error! 너무 깁니다!', {reply_to_message_id: messageId});
}
} else {
bot.sendMessage(chatId, '출력값이 없습니다!', {reply_to_message_id: messageId});
}
});
}
});
const exec = require('child_process').exec;
module.exports = function (query, callback) {
exec(query, (error, stdout, stderr) => {
if (error) {
callback(error, null);
return;
}
callback(null, stdout);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment