diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-11-07 01:13:26 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-11-07 01:13:26 +0300 |
commit | 2addba8bcd5555946ac509c10b5b6f1c9a1fb40a (patch) | |
tree | 2627c56e3a67e21f1a4c72f6fe8536852315f050 | |
parent | 91aff8bc85df529206124452d8bca93b2a23478d (diff) |
sensors_bot: experimental fix
-rw-r--r-- | src/home/telegram/_botlang.py | 3 | ||||
-rw-r--r-- | src/home/telegram/bot.py | 27 | ||||
-rwxr-xr-x | src/sensors_bot.py | 17 |
3 files changed, 35 insertions, 12 deletions
diff --git a/src/home/telegram/_botlang.py b/src/home/telegram/_botlang.py index 318b8b0..f5f85bb 100644 --- a/src/home/telegram/_botlang.py +++ b/src/home/telegram/_botlang.py @@ -66,6 +66,9 @@ class LangPack: else: return self.strings[lang][key] + def get_langpack(self, _lang: str) -> dict: + return self.strings[_lang] + def __call__(self, *args, **kwargs): return self.strings[self.default_lang][args[0]] diff --git a/src/home/telegram/bot.py b/src/home/telegram/bot.py index 9044b07..86ed734 100644 --- a/src/home/telegram/bot.py +++ b/src/home/telegram/bot.py @@ -76,8 +76,6 @@ def _handler_of_handler(*args, **kwargs): store=db) try: _args.insert(0, ctx) - if self: - _args.insert(0, self) f = kwargs['f'] del kwargs['f'] @@ -88,6 +86,17 @@ def _handler_of_handler(*args, **kwargs): else: return_with_context = False + if 'argument' in kwargs and kwargs['argument'] == 'message_key': + mkey = None + for k, v in lang.get_langpack(ctx.lang).items(): + if ctx.text == v: + mkey = k + break + _args.insert(0, mkey) + + if self: + _args.insert(0, self) + result = f(*_args, **kwargs) return result if not return_with_context else (result, ctx) @@ -104,16 +113,22 @@ def _handler_of_handler(*args, **kwargs): def handler(**kwargs): def inner(f): @wraps(f) - def _handler(*args, **kwargs): - return _handler_of_handler(f=f, *args, **kwargs) + def _handler(*args, **inner_kwargs): + if 'argument' in kwargs and kwargs['argument'] == 'message_key': + inner_kwargs['argument'] = 'message_key' + return _handler_of_handler(f=f, *args, **inner_kwargs) if 'message' in kwargs: _updater.dispatcher.add_handler(MessageHandler(text_filter(lang.all(kwargs['message'])), _handler), group=0) + if 'command' in kwargs: _updater.dispatcher.add_handler(CommandHandler(kwargs['command'], _handler), group=0) + if 'callback' in kwargs: _updater.dispatcher.add_handler(CallbackQueryHandler(_handler, pattern=kwargs['callback']), group=0) + return _handler + return inner @@ -447,6 +462,10 @@ def add_conversation(conv: conversation) -> None: _updater.dispatcher.add_handler(conv.get_handler(), group=0) +def add_handler(h): + _updater.dispatcher.add_handler(h, group=0) + + def start(ctx: Context): return _start_handler_ref(ctx) diff --git a/src/sensors_bot.py b/src/sensors_bot.py index dc33a57..c7fe19a 100755 --- a/src/sensors_bot.py +++ b/src/sensors_bot.py @@ -26,12 +26,6 @@ from home.api.types import ( config.load('sensors_bot') bot.initialize() -_sensor_names = [] -for k, v in config['sensors'].items(): - _sensor_names.append(k) - bot.lang.set({k: v['label_ru']}, 'ru') - bot.lang.set({k: v['label_en']}, 'en') - bot.lang.ru( start_message="Выберите датчик на клавиатуре", unknown_command="Неизвестная команда", @@ -45,7 +39,6 @@ bot.lang.ru( loading="Загрузка...", n_hrs="график за %d ч." ) - bot.lang.en( start_message="Select the sensor on the keyboard", unknown_command="Unknown command", @@ -65,7 +58,14 @@ logger = logging.getLogger(__name__) plot_hours = [3, 6, 12, 24] -@bot.handler(messages=_sensor_names) +_sensor_names = [] +for k, v in config['sensors'].items(): + _sensor_names.append(k) + bot.lang.set({k: v['label_ru']}, 'ru') + bot.lang.set({k: v['label_en']}, 'en') + + +@bot.handler(messages=_sensor_names, argument='message_key') def read_sensor(sensor: str, ctx: bot.Context) -> None: host = config['sensors'][sensor]['ip'] port = config['sensors'][sensor]['port'] @@ -177,4 +177,5 @@ def markup(ctx: Optional[bot.Context]) -> Optional[ReplyKeyboardMarkup]: if __name__ == '__main__': if 'api' in config: bot.enable_logging(BotType.SENSORS) + bot.run() |