Config command implemented, allowing administrators to add variables to configuration files

This commit is contained in:
2023-07-25 16:25:07 +02:00
parent 173783e154
commit 9094955812
3 changed files with 102 additions and 2 deletions

View File

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.9.1" __version__ = "2.9.2"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@@ -29,13 +29,17 @@ from davtelepot.utilities import (
async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text, async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text,
get_user, clean_html_string, line_drawing_unordered_list, make_button, get_user, clean_html_string, line_drawing_unordered_list, make_button,
make_inline_keyboard, remove_html_tags, send_part_of_text_file, make_inline_keyboard, remove_html_tags, send_part_of_text_file,
send_csv_file, make_lines_of_buttons send_csv_file, make_lines_of_buttons, join_path
) )
# Use this parameter in SQL `LIMIT x OFFSET y` clauses # Use this parameter in SQL `LIMIT x OFFSET y` clauses
rows_number_limit = 10 rows_number_limit = 10
command_description_parser = re.compile(r'(?P<command>\w+)(\s?-\s?(?P<description>.*))?') command_description_parser = re.compile(r'(?P<command>\w+)(\s?-\s?(?P<description>.*))?')
variable_regex = re.compile(r"(?P<name>[a-zA-Z][\w]*)\s*=\s*"
r"(?P<value>[\d.,]+|True|False|"
r"'[^']*'|"
r"\"[^\"]*\")")
async def _forward_to(update, async def _forward_to(update,
@@ -1805,6 +1809,40 @@ async def _father_button(bot: Bot, user_record: OrderedDict,
return result return result
async def _config_command(bot: Bot, update: dict,
user_record: dict, language: str):
text = get_cleaned_text(
update,
bot,
['config']
)
if not text:
return bot.get_message('admin', 'config_command',
'instructions',
user_record=user_record,
language=language)
match = variable_regex.match(text)
if not match:
return bot.get_message('admin', 'config_command',
'invalid_input',
user_record=user_record,
language=language)
match = match.groupdict()
if (',' in match['value']
and not match['value'].startswith('\'')
and not match['value'].startswith('"')):
match['value'] = match['value'].replace(',', '.')
new_variable = f"{match['name']} = {match['value']}"
with open(join_path(bot.path, 'data', 'config.py'),
'a') as configuration_file:
configuration_file.write(f"{new_variable}\n")
return bot.get_message('admin', 'config_command',
'success',
new_variable=new_variable,
user_record=user_record,
language=language)
def init(telegram_bot: Bot, def init(telegram_bot: Bot,
talk_messages: dict = None, talk_messages: dict = None,
admin_messages: dict = None, admin_messages: dict = None,
@@ -2019,3 +2057,18 @@ def init(telegram_bot: Bot,
update=update, update=update,
user_record=user_record, user_record=user_record,
language=language) language=language)
@telegram_bot.command(command='/config',
aliases=[],
**{key: admin_messages['config_command'][key]
for key in ('reply_keyboard_button',
'description',
'help_section',)
},
show_in_keyboard=False,
authorization_level='admin')
async def config_command(bot, update, user_record, language):
return await _config_command(bot=bot,
update=update,
user_record=user_record,
language=language)

View File

@@ -36,6 +36,53 @@ default_admin_messages = {
'it': "annulla", 'it': "annulla",
}, },
}, },
'config_command': {
'description': {
'en': "Add a variable to the configuration file",
'it': "Aggiungi una variabile al file di configurazione",
},
'help_section': None,
'instructions': {
'en': "<b>Config 🔧</b>\n\n"
"<i>Send me a new configuration variable, for example:</i>\n"
"<code>variable = 2</code>",
'it': "<b>Configurazione 🔧</b>\n\n"
"<i>Mandami una variabile da aggiungere al file di "
"configurazione, per esempio:</i>\n"
"<code>variabile = 2</code>",
},
'invalid_input': {
'en': "<b>❌ Invalid input 🔧</b>\n\n"
"<i>Send me a new configuration variable, for example:</i>\n"
"<code>int_var = 2\n"
"str_var = 'test'\n"
"bool_var = False\n"
"float_var = 3.5</code>",
'it': "<b>❌ Configurazione scorretta 🔧</b>\n\n"
"<i>Mandami una variabile da aggiungere al file di "
"configurazione, per esempio:</i>\n"
"<code>var_int = 2\n"
"var_str = 'test'\n"
"var_bool = False\n"
"var_float = 3.5</code>",
},
'reply_keyboard_button': {
'en': "Config 🔧",
'it': "Configurazione 🔧",
},
'success': {
'en': "<b>✔️ Configuration updated 🔧</b>\n\n"
"The following variable has been added to the configuration "
"file:\n"
"<code>{new_variable}</code>\n\n"
"/restart to apply",
'it': "<b>✔️ Configurazione aggiornata 🔧</b>\n\n"
"La seguente variabile è stata aggiunta al file di "
"configurazione:\n"
"<code>{new_variable}</code>\n\n"
"Per rendere effettive le modifiche fai /restart",
},
},
'confirm': { 'confirm': {
'en': "🔄 Click again to confirm", 'en': "🔄 Click again to confirm",
'it': "🔄 Clicka di nuovo per confermare", 'it': "🔄 Clicka di nuovo per confermare",