Working on calc button

This commit is contained in:
Davte
2020-05-16 15:51:49 +02:00
parent 5d35579e10
commit 5bc58dd7d5

View File

@@ -5,6 +5,7 @@ import datetime
import json import json
from collections import OrderedDict from collections import OrderedDict
from typing import List, Union
# Project modules # Project modules
from .api import TelegramError from .api import TelegramError
@@ -14,87 +15,122 @@ from .utilities import (get_cleaned_text, get_user, make_button,
make_inline_keyboard, recursive_dictionary_update, ) make_inline_keyboard, recursive_dictionary_update, )
calc_buttons = OrderedDict() def get_calc_buttons() -> OrderedDict:
calc_buttons[0] = dict( buttons = OrderedDict()
value=0, buttons['pow'] = dict(
symbol='0', value='**',
order=13 symbol='**',
) order='A1',
calc_buttons[1] = dict( )
value=1, buttons['floordiv'] = dict(
symbol='1', value='//',
order=9 symbol='//',
) order='A2',
calc_buttons[2] = dict( )
value=2, buttons['mod'] = dict(
symbol='2', value='%',
order=10 symbol='mod',
) order='A3',
calc_buttons[3] = dict( )
value=3, buttons['info'] = dict(
symbol='3', value='info',
order=11 symbol='',
) order='A4',
calc_buttons[4] = dict( )
value=4, buttons[0] = dict(
symbol='4', value=0,
order=5 symbol='0',
) order='E1',
calc_buttons[5] = dict( )
value=5, buttons[1] = dict(
symbol='5', value=1,
order=6 symbol='1',
) order='D1',
calc_buttons[6] = dict( )
value=6, buttons[2] = dict(
symbol='6', value=2,
order=7 symbol='2',
) order='D2',
calc_buttons[7] = dict( )
value=7, buttons[3] = dict(
symbol='7', value=3,
order=1 symbol='3',
) order='D3',
calc_buttons[8] = dict( )
value=8, buttons[4] = dict(
symbol='8', value=4,
order=2 symbol='4',
) order='C1',
calc_buttons[9] = dict( )
value=9, buttons[5] = dict(
symbol='9', value=5,
order=3 symbol='5',
) order='C2',
calc_buttons['plus'] = dict( )
value='+', buttons[6] = dict(
symbol='', value=6,
order=4 symbol='6',
) order='C3',
calc_buttons['minus'] = dict( )
value='-', buttons[7] = dict(
symbol='', value=7,
order=8 symbol='7',
) order='B1',
calc_buttons['times'] = dict( )
value='*', buttons[8] = dict(
symbol='✖️', value=8,
order=12 symbol='8',
) order='B2',
calc_buttons['divided'] = dict( )
value='/', buttons[9] = dict(
symbol='', value=9,
order=16 symbol='9',
) order='B3',
calc_buttons['point'] = dict( )
value='.', buttons['plus'] = dict(
symbol='.', value='+',
order=14 symbol='',
) order='B4',
calc_buttons['enter'] = dict( )
value='\n', buttons['minus'] = dict(
symbol='', value='-',
order=15 symbol='',
) order='C4',
)
buttons['times'] = dict(
value='*',
symbol='✖️',
order='D4',
)
buttons['divided'] = dict(
value='/',
symbol='',
order='E4',
)
buttons['point'] = dict(
value='.',
symbol='.',
order='E2',
)
buttons['000'] = dict(
value='*1000',
symbol='0⃣0⃣0',
order='E3',
)
buttons['enter'] = dict(
value='\n',
symbol='',
order='F1',
)
buttons['del'] = dict(
value='del',
symbol='⬅️',
order='F2',
)
return buttons
calc_buttons = get_calc_buttons()
def get_calculator_keyboard(): def get_calculator_keyboard():
@@ -112,6 +148,32 @@ def get_calculator_keyboard():
) )
async def _calculate_button(bot: Bot,
language: str,
data: List[Union[int, str]]):
result, text, reply_markup = '', '', None
if len(data) == 1:
input_value = data[0]
if input_value == 'del':
pass
elif input_value == 'info':
pass
elif input_value in [button['value'] for button in calc_buttons.values()]:
pass
else:
pass # Error!
# Edit the update with the button if a new text is specified
if not text:
return result
return dict(
text=result,
edit=dict(
text=text,
reply_markup=reply_markup
)
)
async def _calculate_command(bot: Bot, async def _calculate_command(bot: Bot,
update: dict, update: dict,
language: str, language: str,
@@ -286,13 +348,19 @@ def init(telegram_bot: Bot, useful_tools_messages=None):
in useful_tools_messages['calculate_command'].items() in useful_tools_messages['calculate_command'].items()
if key in ('description', 'help_section', if key in ('description', 'help_section',
'language_labelled_commands')}, 'language_labelled_commands')},
authorization_level='moderator') authorization_level='everybody')
async def calculate_command(bot, update, language): async def calculate_command(bot, update, language):
return await _calculate_command(bot=bot, return await _calculate_command(bot=bot,
update=update, update=update,
language=language, language=language,
command_name='calc') command_name='calc')
@telegram_bot.button(prefix='calc:///',
separator='|',
authorization_level='everybody')
async def calculate_button(bot, language, data):
return await _calculate_button(bot=bot, language=language, data=data)
@telegram_bot.command(command='/info', @telegram_bot.command(command='/info',
aliases=None, aliases=None,
reply_keyboard_button=None, reply_keyboard_button=None,
@@ -301,7 +369,7 @@ def init(telegram_bot: Bot, useful_tools_messages=None):
in useful_tools_messages['info_command'].items() in useful_tools_messages['info_command'].items()
if key in ('description', 'help_section', if key in ('description', 'help_section',
'language_labelled_commands')}, 'language_labelled_commands')},
authorization_level='moderator') authorization_level='everybody')
async def message_info_command(bot, update, language): async def message_info_command(bot, update, language):
return await _message_info_command(bot=bot, return await _message_info_command(bot=bot,
update=update, update=update,