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 <ysh-alsager@hotmail.com>

* restart: switch to context based callbacks

* restart: restrict the command to owner only

Co-authored-by: Shivam Jha <jhashivam2020@gmail.com>
This commit is contained in:
Youssif Shaaban Alsager 2020-04-21 13:03:05 +03:00 committed by GitHub
parent 78b60d6715
commit 63a67d7a14
Signed by untrusted user: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

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

View File

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