This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
telebot.js
62 lines (54 loc) · 1.61 KB
/
telebot.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
'use strict'
const winston = require('winston')
const moment = require('moment')
const configurationFile = 'settings.json'
const fs = require('fs')
const TelegramBot = require('node-telegram-bot-api')
const exec = require('child_process').exec
/**Load Settings */
const configuration = JSON.parse(fs.readFileSync(configurationFile))
let timestamp = () => {
return moment(new Date()).format('LLL')
}
let formatter = (options) => {
return `[${options.timestamp()}] [${options.level}] ${options.message}`
}
/**Create logger */
const logger = new winston.Logger({
transports: [
new (winston.transports.Console)(
{
timestamp: timestamp,
formatter: formatter
}
),
new (winston.transports.File)(
{
timestamp: timestamp,
filename: configuration.logFile,
json: false,
formatter: formatter
})
]
})
/**Create bot */
const bot = new TelegramBot(configuration.token, { polling: true })
logger.info('Bot is listening')
bot.onText(/\/execute (.+)/, function (msg, match) {
let command = match[1]
let fromId = msg.from.id
exec(command, function (error, stdout, stderr) {
let message = `${msg.from.username} : "${msg.text}" `
let resp = ''
if (error !== null) {
resp = stderr
message += `with error`
logger.warn(message)
} else {
resp = stdout
message += `successful`
logger.info(message)
}
bot.sendMessage(fromId, resp)
})
})