/maintenance command implemented

This commit is contained in:
Davte 2019-07-26 20:20:53 +02:00
parent 417dcb63e4
commit 82b203b5ab
2 changed files with 82 additions and 1 deletions

View File

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

View File

@ -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': "<i>Bot has just been put under maintenance!</i>\n\n"
"Until further notice, it will reply to users "
"with the following message:\n\n"
"{message}",
'it': "<i>Il bot è stato messo in manutenzione!</i>\n\n"
"Fino a nuovo ordine, risponderà a tutti i comandi con il "
"seguente messaggio\n\n"
"{message}"
},
'maintenance_ended': {
'en': "<i>Maintenance ended!</i>",
'it': "<i>Manutenzione terminata!</i>"
}
}
}
@ -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)