-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
executable file
·63 lines (47 loc) · 1.6 KB
/
bot.py
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
#!/usr/bin/env python3
from telegram import Updater
import subprocess
import logging
import sys
import os
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Commands
def echo(bot, update):
bot.sendMessage(update.message.chat_id, text=update.message.text)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def diana(bot, update, args):
logger.info('Message for diana.')
commands = list(args)
commands.insert(0, "./diana/diana")
logger.info('Command: "%s"' % (' '.join(commands)))
# Correct telegram mistakes
if commands[1] == 'help':
commands[1] = '--help'
try:
output = subprocess.check_output(commands, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
output = e.output
logger.info('Output: "%s"' % (output))
bot.sendMessage(chat_id=update.message.chat_id, text=output.decode())
def main():
token = os.environ["TOKEN"]
updater = Updater(token)
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.addTelegramCommandHandler("diana", diana)
# on noncommand i.e message - echo the message on Telegram
# dp.addTelegramMessageHandler(echo)
# on error - print error to stdout
dp.addErrorHandler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':
main()