diff options
Diffstat (limited to 'src/home/bot')
-rw-r--r-- | src/home/bot/__init__.py | 2 | ||||
-rw-r--r-- | src/home/bot/lang.py | 13 | ||||
-rw-r--r-- | src/home/bot/wrapper.py | 34 |
3 files changed, 40 insertions, 9 deletions
diff --git a/src/home/bot/__init__.py b/src/home/bot/__init__.py index 5e68af7..0d93af3 100644 --- a/src/home/bot/__init__.py +++ b/src/home/bot/__init__.py @@ -1,6 +1,6 @@ from .reporting import ReportingHelper from .lang import LangPack -from .wrapper import Wrapper, Context, text_filter +from .wrapper import Wrapper, Context, text_filter, handlermethod from .store import Store from .errors import * from .util import command_usage, user_any_name
\ No newline at end of file diff --git a/src/home/bot/lang.py b/src/home/bot/lang.py index 2f10358..624c748 100644 --- a/src/home/bot/lang.py +++ b/src/home/bot/lang.py @@ -1,6 +1,8 @@ +from __future__ import annotations + import logging -from typing import Union, Optional +from typing import Union, Optional, List, Dict logger = logging.getLogger(__name__) @@ -24,7 +26,7 @@ class LangStrings(dict): class LangPack: - strings: dict[str, LangStrings[str, str]] + strings: Dict[str, LangStrings[str, str]] default_lang: str def __init__(self): @@ -57,11 +59,14 @@ class LangPack: return result @property - def languages(self) -> list[str]: + def languages(self) -> List[str]: return list(self.strings.keys()) def get(self, key: str, lang: str, *args) -> str: - return self.strings[lang][key] % args + if args: + return self.strings[lang][key] % args + else: + return self.strings[lang][key] def __call__(self, *args, **kwargs): return self.strings[self.default_lang][args[0]] diff --git a/src/home/bot/wrapper.py b/src/home/bot/wrapper.py index 8ebde4f..5f399ce 100644 --- a/src/home/bot/wrapper.py +++ b/src/home/bot/wrapper.py @@ -8,6 +8,7 @@ from telegram import ( ReplyKeyboardMarkup, CallbackQuery, User, + Message, ) from telegram.ext import ( Updater, @@ -22,7 +23,7 @@ from telegram.ext import ( ) from telegram.error import TimedOut from ..config import config -from typing import Optional, Union +from typing import Optional, Union, List, Tuple from .store import Store from .lang import LangPack from ..api.types import BotType @@ -110,7 +111,7 @@ class Context: kwargs = dict(parse_mode=ParseMode.HTML) if not isinstance(markup, IgnoreMarkup): kwargs['reply_markup'] = markup - self._update.message.reply_text(text, **kwargs) + return self._update.message.reply_text(text, **kwargs) def reply_exc(self, e: Exception) -> None: self.reply(exc2text(e)) @@ -133,7 +134,7 @@ class Context: return self._update.callback_query @property - def args(self) -> Optional[list[str]]: + def args(self) -> Optional[List[str]]: return self._callback_context.args @property @@ -157,6 +158,25 @@ class Context: return self._update.callback_query and self._update.callback_query.data and self._update.callback_query.data != '' +def handlermethod(f: callable): + def _handler(self, update: Update, context: CallbackContext, *args, **kwargs): + ctx = Context(update, + callback_context=context, + markup_getter=self.markup, + lang=self.lang, + store=self.store) + try: + return f(self, ctx, *args, **kwargs) + except Exception as e: + if not self.exception_handler(e, ctx) and not isinstance(e, TimedOut): + logger.exception(e) + if not ctx.is_callback_context(): + ctx.reply_exc(e) + else: + self.notify_user(ctx.user_id, exc2text(e)) + return _handler + + class Wrapper: store: Optional[Store] updater: Updater @@ -252,7 +272,7 @@ class Wrapper: def exception_handler(self, e: Exception, ctx: Context) -> Optional[bool]: pass - def notify_all(self, text_getter: callable, exclude: tuple[int] = ()) -> None: + def notify_all(self, text_getter: callable, exclude: Tuple[int] = ()) -> None: if 'notify_users' not in config['bot']: logger.error('notify_all() called but no notify_users directive found in the config') return @@ -280,6 +300,12 @@ class Wrapper: def send_file(self, user_id, **kwargs): self.updater.bot.send_document(chat_id=user_id, **kwargs) + def edit_message_text(self, user_id, message_id, *args, **kwargs): + self.updater.bot.edit_message_text(chat_id=user_id, message_id=message_id, parse_mode='HTML', *args, **kwargs) + + def delete_message(self, user_id, message_id): + self.updater.bot.delete_message(chat_id=user_id, message_id=message_id) + # # Language Selection # |