Finished working on calc command
This commit is contained in:
@@ -1015,14 +1015,16 @@ default_useful_tools_messages = {
|
||||
},
|
||||
'help_section': None,
|
||||
'instructions': {
|
||||
'en': "🔢 Calculator 🧮\n\n"
|
||||
'en': "🔢 <b>Calculator</b> 🧮\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"
|
||||
"- <code>ℹ️</code>: show information about special keys\n",
|
||||
'it': "🔢 <b>Calcolatrice</b> 🧮\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"
|
||||
"- <code>ℹ️</code>: 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': "🔢 <b>Calculator</b> 🧮\n\n"
|
||||
"<i>Enter an expression</i>",
|
||||
'it': "🔢 <b>Calcolatrice</b> 🧮\n\n"
|
||||
"<i>Mandami l'espressione</i>",
|
||||
},
|
||||
'special_keys': {
|
||||
'en': "<b>Special keys</b>\n"
|
||||
"- <code>**</code>: exponentiation\n"
|
||||
"- <code>//</code>: floor division\n"
|
||||
"- <code>mod</code>: modulus (remainder of division)\n"
|
||||
"- <code>MR</code>: result of last expression\n"
|
||||
"- <code>ℹ️</code>: show this help message\n"
|
||||
"- <code>💬</code>: write your expression in a message\n"
|
||||
"- <code>⬅️</code>: delete last character\n"
|
||||
"- <code>✅</code>: start a new line (and a new expression)\n",
|
||||
'it': "<b>Tasti speciali</b>\n"
|
||||
"- <code>**</code>: elevamento a potenza\n"
|
||||
"- <code>//</code>: quoziente della divisione\n"
|
||||
"- <code>mod</code>: resto della divisione\n"
|
||||
"- <code>MR</code>: risultato dell'espressione precedente\n"
|
||||
"- <code>ℹ️</code>: mostra questo messaggio\n"
|
||||
"- <code>💬</code>: invia un messaggio con l'espressione\n"
|
||||
"- <code>⬅️</code>: cancella ultimo carattere\n"
|
||||
"- <code>✅</code>: vai a capo (inizia una nuova espressione)\n",
|
||||
},
|
||||
'use_buttons': {
|
||||
'en': "Use buttons to enter an algebraic expression.\n\n"
|
||||
"<i>The input will be displayed after you stop typing for a "
|
||||
|
||||
@@ -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,6 +233,34 @@ async def _calculate_button(bot: Bot,
|
||||
language=language)
|
||||
)
|
||||
update['data'] = data
|
||||
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:
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user