From 63a67d7a141f62f13ddb733060c3161a695820b0 Mon Sep 17 00:00:00 2001 From: Youssif Shaaban Alsager Date: Tue, 21 Apr 2020 13:03:05 +0300 Subject: [PATCH] Add restart command (#53) * Add restart command This is handy when you need to restart the bot when something goes wrong. Currently, the bot restarts but doesn't send a message after restarting is done, feel free to improve this. It'll need something like storing bot status locally then on bot start, if restart status exists send a message. Signed-off-by: yshalsager * restart: switch to context based callbacks * restart: restrict the command to owner only Co-authored-by: Shivam Jha --- bot/__main__.py | 10 ++++++++++ bot/helper/telegram_helper/bot_commands.py | 1 + 2 files changed, 11 insertions(+) diff --git a/bot/__main__.py b/bot/__main__.py index 695b654..727c001 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,5 +1,7 @@ import shutil import signal +from os import execl +from sys import executable from telegram.ext import CommandHandler, run_async @@ -33,6 +35,11 @@ def start(update,context): @run_async +def restart(update, context): + reply = sendMessage("Restarting, Please wait!", context.bot, update) + execl(executable, executable, "-m", "bot") + + def ping(update, context): start_time = int(round(time.time() * 1000)) reply = sendMessage("Starting Ping", context.bot, update) @@ -80,6 +87,8 @@ def main(): filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) ping_handler = CommandHandler(BotCommands.PingCommand, ping, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + restart_handler = CommandHandler(BotCommands.RestartCommand, restart, + filters=CustomFilters.owner_filter) help_handler = CommandHandler(BotCommands.HelpCommand, bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) stats_handler = CommandHandler(BotCommands.StatsCommand, @@ -87,6 +96,7 @@ def main(): log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter) dispatcher.add_handler(start_handler) dispatcher.add_handler(ping_handler) + dispatcher.add_handler(restart_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(stats_handler) dispatcher.add_handler(log_handler) diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index d4e8b64..fee3fa2 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -10,6 +10,7 @@ class _BotCommands: self.AuthorizeCommand = 'authorize' self.UnAuthorizeCommand = 'unauthorize' self.PingCommand = 'ping' + self.RestartCommand = 'restart' self.StatsCommand = 'stats' self.HelpCommand = 'help' self.LogCommand = 'log'