|
16 | 16 | # You should have received a copy of the GNU Lesser General Public License |
17 | 17 | # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | | -from pyrogram.api.core import Object |
| 19 | +from base64 import b64encode |
| 20 | +from struct import pack |
20 | 21 |
|
| 22 | +from pyrogram.api import types |
| 23 | +from ..pyrogram_type import PyrogramType |
| 24 | +from ..user_and_chats import User |
21 | 25 |
|
22 | | -class CallbackQuery(Object): |
| 26 | + |
| 27 | +class CallbackQuery(PyrogramType): |
23 | 28 | """This object represents an incoming callback query from a callback button in an inline keyboard. |
24 | 29 | If the button that originated the query was attached to a message sent by the bot, the field message |
25 | 30 | 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): |
50 | 55 | Short name of a Game to be returned, serves as the unique identifier for the game. |
51 | 56 |
|
52 | 57 | """ |
53 | | - ID = 0xb0700024 |
54 | | - |
55 | | - def __init__( |
56 | | - self, |
57 | | - id: str, |
58 | | - from_user, |
59 | | - chat_instance: str, |
60 | | - client=None, |
61 | | - message=None, |
62 | | - inline_message_id: str = None, |
63 | | - data: bytes = None, |
64 | | - game_short_name: str = None |
65 | | - ): |
66 | | - self._client = client |
67 | | - self.id = id # string |
68 | | - self.from_user = from_user # User |
69 | | - self.message = message # flags.0?Message |
70 | | - self.inline_message_id = inline_message_id # flags.1?string |
71 | | - self.chat_instance = chat_instance # string |
72 | | - self.data = data # flags.2?string |
73 | | - self.game_short_name = game_short_name # flags.3?string |
| 58 | + |
| 59 | + def __init__(self, *, client, raw, id: str, from_user, chat_instance: str, message=None, |
| 60 | + inline_message_id: str = None, data: bytes = None, game_short_name: str = None): |
| 61 | + super().__init__(client, raw) |
| 62 | + |
| 63 | + self.id = id |
| 64 | + self.from_user = from_user |
| 65 | + self.message = message |
| 66 | + self.inline_message_id = inline_message_id |
| 67 | + self.chat_instance = chat_instance |
| 68 | + self.data = data |
| 69 | + self.game_short_name = game_short_name |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def parse(client, callback_query, users) -> "CallbackQuery": |
| 73 | + message = None |
| 74 | + inline_message_id = None |
| 75 | + |
| 76 | + if isinstance(callback_query, types.UpdateBotCallbackQuery): |
| 77 | + peer = callback_query.peer |
| 78 | + |
| 79 | + if isinstance(peer, types.PeerUser): |
| 80 | + peer_id = peer.user_id |
| 81 | + elif isinstance(peer, types.PeerChat): |
| 82 | + peer_id = -peer.chat_id |
| 83 | + else: |
| 84 | + peer_id = int("-100" + str(peer.channel_id)) |
| 85 | + |
| 86 | + message = client.get_messages(peer_id, callback_query.msg_id) |
| 87 | + elif isinstance(callback_query, types.UpdateInlineBotCallbackQuery): |
| 88 | + inline_message_id = b64encode( |
| 89 | + pack( |
| 90 | + "<iqq", |
| 91 | + callback_query.msg_id.dc_id, |
| 92 | + callback_query.msg_id.id, |
| 93 | + callback_query.msg_id.access_hash |
| 94 | + ), |
| 95 | + b"-_" |
| 96 | + ).decode().rstrip("=") |
| 97 | + |
| 98 | + return CallbackQuery( |
| 99 | + id=str(callback_query.query_id), |
| 100 | + from_user=User.parse(client, users[callback_query.user_id]), |
| 101 | + message=message, |
| 102 | + inline_message_id=inline_message_id, |
| 103 | + chat_instance=str(callback_query.chat_instance), |
| 104 | + data=callback_query.data, |
| 105 | + game_short_name=callback_query.game_short_name, |
| 106 | + client=client, |
| 107 | + raw=callback_query |
| 108 | + ) |
74 | 109 |
|
75 | 110 | def answer(self, text: str = None, show_alert: bool = None, url: str = None, cache_time: int = 0): |
76 | 111 | """Bound method *answer* of :obj:`CallbackQuery <pyrogram.CallbackQuery>`. |
|
0 commit comments