From 2addba8bcd5555946ac509c10b5b6f1c9a1fb40a Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 7 Nov 2022 01:13:26 +0300 Subject: sensors_bot: experimental fix --- src/home/telegram/_botlang.py | 3 +++ src/home/telegram/bot.py | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src/home') 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) -- cgit v1.2.3