import re import struct from .common import EventBuilder, EventCommon, name_inner_event from .. import utils from ..tl import types, functions from ..tl.custom.sendergetter import SenderGetter @name_inner_event class CallbackQuery(EventBuilder): """ Represents a callback query event (when an inline button is clicked). Note that the `chats` parameter will **not** work with normal IDs or peers if the clicked inline button comes from a "via bot" message. The `chats` parameter also supports checking against the `chat_instance` which should be used for inline callbacks. Args: data (`bytes` | `str` | `callable`, optional): If set, the inline button payload data must match this data. A UTF-8 string can also be given, a regex or a callable. For instance, to check against ``'data_1'`` and ``'data_2'`` you can use ``re.compile(b'data_')``. """ def __init__( self, chats=None, *, blacklist_chats=False, func=None, data=None): super().__init__(chats, blacklist_chats=blacklist_chats, func=func) if isinstance(data, bytes): self.data = data elif isinstance(data, str): self.data = data.encode('utf-8') elif not data or callable(data): self.data = data elif hasattr(data, 'match') and callable(data.match): if not isinstance(getattr(data, 'pattern', b''), bytes): data = re.compile(data.pattern.encode('utf-8'), data.flags & (~re.UNICODE)) self.data = data.match else: raise TypeError('Invalid data type given') @classmethod def build(cls, update): if isinstance(update, types.UpdateBotCallbackQuery): event = cls.Event(update, update.peer, update.msg_id) elif isinstance(update, types.UpdateInlineBotCallbackQuery): # See https://github.com/LonamiWebs/Telethon/pull/1005 # The long message ID is actually just msg_id + peer_id mid, pid = struct.unpack('`. This method also creates a task to `answer` the callback. .. note:: This method won't respect the previous message unlike `Message.edit `, since the message object is normally not present. """ self._client.loop.create_task(self.answer()) return await self._client.edit_message( await self.get_input_chat(), self.query.msg_id, *args, **kwargs ) async def delete(self, *args, **kwargs): """ Deletes the message. Shorthand for `telethon.client.messages.MessageMethods.delete_messages` with ``entity`` and ``message_ids`` already set. If you need to delete more than one message at once, don't use this `delete` method. Use a `telethon.client.telegramclient.TelegramClient` instance directly. This method also creates a task to `answer` the callback. """ self._client.loop.create_task(self.answer()) return await self._client.delete_messages( await self.get_input_chat(), [self.query.msg_id], *args, **kwargs )