Implement /log command for easier debugging

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2019-10-14 20:50:35 +05:30
parent a0cdda9e6e
commit 6ce2507c36
4 changed files with 22 additions and 1 deletions

View File

@ -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()

View File

@ -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)

View File

@ -11,6 +11,7 @@ class _BotCommands:
self.PingCommand = 'ping'
self.DiskCommand = 'disk'
self.HelpCommand = 'help'
self.LogCommand = 'log'
BotCommands = _BotCommands()

View File

@ -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)