diff --git a/davtelepot/messages.py b/davtelepot/messages.py index 717fc5c..b7b16cf 100644 --- a/davtelepot/messages.py +++ b/davtelepot/messages.py @@ -1015,14 +1015,16 @@ default_useful_tools_messages = { }, 'help_section': None, 'instructions': { - 'en': "🔢 Calculator 🧮\n\n" + 'en': "🔢 Calculator 🧮\n\n" "Enter an algebraic expression after /calc to get its " "result, or use the command in reply to a message containing " - "an expression, or use the keyboard below.\n", - 'it': "🔢 Calcolatrice 🧮\n\n" + "an expression, or use the keyboard below.\n\n" + "- ℹ️: show information about special keys\n", + 'it': "🔢 Calcolatrice 🧮\n\n" "Inserisci un'espressione algebrica dopo /calcola per " "ottenerne il risultato, oppure usa il comando in risposta, " - "o ancora usa la tastiera qui sotto.\n", + "o ancora usa la tastiera qui sotto.\n\n" + "- ℹ️: mostra informazioni sui tasti speciali\n", }, 'invalid_expression': { 'en': "Invalid expression: {error}", @@ -1032,6 +1034,32 @@ default_useful_tools_messages = { 'en': "calculate", 'it': "calcola", }, + 'message_input': { + 'en': "🔢 Calculator 🧮\n\n" + "Enter an expression", + 'it': "🔢 Calcolatrice 🧮\n\n" + "Mandami l'espressione", + }, + 'special_keys': { + 'en': "Special keys\n" + "- **: exponentiation\n" + "- //: floor division\n" + "- mod: modulus (remainder of division)\n" + "- MR: result of last expression\n" + "- ℹ️: show this help message\n" + "- 💬: write your expression in a message\n" + "- ⬅️: delete last character\n" + "- : start a new line (and a new expression)\n", + 'it': "Tasti speciali\n" + "- **: elevamento a potenza\n" + "- //: quoziente della divisione\n" + "- mod: resto della divisione\n" + "- MR: risultato dell'espressione precedente\n" + "- ℹ️: mostra questo messaggio\n" + "- 💬: invia un messaggio con l'espressione\n" + "- ⬅️: cancella ultimo carattere\n" + "- : vai a capo (inizia una nuova espressione)\n", + }, 'use_buttons': { 'en': "Use buttons to enter an algebraic expression.\n\n" "The input will be displayed after you stop typing for a " diff --git a/davtelepot/useful_tools.py b/davtelepot/useful_tools.py index 2cb4438..29fdc79 100644 --- a/davtelepot/useful_tools.py +++ b/davtelepot/useful_tools.py @@ -38,8 +38,8 @@ def get_calc_buttons() -> OrderedDict: ) buttons['_'] = dict( value='_', - symbol='_️', - order='A4', + symbol='MR', + order='B5', ) buttons[0] = dict( value='0', @@ -129,35 +129,30 @@ def get_calc_buttons() -> OrderedDict: buttons['del'] = dict( value='del', symbol='⬅️', - order='F2', + order='E5', ) buttons['('] = dict( value='(', symbol='(️', - order='A5', + order='A4', ) buttons[')'] = dict( value=')', symbol=')️', - order='B5', + order='A5', ) - buttons['m'] = dict( - value=')', - symbol=')️', + buttons['info'] = dict( + value='info', + symbol='ℹ️️', order='C5', ) - buttons['u'] = dict( - value=')', - symbol=')️', + buttons['parser'] = dict( + value='parser', + symbol='💬️', order='D5', ) - buttons['d'] = dict( - value=')', - symbol=')️', - order='E5', - ) return buttons @@ -225,9 +220,11 @@ async def _calculate_button(bot: Bot, 'useful_tools', 'calculate_command', 'use_buttons', language=language ) - reply_markup = get_calculator_keyboard(additional_data=[record_id]) else: record_id = data[0] + reply_markup = get_calculator_keyboard( + additional_data=([record_id] if record_id else None) + ) if record_id not in bot.shared_data['calc']: bot.shared_data['calc'][record_id] = [] asyncio.ensure_future( @@ -236,7 +233,35 @@ async def _calculate_button(bot: Bot, language=language) ) update['data'] = data - bot.shared_data['calc'][record_id].append(update) + if len(data) and data[-1] in ('info', 'parser'): + command = data[-1] + if command == 'parser': + reply_markup = None + bot.set_individual_text_message_handler( + handler=_calculate_command, + user_id=user_record['telegram_id'] + ) + elif command == 'info': + reply_markup = make_inline_keyboard( + [ + make_button( + text='Ok', + prefix='calc:///', + delimiter='|', + data=[record_id, 'back'] + ) + ] + ) + text = bot.get_message( + 'useful_tools', 'calculate_command', ( + 'special_keys' if command == 'info' + else 'message_input' if command == 'parser' + else '' + ), + language=language + ) + else: + bot.shared_data['calc'][record_id].append(update) # Edit the update with the button if a new text is specified if not text: return @@ -309,7 +334,7 @@ async def calculate_session(bot: Bot, while queue_len != len(queue): queue_len = len(queue) await asyncio.sleep(buffer_seconds) - last_entry = max(queue, key=lambda u: u['id']) + last_entry = max(queue, key=lambda u: u['id'], default=None) # Delete record-associated queue queue = queue.copy() del bot.shared_data['calc'][record_id] @@ -336,6 +361,8 @@ async def calculate_session(bot: Bot, input_value = data[1] if input_value == 'del': expression = expression[:-1] + elif input_value == 'back': + pass elif input_value in calc_buttons: expression += calc_buttons[input_value]['value'] else: @@ -362,6 +389,8 @@ async def calculate_session(bot: Bot, 'useful_tools', 'calculate_command', 'instructions', language=language ) + if last_entry is None: + return await bot.edit_message_text( text=text, update=last_entry,