Ejemplo n.º 1
0
    def create_callback_query(self, json_obj):
        query_id = self._get_simple_field(json_obj, 'id')
        from_user = self.get_from(json_obj)
        message = self.get_message(json_obj)
        chat_instance = self._get_simple_field(json_obj, 'chat_instance')
        inline_message_id = self._get_simple_field(json_obj, 'inline_message_id')
        data = self._get_simple_field(json_obj, 'data')
        game_short_name = self._get_simple_field(json_obj, 'game_short_name')

        callback_query = CallbackQuery(
            id=query_id, from_user=from_user, chat_instance=chat_instance
        )

        if message:
            callback_query.message = message

        if inline_message_id:
            callback_query.inline_message_id = inline_message_id

        if data:
            callback_query.data = data

        if game_short_name:
            callback_query.game_short_name = game_short_name

        return callback_query
Ejemplo n.º 2
0
    def process_callback_query(self, callback_query: CallbackQuery) -> None:
        """Replaces the data in the callback query and the attached messages keyboard with the
        cached objects, if necessary. If the data could not be found,
        :class:`telegram.ext.InvalidCallbackData` will be inserted.
        If :attr:`callback_query.data` or :attr:`callback_query.message` is present, this also
        saves the callback queries ID in order to be able to resolve it to the stored data.

        Note:
            Also considers inserts data into the buttons of
            :attr:`telegram.Message.reply_to_message` and :attr:`telegram.Message.pinned_message`
            if necessary.

        Warning:
            *In place*, i.e. the passed :class:`telegram.CallbackQuery` will be changed!

        Args:
            callback_query (:class:`telegram.CallbackQuery`): The callback query.

        """
        with self.__lock:
            mapped = False

            if callback_query.data:
                data = callback_query.data

                # Get the cached callback data for the CallbackQuery
                keyboard_uuid, button_data = self.__get_keyboard_uuid_and_button_data(
                    data)
                callback_query.data = button_data  # type: ignore[assignment]

                # Map the callback queries ID to the keyboards UUID for later use
                if not mapped and not isinstance(button_data,
                                                 InvalidCallbackData):
                    self._callback_queries[
                        callback_query.id] = keyboard_uuid  # type: ignore
                    mapped = True

            # Get the cached callback data for the inline keyboard attached to the
            # CallbackQuery.
            if callback_query.message:
                self.__process_message(callback_query.message)
                for message in (
                        callback_query.message.pinned_message,
                        callback_query.message.reply_to_message,
                ):
                    if message:
                        self.__process_message(message)