123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- """Make a self-consistent bot help section."""
- # Third party modules
- from davtelepot.utilities import (
- extract, get_cleaned_text, json_read, make_inline_keyboard,
- make_lines_of_buttons, make_button, MyOD
- )
- # Project modules
- from . import roles
- DENY_MESSAGE = (
- "Chiedi di essere autorizzato: se la tua richiesta verrà accolta, "
- "ripeti il comando /help per leggere il messaggio di aiuto."
- )
- def get_command_description(bot, update, user_record):
- """Get a string description of `bot` commands.
- Show only commands available for `update` sender.
- """
- user_role = roles.get_role(bot=bot, update=update, user_record=user_record)
- return "\n".join(
- [
- "/{}: {}".format(
- command,
- details['description']
- )
- for command, details in sorted(
- bot.commands.items(),
- key=lambda x:x[0]
- )
- if details['description']
- and user_role <= roles.get_privilege_code(
- details['authorization_level']
- )
- ]
- )
- def _make_button(x, y):
- if not y.startswith('help:///'):
- y = 'help:///{}'.format(y)
- return make_button(x, y)
- HELP_MENU_BUTTON = make_inline_keyboard(
- [
- _make_button(
- 'Torna al menu Guida 📖',
- 'menu'
- )
- ],
- 1
- )
- def get_help_buttons(bot, update, user_record):
- """Get `bot` help menu inline keyboard.
- Show only buttons available for `update` sender.
- """
- user_role = roles.get_role(bot=bot, update=update, user_record=user_record)
- buttons_list = [
- _make_button(
- section['label'],
- section['abbr']
- )
- for section in bot.help_sections.values()
- if 'auth' in section
- and user_role <= roles.get_privilege_code(
- section['auth']
- )
- ]
- return dict(
- inline_keyboard=(
- make_lines_of_buttons(buttons_list, 3)
- + make_lines_of_buttons(
- [
- _make_button('Comandi 🤖', 'commands')
- ],
- 1
- )
- + (
- bot.help_buttons
- if bot.authorization_function(update=update,
- authorization_level='user')
- else []
- )
- )
- )
- async def _help_command(bot, update, user_record):
- if not bot.authorization_function(update=update,
- authorization_level='everybody'):
- return DENY_MESSAGE
- reply_markup = get_help_buttons(bot, update, user_record)
- return dict(
- text=bot.help_message.format(bot=bot),
- parse_mode='HTML',
- reply_markup=reply_markup,
- disable_web_page_preview=True
- )
- async def _help_button(bot, update, user_record):
- data = update['data']
- command = extract(data, ':///')
- result, text, rm = '', '', None
- if command == 'commands':
- text = "<b>Comandi di {bot.name}</b>\n\n{cd}".format(
- bot=bot,
- cd=get_command_description(bot, update, user_record)
- )
- rm = HELP_MENU_BUTTON
- elif command == 'menu':
- text = bot.help_message.format(bot=bot)
- rm = get_help_buttons(bot, update, user_record)
- else:
- for code, section in bot.help_sections.items():
- if section['abbr'] == command:
- if not bot.authorization_function(
- update=update,
- authorization_level=section['auth']
- ):
- return "Non sei autorizzato!"
- rm = HELP_MENU_BUTTON
- text = (
- '<b>{s[label]}</b>\n\n{s[descr]}'
- ).format(
- s=section
- ).format(
- bot=bot
- )
- break
- if text or rm:
- return dict(
- text=result,
- edit=dict(
- text=text,
- parse_mode='HTML',
- reply_markup=rm,
- disable_web_page_preview=True
- )
- )
- return result
- async def _start_command(bot, update, user_record):
- text = get_cleaned_text(update=update, bot=bot, replace=['start'])
- if not text:
- return await _help_command(bot, update, user_record)
- update['text'] = text
- await bot.text_message_handler(
- update=update,
- user_record=None
- )
- return
- def init(bot, help_message="<b>Guida</b>",
- help_sections_file='data/help.json', help_buttons=[]):
- """Assign parsers, commands, buttons and queries to given `bot`."""
- bot.help_message = help_message
- bot.help_buttons = help_buttons
- bot.help_sections = MyOD()
- for code, section in enumerate(
- json_read(
- help_sections_file,
- default=[]
- )
- ):
- bot.help_sections[code] = section
- @bot.command("/start", authorization_level='everybody')
- async def start_command(bot, update, user_record):
- return await _start_command(bot, update, user_record)
- @bot.command(command='/help', aliases=['Guida 📖', '00help'],
- show_in_keyboard=True, description="Aiuto",
- authorization_level='everybody')
- async def help_command(bot, update, user_record):
- result = await _help_command(bot, update, user_record)
- return result
- @bot.button(prefix='help:///', authorization_level='everybody')
- async def help_button(bot, update, user_record):
- return await _help_button(bot, update, user_record)
|