/help command refactored using davtelepot.helper
This commit is contained in:
parent
bf3003c4b7
commit
24952b2b82
@ -1,4 +1,3 @@
|
|||||||
exclude ciclopibot/data/*
|
exclude ciclopibot/data/*
|
||||||
include ciclopibot/data/__init__.py
|
include ciclopibot/data/__init__.py
|
||||||
include ciclopibot/data/help.json
|
|
||||||
exclude ciclopibot/img*
|
exclude ciclopibot/img*
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
__author__ = "Davide Testa"
|
__author__ = "Davide Testa"
|
||||||
__email__ = "davide@davte.it"
|
__email__ = "davide@davte.it"
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "1.1.0"
|
__version__ = "1.1.1"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
@ -13,88 +13,9 @@ from davtelepot import administration_tools, authorization, languages
|
|||||||
from . import ciclopi
|
from . import ciclopi
|
||||||
from . import helper
|
from . import helper
|
||||||
from .data.passwords import bot_token
|
from .data.passwords import bot_token
|
||||||
|
from .messages import (
|
||||||
language_messages = {
|
language_messages, supported_languages
|
||||||
'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': "<b>Choose a language</b>",
|
|
||||||
'it': "<b>Seleziona una lingua</b>"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
supported_languages = {
|
|
||||||
'en': {
|
|
||||||
'flag': '🇬🇧',
|
|
||||||
'name': 'English'
|
|
||||||
},
|
|
||||||
'it': {
|
|
||||||
'flag': '🇮🇹',
|
|
||||||
'name': 'Italiano'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ciclopibot_help_messages = {
|
|
||||||
'help_command': {
|
|
||||||
'text': {
|
|
||||||
'en': "<b>📖 {bot.name} guide</b>\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': "<b>📖 Guida di {bot.name}\n\n</b>"
|
|
||||||
"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."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
path = os.path.dirname(__file__)
|
path = os.path.dirname(__file__)
|
||||||
@ -168,8 +89,6 @@ if __name__ == '__main__':
|
|||||||
ciclopi.init(bot)
|
ciclopi.init(bot)
|
||||||
helper.init(
|
helper.init(
|
||||||
bot=bot,
|
bot=bot,
|
||||||
help_messages=ciclopibot_help_messages,
|
|
||||||
help_sections_file='ciclopibot/data/help.json'
|
|
||||||
)
|
)
|
||||||
authorization.init(bot)
|
authorization.init(bot)
|
||||||
languages.init(
|
languages.init(
|
||||||
|
@ -1606,6 +1606,7 @@ def init(bot, ciclopi_messages=None, ciclopi_messages_json=None,
|
|||||||
description=(
|
description=(
|
||||||
bot.messages['ciclopi']['command']['description']
|
bot.messages['ciclopi']['command']['description']
|
||||||
),
|
),
|
||||||
|
help_section=bot.messages['ciclopi']['help'],
|
||||||
authorization_level='everybody')
|
authorization_level='everybody')
|
||||||
async def ciclopi_command(bot, update, user_record):
|
async def ciclopi_command(bot, update, user_record):
|
||||||
return await _ciclopi_command(bot, update, user_record)
|
return await _ciclopi_command(bot, update, user_record)
|
||||||
|
@ -11,7 +11,6 @@ Examples of data files
|
|||||||
local_host = '127.0.0.1'
|
local_host = '127.0.0.1'
|
||||||
port = 8080
|
port = 8080
|
||||||
```
|
```
|
||||||
- `help.json`: file where /help data are stored
|
|
||||||
- `passwords.py`: secret file where you can store your bot token
|
- `passwords.py`: secret file where you can store your bot token
|
||||||
```python
|
```python
|
||||||
bot_token = "111222333:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
bot_token = "111222333:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||||
|
@ -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."
|
|
||||||
}
|
|
||||||
]
|
|
@ -1,234 +1,19 @@
|
|||||||
"""Make a self-consistent bot help section."""
|
"""Make a self-consistent bot help section."""
|
||||||
|
|
||||||
# Third party modules
|
# Third party modules
|
||||||
from davtelepot.utilities import (
|
import davtelepot.helper
|
||||||
extract, get_cleaned_text, json_read, make_inline_keyboard,
|
from davtelepot.utilities import json_read
|
||||||
make_lines_of_buttons, make_button, MyOD
|
|
||||||
)
|
|
||||||
|
|
||||||
default_help_messages = {
|
# Project modules
|
||||||
'help_command': {
|
from .messages import default_help_messages
|
||||||
'text': {
|
|
||||||
'en': "<b>Guide</b>",
|
|
||||||
'it': "<b>Guida</b>"
|
|
||||||
},
|
|
||||||
'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 = "<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.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 = (
|
|
||||||
'<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_messages=None,
|
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`."""
|
"""Assign parsers, commands, buttons and queries to given `bot`."""
|
||||||
if help_messages is None:
|
if help_messages is None:
|
||||||
help_messages = default_help_messages
|
help_messages = default_help_messages
|
||||||
bot.messages['help'] = help_messages
|
davtelepot.helper.init(
|
||||||
bot.help_buttons = help_buttons
|
bot=bot,
|
||||||
bot.help_sections = MyOD()
|
help_messages=help_messages,
|
||||||
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)
|
|
||||||
|
@ -1,6 +1,68 @@
|
|||||||
"""Default messages for bot functions."""
|
"""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': "<b>Choose a language</b>",
|
||||||
|
'it': "<b>Seleziona una lingua</b>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
supported_languages = {
|
||||||
|
'en': {
|
||||||
|
'flag': '🇬🇧',
|
||||||
|
'name': 'English'
|
||||||
|
},
|
||||||
|
'it': {
|
||||||
|
'flag': '🇮🇹',
|
||||||
|
'name': 'Italiano'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default_ciclopi_messages = {
|
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': {
|
'command': {
|
||||||
'description': {
|
'description': {
|
||||||
'en': "CiloPi stations status",
|
'en': "CiloPi stations status",
|
||||||
@ -384,3 +446,51 @@ default_ciclopi_messages = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default_help_messages = {
|
||||||
|
'help_command': {
|
||||||
|
'header': {
|
||||||
|
'en': "<b>{bot.name} commands</b>\n\n"
|
||||||
|
"{commands}",
|
||||||
|
'it': "<b>Comandi di {bot.name}</b>\n\n"
|
||||||
|
"{commands}",
|
||||||
|
},
|
||||||
|
'text': {
|
||||||
|
'en': "<b>📖 {bot.name} guide</b>\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': "<b>📖 Guida di {bot.name}\n\n</b>"
|
||||||
|
"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 🤖",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user