From 61cebd0c1f2869cbd4b34c821bed5ef2c35e0731 Mon Sep 17 00:00:00 2001 From: Davte Date: Tue, 18 Feb 2020 15:36:58 +0100 Subject: [PATCH] Multi-language support for administration core functions --- davtelepot/__init__.py | 2 +- davtelepot/administration_tools.py | 5 ++++- davtelepot/bot.py | 19 ++++++++++++++----- davtelepot/languages.py | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index f416b79..a1b27bf 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.4.4" +__version__ = "2.4.5" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/administration_tools.py b/davtelepot/administration_tools.py index d2af4ee..ff5f18d 100644 --- a/davtelepot/administration_tools.py +++ b/davtelepot/administration_tools.py @@ -1070,8 +1070,11 @@ async def _errors_command(bot, update, user_record): async def _maintenance_command(bot, update, user_record): + maintenance_message = get_cleaned_text(update, bot, ['maintenance']) + if maintenance_message.startswith('{'): + maintenance_message = json.loads(maintenance_message) maintenance_status = bot.change_maintenance_status( - maintenance_message=get_cleaned_text(update, bot, ['maintenance']) + maintenance_message=maintenance_message ) if maintenance_status: return bot.get_message( diff --git a/davtelepot/bot.py b/davtelepot/bot.py index 3819743..b96f708 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -476,7 +476,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): message = self._unknown_command_message else: message = self.__class__._unknown_command_message - if message is not None: + if isinstance(message, str): message = message.format(bot=self) return message @@ -726,7 +726,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): if command in self.commands: replier = self.commands[command]['handler'] elif 'chat' in update and update['chat']['id'] > 0: - reply = self.unknown_command_message + reply = dict(text=self.unknown_command_message) else: # Handle command aliases and text parsers # Aliases are case insensitive: text and alias are both .lower() for alias, function in self.command_aliases.items(): @@ -1086,7 +1086,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): reply_markup=None, update=None, reply_to_update=False, - send_default_keyboard=True): + send_default_keyboard=True, + user_record=None): """Send text via message(s). This method wraps lower-level `TelegramBot.sendMessage` method. @@ -1119,6 +1120,14 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): if not text: return parse_mode = str(parse_mode) + if isinstance(text, dict) and chat_id > 0: + if user_record is None : + user_record = self.db['users'].find_one(telegram_id=chat_id) + text = self.get_message( + update=update, + user_record=user_record, + messages=text + ) text_chunks = self.split_message_text( text=text, limit=self.__class__.TELEGRAM_MESSAGES_MAX_LEN - 100, @@ -2026,7 +2035,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): ).parameters } ) - return self.authorization_denied_message + return dict(text=self.authorization_denied_message) self.commands[command] = dict( handler=decorated_command_handler, description=description, @@ -2101,7 +2110,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): if name in inspect.signature(parser).parameters } ) - return bot.authorization_denied_message + return dict(text=bot.authorization_denied_message) self.text_message_parsers[condition] = dict( handler=decorated_parser, description=description, diff --git a/davtelepot/languages.py b/davtelepot/languages.py index 65c622b..7b874ac 100644 --- a/davtelepot/languages.py +++ b/davtelepot/languages.py @@ -120,7 +120,7 @@ class MultiLanguageObject(object): return language or self.default_language def get_message(self, *fields, update=None, user_record=None, - default_message=None, language=None, **format_kwargs): + default_message=None, language=None, messages=None, **format_kwargs): """Given a list of strings (`fields`), return proper message. Language will be determined by `get_language` method. @@ -137,7 +137,7 @@ class MultiLanguageObject(object): language=language ) # Find result for `language` - result = self.messages + result = messages or self.messages for field in fields: if field not in result: logging.debug(