From 24952b2b827b44058575fd5e15344c112d21f553 Mon Sep 17 00:00:00 2001 From: Davte Date: Sun, 18 Aug 2019 21:04:38 +0200 Subject: [PATCH] /help command refactored using davtelepot.helper --- MANIFEST.in | 1 - ciclopibot/__init__.py | 2 +- ciclopibot/bot.py | 87 +------------- ciclopibot/ciclopi.py | 1 + ciclopibot/data/__init__.py | 1 - ciclopibot/data/help.json | 8 -- ciclopibot/helper.py | 233 ++---------------------------------- ciclopibot/messages.py | 110 +++++++++++++++++ 8 files changed, 124 insertions(+), 319 deletions(-) delete mode 100644 ciclopibot/data/help.json diff --git a/MANIFEST.in b/MANIFEST.in index 56321a3..e429dde 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,3 @@ exclude ciclopibot/data/* include ciclopibot/data/__init__.py -include ciclopibot/data/help.json exclude ciclopibot/img* diff --git a/ciclopibot/__init__.py b/ciclopibot/__init__.py index a13eff7..d44e423 100644 --- a/ciclopibot/__init__.py +++ b/ciclopibot/__init__.py @@ -3,6 +3,6 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __license__ = "GNU General Public License v3.0" -__version__ = "1.1.0" +__version__ = "1.1.1" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/ciclopibot/bot.py b/ciclopibot/bot.py index e6d66a9..e1e9ff2 100644 --- a/ciclopibot/bot.py +++ b/ciclopibot/bot.py @@ -13,88 +13,9 @@ from davtelepot import administration_tools, authorization, languages from . import ciclopi from . import helper from .data.passwords import bot_token - -language_messages = { - 'language_command': { - 'name': { - 'en': "/language", - 'it': "/lingua" - }, - 'reply_keyboard_button': { - 'en': "Language ๐Ÿ—ฃ", - 'it': "Lingua ๐Ÿ—ฃ" - }, - 'alias': { - 'en': "Language ๐Ÿ—ฃ", - 'it': "Lingua ๐Ÿ—ฃ" - }, - 'description': { - 'en': "Change language settings", - 'it': "Cambia le impostazioni della lingua" - } - }, - 'language_button': { - 'description': { - 'en': "Change language settings", - 'it': "Cambia le impostazioni della lingua" - }, - 'language_set': { - 'en': "Selected language: English ๐Ÿ‡ฌ๐Ÿ‡ง", - 'it': "Lingua selezionata: Italiano ๐Ÿ‡ฎ๐Ÿ‡น" - } - }, - 'language_panel': { - 'text': { - 'en': "Choose a language", - 'it': "Seleziona una lingua" - } - } -} - -supported_languages = { - 'en': { - 'flag': '๐Ÿ‡ฌ๐Ÿ‡ง', - 'name': 'English' - }, - 'it': { - 'flag': '๐Ÿ‡ฎ๐Ÿ‡น', - 'name': 'Italiano' - } -} - -ciclopibot_help_messages = { - 'help_command': { - 'text': { - 'en': "๐Ÿ“– {bot.name} guide\n\n" - "Welcome!\n" - "To visit a guide section, press the corresponding button.\n" - "To view all available commands, see section `Comandi`.\n\n" - "Bot author and administrator: @Davte", - 'it': "๐Ÿ“– Guida di {bot.name}\n\n" - "Benvenuto!\n" - "Per leggere una sezione della guida premi il bottone " - "corrispondente. Per conoscere tutti i comandi " - "disponibili, visita l'apposita sezione della guida " - "premendo il pulsante Comandi.\n\n" - "Autore e amministratore del bot: @Davte" - }, - 'reply_keyboard_button': { - 'en': "Help ๐Ÿ“–", - 'it': "Guida ๐Ÿ“–" - }, - 'description': { - 'en': "Help", - 'it': "Aiuto" - }, - 'access_denied_message': { - 'en': "Ask for authorization. If your request is accepted, send " - "/help command again to read the guide.", - 'it': "Chiedi di essere autorizzato: se la tua richiesta " - "verrร  accolta, ripeti il comando /help per leggere " - "il messaggio di aiuto." - } - } -} +from .messages import ( + language_messages, supported_languages +) if __name__ == '__main__': path = os.path.dirname(__file__) @@ -168,8 +89,6 @@ if __name__ == '__main__': ciclopi.init(bot) helper.init( bot=bot, - help_messages=ciclopibot_help_messages, - help_sections_file='ciclopibot/data/help.json' ) authorization.init(bot) languages.init( diff --git a/ciclopibot/ciclopi.py b/ciclopibot/ciclopi.py index 86e207d..b060fe7 100644 --- a/ciclopibot/ciclopi.py +++ b/ciclopibot/ciclopi.py @@ -1606,6 +1606,7 @@ def init(bot, ciclopi_messages=None, ciclopi_messages_json=None, description=( bot.messages['ciclopi']['command']['description'] ), + help_section=bot.messages['ciclopi']['help'], authorization_level='everybody') async def ciclopi_command(bot, update, user_record): return await _ciclopi_command(bot, update, user_record) diff --git a/ciclopibot/data/__init__.py b/ciclopibot/data/__init__.py index 12a3224..d7c15aa 100644 --- a/ciclopibot/data/__init__.py +++ b/ciclopibot/data/__init__.py @@ -11,7 +11,6 @@ Examples of data files local_host = '127.0.0.1' port = 8080 ``` -- `help.json`: file where /help data are stored - `passwords.py`: secret file where you can store your bot token ```python bot_token = "111222333:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" diff --git a/ciclopibot/data/help.json b/ciclopibot/data/help.json deleted file mode 100644 index b0f09d2..0000000 --- a/ciclopibot/data/help.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - { - "label": "CicloPi ๐Ÿšฒ", - "abbr": "ciclopi", - "auth": "everybody", - "descr": "Per vedere quante bici disponibili e quanti posti liberi ci sono in ogni stazione CicloPi usa il comando /ciclopi." - } -] diff --git a/ciclopibot/helper.py b/ciclopibot/helper.py index ab6efdd..c9e6eca 100644 --- a/ciclopibot/helper.py +++ b/ciclopibot/helper.py @@ -1,234 +1,19 @@ """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 -) +import davtelepot.helper +from davtelepot.utilities import json_read -default_help_messages = { - 'help_command': { - 'text': { - 'en': "Guide", - 'it': "Guida" - }, - 'reply_keyboard_button': { - 'en': "Help ๐Ÿ“–", - 'it': "Guida ๐Ÿ“–" - }, - 'description': { - 'en': "Help", - 'it': "Aiuto" - }, - 'access_denied_message': { - 'en': "Ask for authorization. If your request is accepted, send " - "/help command again to read the guide.", - 'it': "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 = bot.Role.get_user_role( - user_record=user_record - ) - return "\n".join( - [ - "/{}: {}".format( - command, - bot.get_message( - 'commands', command, 'description', - user_record=user_record, update=update, - default_message=( - details['description'] - if type(details['description']) is str - else '' - ) - ) - ) - for command, details in sorted( - bot.commands.items(), - key=lambda x:x[0] - ) - if details['description'] - and user_role.code <= bot.Role.get_user_role( - user_role_id=details['authorization_level'] - ).code - ] - ) - - -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 = bot.Role.get_user_role( - 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.code <= bot.Role.get_user_role( - user_role_id=section['auth'] - ).code - ] - 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 bot.get_message( - 'help', 'help_command', 'access_denied_message', - update=update, user_record=user_record - ) - reply_markup = get_help_buttons(bot, update, user_record) - return dict( - text=bot.get_message( - 'help', 'help_command', 'text', - update=update, user_record=user_record, - 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 = "Comandi di {bot.name}\n\n{cd}".format( - bot=bot, - cd=get_command_description(bot, update, user_record) - ) - rm = HELP_MENU_BUTTON - elif command == 'menu': - text = bot.get_message( - 'help', 'help_command', 'text', - update=update, user_record=user_record, - 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 = ( - '{s[label]}\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 +# Project modules +from .messages import default_help_messages def init(bot, help_messages=None, - help_sections_file='data/help.json', help_buttons=[]): + help_buttons=[]): """Assign parsers, commands, buttons and queries to given `bot`.""" if help_messages is None: help_messages = default_help_messages - bot.messages['help'] = help_messages - 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 ๐Ÿ“–', 'Help ๐Ÿ“–', '00help'], - reply_keyboard_button=help_messages['help_command'][ - 'reply_keyboard_button'], - show_in_keyboard=True, - description=help_messages['help_command']['description'], - 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) + davtelepot.helper.init( + bot=bot, + help_messages=help_messages, + ) diff --git a/ciclopibot/messages.py b/ciclopibot/messages.py index 12128c2..8c2840d 100644 --- a/ciclopibot/messages.py +++ b/ciclopibot/messages.py @@ -1,6 +1,68 @@ """Default messages for bot functions.""" +language_messages = { + 'language_command': { + 'name': { + 'en': "/language", + 'it': "/lingua" + }, + 'reply_keyboard_button': { + 'en': "Language ๐Ÿ—ฃ", + 'it': "Lingua ๐Ÿ—ฃ" + }, + 'alias': { + 'en': "Language ๐Ÿ—ฃ", + 'it': "Lingua ๐Ÿ—ฃ" + }, + 'description': { + 'en': "Change language settings", + 'it': "Cambia le impostazioni della lingua" + } + }, + 'language_button': { + 'description': { + 'en': "Change language settings", + 'it': "Cambia le impostazioni della lingua" + }, + 'language_set': { + 'en': "Selected language: English ๐Ÿ‡ฌ๐Ÿ‡ง", + 'it': "Lingua selezionata: Italiano ๐Ÿ‡ฎ๐Ÿ‡น" + } + }, + 'language_panel': { + 'text': { + 'en': "Choose a language", + 'it': "Seleziona una lingua" + } + } +} + +supported_languages = { + 'en': { + 'flag': '๐Ÿ‡ฌ๐Ÿ‡ง', + 'name': 'English' + }, + 'it': { + 'flag': '๐Ÿ‡ฎ๐Ÿ‡น', + 'name': 'Italiano' + } +} + default_ciclopi_messages = { + 'help': { + "name": "ciclopi", + "label": { + 'en': "CicloPi ๐Ÿšฒ", + 'it': "CicloPi ๐Ÿšฒ", + }, + "authorization_level": "everybody", + "description": { + 'en': "Use /ciclopi command to know how many available bikes and " + "free stalls are available in each station.", + 'it': "Per vedere quante bici disponibili e quanti posti liberi " + "ci sono in ogni stazione CicloPi usa il comando /ciclopi.", + }, + }, 'command': { 'description': { 'en': "CiloPi stations status", @@ -384,3 +446,51 @@ default_ciclopi_messages = { }, } } + +default_help_messages = { + 'help_command': { + 'header': { + 'en': "{bot.name} commands\n\n" + "{commands}", + 'it': "Comandi di {bot.name}\n\n" + "{commands}", + }, + 'text': { + 'en': "๐Ÿ“– {bot.name} guide\n\n" + "Welcome!\n" + "To visit a guide section, press the corresponding button.\n" + "To view all available commands, see section `Comandi`.\n\n" + "Bot author and administrator: @Davte", + 'it': "๐Ÿ“– Guida di {bot.name}\n\n" + "Benvenuto!\n" + "Per leggere una sezione della guida premi il bottone " + "corrispondente. Per conoscere tutti i comandi " + "disponibili, visita l'apposita sezione della guida " + "premendo il pulsante Comandi.\n\n" + "Autore e amministratore del bot: @Davte" + }, + 'reply_keyboard_button': { + 'en': "Help ๐Ÿ“–", + 'it': "Guida ๐Ÿ“–" + }, + 'description': { + 'en': "Help", + 'it': "Aiuto" + }, + 'access_denied_message': { + 'en': "Ask for authorization. If your request is accepted, send " + "/help command again to read the guide.", + 'it': "Chiedi di essere autorizzato: se la tua richiesta " + "verrร  accolta, ripeti il comando /help per leggere " + "il messaggio di aiuto." + }, + 'back_to_help_menu': { + 'en': "Back to guide menu ๐Ÿ“–", + 'it': "Torna al menu Guida ๐Ÿ“–", + }, + }, + 'commands_button_label': { + 'en': "Commands ๐Ÿค–", + 'it': "Comandi ๐Ÿค–", + }, +}