From 6ce2507c363d14fb00b51d038d00768b0fa83657 Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Mon, 14 Oct 2019 20:50:35 +0530 Subject: [PATCH] Implement /log command for easier debugging Signed-off-by: lzzy12 --- bot/__init__.py | 4 ++++ bot/__main__.py | 11 ++++++++++- bot/helper/telegram_helper/bot_commands.py | 1 + bot/helper/telegram_helper/message_utils.py | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bot/__init__.py b/bot/__init__.py index b4af047..e3914e3 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -5,7 +5,11 @@ import threading from telegram.ext import Updater import os +with open('log.txt', 'r+') as f: + f.truncate(0) + logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[logging.FileHandler('log.txt'), logging.StreamHandler()], level=logging.INFO) config = configparser.ConfigParser() diff --git a/bot/__main__.py b/bot/__main__.py index 834a8cd..11afa83 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -37,6 +37,11 @@ def ping(update, context): editMessage(f'{end_time - start_time} ms', context, reply) +@run_async +def log(update, context): + sendLogFile(context, update) + + @run_async def bot_help(update, context): help_string = f''' @@ -55,6 +60,9 @@ def bot_help(update, context): /{BotCommands.DiskCommand}: Show a status of the disk usage of the machine the bot is hosted on /{BotCommands.AuthorizeCommand}: Authorize a chat or a user to use the bot (Can only be invoked by owner of the bot) + +/{BotCommands.LogCommand}: Get a log file of the bot. Handy for getting crash reports + ''' sendMessage(help_string, context, update) @@ -69,11 +77,12 @@ def main(): bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) disk_handler = CommandHandler(BotCommands.DiskCommand, disk_usage, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) - + log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter) dispatcher.add_handler(start_handler) dispatcher.add_handler(ping_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(disk_handler) + dispatcher.add_handler(log_handler) updater.start_polling() LOGGER.info("Bot Started!") signal.signal(signal.SIGINT, fs_utils.exit_clean_up) diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index a6057af..7db7d57 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -11,6 +11,7 @@ class _BotCommands: self.PingCommand = 'ping' self.DiskCommand = 'disk' self.HelpCommand = 'help' + self.LogCommand = 'log' BotCommands = _BotCommands() diff --git a/bot/helper/telegram_helper/message_utils.py b/bot/helper/telegram_helper/message_utils.py index c584db8..ed96003 100644 --- a/bot/helper/telegram_helper/message_utils.py +++ b/bot/helper/telegram_helper/message_utils.py @@ -21,6 +21,13 @@ def deleteMessage(context, message: Message): message_id=message.message_id) +def sendLogFile(context, update: Update): + with open('log.txt', 'rb') as f: + context.bot.send_document(document=f, filename=f.name, + reply_to_message_id=update.message.message_id, + chat_id=update.message.chat_id) + + def auto_delete_message(context, cmd_message: Message, bot_message: Message): if AUTO_DELETE_MESSAGE_DURATION != -1: time.sleep(AUTO_DELETE_MESSAGE_DURATION)