From e19e022e12f7b70e6c2cbc5ee6d9b94edcbbfb3f Mon Sep 17 00:00:00 2001 From: Davte Date: Mon, 18 May 2020 18:33:40 +0200 Subject: [PATCH] Track input via message of algebraic expressions as well as input via buttons. --- davtelepot/__init__.py | 2 +- davtelepot/useful_tools.py | 46 +++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index 2a58146..ba0bbf1 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -11,7 +11,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.5.15" +__version__ = "2.5.16" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/useful_tools.py b/davtelepot/useful_tools.py index 29fdc79..b0299e0 100644 --- a/davtelepot/useful_tools.py +++ b/davtelepot/useful_tools.py @@ -238,7 +238,7 @@ async def _calculate_button(bot: Bot, if command == 'parser': reply_markup = None bot.set_individual_text_message_handler( - handler=_calculate_command, + handler=wrap_calculate_command(record_id=record_id), user_id=user_record['telegram_id'] ) elif command == 'info': @@ -398,11 +398,26 @@ async def calculate_session(bot: Bot, ) +def wrap_calculate_command(record_id: int = None, command_name: str = 'calc'): + async def wrapped_calculate_command(bot: Bot, + update: dict, + user_record: OrderedDict, + language: str,): + return await _calculate_command(bot=bot, + update=update, + user_record=user_record, + language=language, + command_name=command_name, + record_id=record_id) + return wrapped_calculate_command + + async def _calculate_command(bot: Bot, update: dict, user_record: OrderedDict, language: str, - command_name: str = 'calc'): + command_name: str = 'calc', + record_id: int = None): if 'reply_to_message' in update: update = update['reply_to_message'] command_aliases = [command_name] @@ -420,18 +435,33 @@ async def _calculate_command(bot: Bot, ) reply_markup = get_calculator_keyboard() else: - record_id = bot.db['calculations'].insert( - dict( - user_id=user_record['id'], - created=datetime.datetime.now(), - expression=text + if record_id is None: + record_id = bot.db['calculations'].insert( + dict( + user_id=user_record['id'], + created=datetime.datetime.now(), + expression=text + ) ) + expression = text + else: + record = bot.db['calculations'].find_one( + id=record_id + ) + expression = f"{record['expression'] or ''}\n{text}" + bot.db['calculations'].update( + dict( + id=record_id, + modified=datetime.datetime.now(), + expression=expression + ), + ['id'] ) text = bot.get_message( 'useful_tools', 'calculate_command', 'result', language=language, expressions=evaluate_expressions(bot=bot, - expressions=text, + expressions=expression, language=language) ) reply_markup = get_calculator_keyboard(additional_data=[record_id])