Working on /calc command

This commit is contained in:
Davte
2020-05-15 19:48:04 +02:00
parent b0132e0b30
commit 5d35579e10
3 changed files with 190 additions and 26 deletions

View File

@@ -2232,7 +2232,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
handler=decorated_command_handler, handler=decorated_command_handler,
description=description, description=description,
authorization_level=authorization_level, authorization_level=authorization_level,
language_labelled_commands=language_labelled_commands language_labelled_commands=language_labelled_commands,
aliases=aliases
) )
if type(description) is dict: if type(description) is dict:
self.messages['commands'][command] = dict( self.messages['commands'][command] = dict(

View File

@@ -1008,6 +1008,27 @@ default_unknown_command_message = {
} }
default_useful_tools_messages = { default_useful_tools_messages = {
'calculate_command': {
'description': {
'en': "Do calculations",
'it': "Calcola",
},
'help_section': None,
'instructions': {
'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"
"Inserisci un'espressione algebrica dopo /calcola per "
"ottenerne il risultato, oppure usa il comando in risposta, "
"o ancora usa la tastiera qui sotto.\n",
},
'language_labelled_commands': {
'en': "calculate",
'it': "calcola",
},
},
'info_command': { 'info_command': {
'description': { 'description': {
'en': "Use this command in reply to get information about a message", 'en': "Use this command in reply to get information about a message",

View File

@@ -10,34 +10,134 @@ from collections import OrderedDict
from .api import TelegramError from .api import TelegramError
from .bot import Bot from .bot import Bot
from .messages import default_useful_tools_messages from .messages import default_useful_tools_messages
from .utilities import get_cleaned_text, recursive_dictionary_update, get_user from .utilities import (get_cleaned_text, get_user, make_button,
make_inline_keyboard, recursive_dictionary_update, )
async def _message_info_command(bot: Bot, update: dict, language: str): calc_buttons = OrderedDict()
"""Provide information about selected update. calc_buttons[0] = dict(
value=0,
Selected update: the message `update` is sent in reply to. If `update` is symbol='0',
not a reply to anything, it gets selected. order=13
The update containing the command, if sent in reply, is deleted.
"""
if 'reply_to_message' in update:
selected_update = update['reply_to_message']
else:
selected_update = update
await bot.send_message(
text=bot.get_message(
'useful_tools', 'info_command', 'result',
language=language,
info=json.dumps(selected_update, indent=2)
),
update=update,
reply_to_message_id=selected_update['message_id'],
) )
if selected_update != update: calc_buttons[1] = dict(
try: value=1,
await bot.delete_message(update=update) symbol='1',
except TelegramError: order=9
pass )
calc_buttons[2] = dict(
value=2,
symbol='2',
order=10
)
calc_buttons[3] = dict(
value=3,
symbol='3',
order=11
)
calc_buttons[4] = dict(
value=4,
symbol='4',
order=5
)
calc_buttons[5] = dict(
value=5,
symbol='5',
order=6
)
calc_buttons[6] = dict(
value=6,
symbol='6',
order=7
)
calc_buttons[7] = dict(
value=7,
symbol='7',
order=1
)
calc_buttons[8] = dict(
value=8,
symbol='8',
order=2
)
calc_buttons[9] = dict(
value=9,
symbol='9',
order=3
)
calc_buttons['plus'] = dict(
value='+',
symbol='',
order=4
)
calc_buttons['minus'] = dict(
value='-',
symbol='',
order=8
)
calc_buttons['times'] = dict(
value='*',
symbol='✖️',
order=12
)
calc_buttons['divided'] = dict(
value='/',
symbol='',
order=16
)
calc_buttons['point'] = dict(
value='.',
symbol='.',
order=14
)
calc_buttons['enter'] = dict(
value='\n',
symbol='',
order=15
)
def get_calculator_keyboard():
return make_inline_keyboard(
[
make_button(
text=button['symbol'],
prefix='calc:///',
delimiter='|',
data=[button['value']]
)
for button in sorted(calc_buttons.values(), key=lambda b: b['order'])
],
4
)
async def _calculate_command(bot: Bot,
update: dict,
language: str,
command_name: str = 'calc'):
reply_markup = None
if 'reply_to_message' in update:
update = update['reply_to_message']
command_aliases = [command_name]
if command_name in bot.commands:
command_aliases += list(
bot.commands[command_name]['language_labelled_commands'].values()
) + bot.commands[command_name]['aliases']
text = get_cleaned_text(bot=bot,
update=update,
replace=command_aliases)
if not text:
text = bot.get_message(
'useful_tools', 'calculate_command', 'instructions',
language=language
)
reply_markup = get_calculator_keyboard()
else:
text = 'pass'
await bot.send_message(text=text,
update=update,
reply_markup=reply_markup)
async def _length_command(bot: Bot, update: dict, user_record: OrderedDict): async def _length_command(bot: Bot, update: dict, user_record: OrderedDict):
@@ -82,6 +182,33 @@ async def _length_command(bot: Bot, update: dict, user_record: OrderedDict):
) )
async def _message_info_command(bot: Bot, update: dict, language: str):
"""Provide information about selected update.
Selected update: the message `update` is sent in reply to. If `update` is
not a reply to anything, it gets selected.
The update containing the command, if sent in reply, is deleted.
"""
if 'reply_to_message' in update:
selected_update = update['reply_to_message']
else:
selected_update = update
await bot.send_message(
text=bot.get_message(
'useful_tools', 'info_command', 'result',
language=language,
info=json.dumps(selected_update, indent=2)
),
update=update,
reply_to_message_id=selected_update['message_id'],
)
if selected_update != update:
try:
await bot.delete_message(update=update)
except TelegramError:
pass
async def _ping_command(bot: Bot, update: dict): async def _ping_command(bot: Bot, update: dict):
"""Return `pong` only in private chat.""" """Return `pong` only in private chat."""
chat_id = bot.get_chat_id(update=update) chat_id = bot.get_chat_id(update=update)
@@ -151,6 +278,21 @@ def init(telegram_bot: Bot, useful_tools_messages=None):
) )
telegram_bot.messages['useful_tools'] = useful_tools_messages telegram_bot.messages['useful_tools'] = useful_tools_messages
@telegram_bot.command(command='/calc',
aliases=None,
reply_keyboard_button=None,
show_in_keyboard=False,
**{key: val for key, val
in useful_tools_messages['calculate_command'].items()
if key in ('description', 'help_section',
'language_labelled_commands')},
authorization_level='moderator')
async def calculate_command(bot, update, language):
return await _calculate_command(bot=bot,
update=update,
language=language,
command_name='calc')
@telegram_bot.command(command='/info', @telegram_bot.command(command='/info',
aliases=None, aliases=None,
reply_keyboard_button=None, reply_keyboard_button=None,