From bc8120ed4b6910b6b1f019ef62b94206b69812f4 Mon Sep 17 00:00:00 2001 From: _run Date: Sat, 30 Dec 2023 15:14:25 +0500 Subject: [PATCH] Fix issues related with reactions handlers --- telebot/apihelper.py | 2 +- telebot/asyncio_helper.py | 2 +- telebot/types.py | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index a50b61830..3558219f0 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -335,7 +335,7 @@ def set_message_reaction(token, chat_id, message_id, reaction=None, is_big=None) method_url = r'setMessageReaction' payload = {'chat_id': chat_id, 'message_id': message_id} if reaction: - payload['reaction'] = reaction + payload['reaction'] = json.dumps([r.to_dict() for r in reaction]) if is_big is not None: payload['is_big'] = is_big return _make_request(token, method_url, params=payload) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 5da6859cf..872fb591d 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -320,7 +320,7 @@ async def set_message_reaction(token, chat_id, message_id, reaction=None, is_big method_url = r'setMessageReaction' payload = {'chat_id': chat_id, 'message_id': message_id} if reaction: - payload['reaction'] = reaction + payload['reaction'] = json.dumps([r.to_dict() for r in reaction]) if is_big is not None: payload['is_big'] = is_big return await _process_request(token, method_url, params=payload) diff --git a/telebot/types.py b/telebot/types.py index f47670917..8a9930412 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -7986,7 +7986,13 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) - return cls(**obj) + # remove type + if obj['type'] == 'emoji': + del obj['type'] + return ReactionTypeEmoji(**obj) + elif obj['type'] == 'custom_emoji': + del obj['type'] + return ReactionTypeCustomEmoji(**obj) def __init__(self, type: str) -> None: self.type: str = type @@ -8028,6 +8034,8 @@ def to_dict(self) -> dict: return json_dict + + class ReactionTypeCustomEmoji(ReactionType): """ @@ -8055,6 +8063,8 @@ def to_dict(self) -> dict: return json_dict + + class MessageReactionUpdated(JsonDeserializable): """ @@ -8092,6 +8102,16 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) + + if 'user' in obj: + obj['user'] = User.de_json(obj['user']) + if 'actor_chat' in obj: + obj['actor_chat'] = Chat.de_json(obj['actor_chat']) + obj['old_reaction'] = [ReactionType.de_json(reaction) for reaction in obj['old_reaction']] + obj['new_reaction'] = [ReactionType.de_json(reaction) for reaction in obj['new_reaction']] + if 'chat' in obj: + obj['chat'] = Chat.de_json(obj['chat']) + return cls(**obj) def __init__(self, chat: Chat, message_id: int, date: int, old_reaction: List[ReactionType], new_reaction: List[ReactionType],