From 52b9319734021f3d95df6502d166178c5c50453b Mon Sep 17 00:00:00 2001
From: Dan <14043624+delivrance@users.noreply.github.com>
Date: Mon, 17 Dec 2018 13:01:41 +0100
Subject: [PATCH] Refactor CallbackQuery
---
pyrogram/client/types/bots/callback_query.py | 79 ++++++++++++++------
1 file changed, 57 insertions(+), 22 deletions(-)
diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py
index 046981b7..61ece31e 100644
--- a/pyrogram/client/types/bots/callback_query.py
+++ b/pyrogram/client/types/bots/callback_query.py
@@ -16,10 +16,15 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
-from pyrogram.api.core import Object
+from base64 import b64encode
+from struct import pack
+
+from pyrogram.api import types
+from ..pyrogram_type import PyrogramType
+from ..user_and_chats import User
-class CallbackQuery(Object):
+class CallbackQuery(PyrogramType):
"""This object represents an incoming callback query from a callback button in an inline keyboard.
If the button that originated the query was attached to a message sent by the bot, the field message
will be present. If the button was attached to a message sent via the bot (in inline mode),
@@ -50,27 +55,57 @@ class CallbackQuery(Object):
Short name of a Game to be returned, serves as the unique identifier for the game.
"""
- ID = 0xb0700024
- def __init__(
- self,
- id: str,
- from_user,
- chat_instance: str,
- client=None,
- message=None,
- inline_message_id: str = None,
- data: bytes = None,
- game_short_name: str = None
- ):
- self._client = client
- self.id = id # string
- self.from_user = from_user # User
- self.message = message # flags.0?Message
- self.inline_message_id = inline_message_id # flags.1?string
- self.chat_instance = chat_instance # string
- self.data = data # flags.2?string
- self.game_short_name = game_short_name # flags.3?string
+ def __init__(self, *, client, raw, id: str, from_user, chat_instance: str, message=None,
+ inline_message_id: str = None, data: bytes = None, game_short_name: str = None):
+ super().__init__(client, raw)
+
+ self.id = id
+ self.from_user = from_user
+ self.message = message
+ self.inline_message_id = inline_message_id
+ self.chat_instance = chat_instance
+ self.data = data
+ self.game_short_name = game_short_name
+
+ @staticmethod
+ def parse(client, callback_query, users) -> "CallbackQuery":
+ message = None
+ inline_message_id = None
+
+ if isinstance(callback_query, types.UpdateBotCallbackQuery):
+ peer = callback_query.peer
+
+ if isinstance(peer, types.PeerUser):
+ peer_id = peer.user_id
+ elif isinstance(peer, types.PeerChat):
+ peer_id = -peer.chat_id
+ else:
+ peer_id = int("-100" + str(peer.channel_id))
+
+ message = client.get_messages(peer_id, callback_query.msg_id)
+ elif isinstance(callback_query, types.UpdateInlineBotCallbackQuery):
+ inline_message_id = b64encode(
+ pack(
+ "`.