From 8f20c9b825cabab7a3f0f5dd2cfe000cc7f72c28 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Tue, 28 Jun 2022 03:22:30 +0300 Subject: polaris pwk 1725cgld full support - significant improvements, correctnesses and stability fixes in protocol implementation - correct handling of device appearances and disappearances - flawlessly functioning telegram bot that re-renders kettle's state (temperature and other) in real time --- src/home/bot/wrapper.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src/home/bot/wrapper.py') 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 # -- cgit v1.2.3