Support having numbers in custom keyboard

This commit is contained in:
Matthias 2021-05-29 08:12:25 +02:00
parent 14df243661
commit 313567d07d
2 changed files with 15 additions and 11 deletions

View File

@ -5,6 +5,7 @@ This module manage Telegram communication
""" """
import json import json
import logging import logging
import re
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from html import escape from html import escape
from itertools import chain from itertools import chain
@ -97,24 +98,27 @@ class Telegram(RPCHandler):
# TODO: DRY! - its not good to list all valid cmds here. But otherwise # TODO: DRY! - its not good to list all valid cmds here. But otherwise
# this needs refacoring of the whole telegram module (same # this needs refacoring of the whole telegram module (same
# problem in _help()). # problem in _help()).
valid_keys: List[str] = ['/start', '/stop', '/status', '/status table', valid_keys: List[str] = [r'/start$', r'/stop$', r'/status$', r'/status table$',
'/trades', '/performance', '/daily', r'/trades$', r'/performance$', r'/daily$', r'/daily \d+$',
'/profit', '/profit day', '/profit week', r'/profit$', r'/profit \d+',
'/stats', '/count', '/locks', '/balance', r'/stats$', r'/count$', r'/locks$', r'/balance$',
'/stopbuy', '/reload_config', '/show_config', r'/stopbuy$', r'/reload_config$', r'/show_config$',
'/logs', '/whitelist', '/blacklist', '/edge', r'/logs$', r'/whitelist$', r'/blacklist$', r'/edge$',
'/help', '/version'] r'/forcebuy$', r'/help$', r'/version$']
# Create keys for generation
valid_keys_print = [k.replace('$', '') for k in valid_keys]
# custom keyboard specified in config.json # custom keyboard specified in config.json
cust_keyboard = self._config['telegram'].get('keyboard', []) cust_keyboard = self._config['telegram'].get('keyboard', [])
if cust_keyboard: if cust_keyboard:
combined = "(" + ")|(".join(valid_keys) + ")"
# check for valid shortcuts # check for valid shortcuts
invalid_keys = [b for b in chain.from_iterable(cust_keyboard) invalid_keys = [b for b in chain.from_iterable(cust_keyboard)
if b not in valid_keys] if not re.match(combined, b)]
if len(invalid_keys): if len(invalid_keys):
err_msg = ('config.telegram.keyboard: Invalid commands for ' err_msg = ('config.telegram.keyboard: Invalid commands for '
f'custom Telegram keyboard: {invalid_keys}' f'custom Telegram keyboard: {invalid_keys}'
f'\nvalid commands are: {valid_keys}') f'\nvalid commands are: {valid_keys_print}')
raise OperationalException(err_msg) raise OperationalException(err_msg)
else: else:
self._keyboard = cust_keyboard self._keyboard = cust_keyboard

View File

@ -1568,7 +1568,7 @@ def test__send_msg_keyboard(default_conf, mocker, caplog) -> None:
['/count', '/start', '/stop', '/help']] ['/count', '/start', '/stop', '/help']]
default_keyboard = ReplyKeyboardMarkup(default_keys_list) default_keyboard = ReplyKeyboardMarkup(default_keys_list)
custom_keys_list = [['/daily', '/stats', '/balance', '/profit'], custom_keys_list = [['/daily', '/stats', '/balance', '/profit', '/profit 5'],
['/count', '/start', '/reload_config', '/help']] ['/count', '/start', '/reload_config', '/help']]
custom_keyboard = ReplyKeyboardMarkup(custom_keys_list) custom_keyboard = ReplyKeyboardMarkup(custom_keys_list)
@ -1602,5 +1602,5 @@ def test__send_msg_keyboard(default_conf, mocker, caplog) -> None:
used_keyboard = bot.send_message.call_args[1]['reply_markup'] used_keyboard = bot.send_message.call_args[1]['reply_markup']
assert used_keyboard == custom_keyboard assert used_keyboard == custom_keyboard
assert log_has("using custom keyboard from config.json: " assert log_has("using custom keyboard from config.json: "
"[['/daily', '/stats', '/balance', '/profit'], ['/count', " "[['/daily', '/stats', '/balance', '/profit', '/profit 5'], ['/count', "
"'/start', '/reload_config', '/help']]", caplog) "'/start', '/reload_config', '/help']]", caplog)