From 82b203b5ab6f67663737b8183e649871f086561b Mon Sep 17 00:00:00 2001 From: Davte Date: Fri, 26 Jul 2019 20:20:53 +0200 Subject: [PATCH] /maintenance command implemented --- davtelepot/__init__.py | 2 +- davtelepot/administration_tools.py | 81 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index 367e13a..5a07362 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -14,7 +14,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.1.35" +__version__ = "2.1.36" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/administration_tools.py b/davtelepot/administration_tools.py index e975650..fc788be 100644 --- a/davtelepot/administration_tools.py +++ b/davtelepot/administration_tools.py @@ -705,6 +705,26 @@ default_admin_messages = { "errori.\n" "L'ordine è cronologico, con i messaggi nuovi in alto." } + }, + 'maintenance_command': { + 'description': { + 'en': "Put the bot under maintenance", + 'it': "Metti il bot in manutenzione" + }, + 'maintenance_started': { + 'en': "Bot has just been put under maintenance!\n\n" + "Until further notice, it will reply to users " + "with the following message:\n\n" + "{message}", + 'it': "Il bot è stato messo in manutenzione!\n\n" + "Fino a nuovo ordine, risponderà a tutti i comandi con il " + "seguente messaggio\n\n" + "{message}" + }, + 'maintenance_ended': { + 'en': "Maintenance ended!", + 'it': "Manutenzione terminata!" + } } } @@ -1045,6 +1065,52 @@ async def _errors_command(bot, update, user_record): return +async def _maintenance_command(bot, update, user_record): + maintenance_status = bot.change_maintenance_status( + maintenance_message=get_cleaned_text(update, bot, ['maintenance']) + ) + if maintenance_status: + return bot.get_message( + 'admin', 'maintenance_command', 'maintenance_started', + update=update, user_record=user_record, + message=bot.maintenance_message + ) + return bot.get_message( + 'admin', 'maintenance_command', 'maintenance_ended', + update=update, user_record=user_record + ) + + +def get_maintenance_exception_criterion(bot, allowed_command): + """Get a criterion to allow a type of updates during maintenance. + + `bot` : davtelepot.bot.Bot() instance + `allowed_command` : str (command to be allowed during maintenance) + """ + def criterion(update): + if 'message' not in update: + return False + update = update['message'] + text = get_cleaned_text(update, bot, []) + if ( + 'from' not in update + or 'id' not in update['from'] + ): + return False + with bot.db as db: + user_record = db['users'].find_one( + telegram_id=update['from']['id'] + ) + if not bot.authorization_function( + update=update, + user_record=user_record, + authorization_level=2 + ): + return False + return text == allowed_command.strip('/') + return criterion + + def init(bot, talk_messages=None, admin_messages=None): """Assign parsers, commands, buttons and queries to given `bot`.""" if talk_messages is None: @@ -1063,6 +1129,11 @@ def init(bot, talk_messages=None, admin_messages=None): ) ) + allowed_during_maintenance = [ + get_maintenance_exception_criterion(bot, command) + for command in ['stop', 'restart', 'maintenance'] + ] + @bot.additional_task(when='BEFORE') async def load_talking_sessions(): sessions = [] @@ -1182,3 +1253,13 @@ def init(bot, talk_messages=None, admin_messages=None): authorization_level='admin') async def errors_command(bot, update, user_record): return await _errors_command(bot, update, user_record) + + for exception in allowed_during_maintenance: + bot.allow_during_maintenance(exception) + + @bot.command(command='/maintenance', aliases=[], show_in_keyboard=False, + description=admin_messages[ + 'maintenance_command']['description'], + authorization_level='admin') + async def maintenance_command(bot, update, user_record): + return await _maintenance_command(bot, update, user_record)