コード例 #1
0
    def send_message(
            self,
            peer: Peer or AsyncTask,
            text: str,
            interactive_media_groups: List[InteractiveMediaGroup] = None,
            uid: int = None) -> UUID:
        """Send text message to peer.
        Message can contain interactive media groups (buttons, selects etc.).

        :param peer: Peer or AsyncTask (in which located User or Group)
        :param text: message text (not null)
        :param interactive_media_groups: groups of interactive media components (buttons etc.)
        :param uid: send message only for user by id
        :return: UUID (message id)
        """
        peer = get_peer(peer)
        if text == '' or text is None:
            raise AttributeError('Text message must contain some text.')
        out_peer = self.manager.get_out_peer(peer)
        msg = messaging_pb2.MessageContent()
        msg.textMessage.text = text
        if interactive_media_groups is not None:
            for g in interactive_media_groups:
                media = msg.textMessage.media.add()
                g.render(media)
        request = messaging_pb2.RequestSendMessage(
            peer=out_peer,
            deduplication_id=random.randint(0, 100000000),
            message=msg,
            is_only_for_user=uid)
        return self.__send_message(request)
コード例 #2
0
    def forward(
            self,
            peer: Peer or AsyncTask,
            mids: List[UUID or AsyncTask],
            text: str = None,
            interactive_media_groups: List[InteractiveMediaGroup] = None
    ) -> UUID:
        """Forward messages to peer. Message can contain interactive media groups (buttons, selects etc.).

        :param peer: Peer or AsyncTask (in which located User or Group)
        :param mids: list of UUIDs
        :param text: message text
        :param interactive_media_groups: groups of interactive media components (buttons etc.)
        :return: UUID (message id)
        """
        peer = get_peer(peer)
        mids = get_uuids(mids)
        if text is None:
            text = ''

        out_peer, msg = self.__get_out_peer_and_message(
            peer, text, interactive_media_groups)
        request = messaging_pb2.RequestSendMessage(
            peer=out_peer,
            deduplication_id=random.randint(0, 100000000),
            message=msg,
            forward=messaging_pb2.ReferencedMessages(mids=mids),
        )
        return self.__send_message(request)
コード例 #3
0
    def reply(
            self,
            peer: Peer or AsyncTask,
            mids: List[UUID or AsyncTask],
            text: str = None,
            interactive_media_groups: List[InteractiveMediaGroup] = None
    ) -> UUID:
        """Reply messages to peer. Message can contain interactive media groups (buttons, selects etc.).

        :param peer: Peer or AsyncTask (in which located User or Group)
        :param mids: list of UUIDs
        :param text: message text
        :param interactive_media_groups: groups of interactive media components (buttons etc.)
        :return: UUID (message id)
        """
        peer = get_peer(peer)
        mids = get_uuids(mids)
        if text is None:
            text = ''

        out_peer = self.manager.get_out_peer(peer)
        msg = messaging_pb2.MessageContent()
        msg.textMessage.text = text
        if interactive_media_groups is not None:
            for g in interactive_media_groups:
                media = msg.textMessage.media.add()
                g.render(media)
        request = messaging_pb2.RequestSendMessage(
            peer=out_peer,
            deduplication_id=random.randint(0, 100000000),
            message=msg,
            reply=messaging_pb2.ReferencedMessages(mids=mids))
        return self.__send_message(request)
コード例 #4
0
    def send_image(self, peer: Peer or AsyncTask, file: str) -> UUID or None:
        """Send image as image (not as file) to peer.

        :param peer: Peer or AsyncTask (in which located User or Group)
        :param file: path to image file
        :return: UUID (message id)
        """
        peer = get_peer(peer)

        if isinstance(file, str) and not is_image(file):
            raise IOError('File is not an image.')

        location = self.internal.uploading.upload_file(file).wait()
        if location is None:
            return None
        location = location.to_api()
        out_peer = self.manager.get_out_peer(peer)
        msg = messaging_pb2.MessageContent()

        msg.documentMessage.CopyFrom(content.get_image_content(file, location))

        request = messaging_pb2.RequestSendMessage(
            peer=out_peer,
            deduplication_id=random.randint(0, 100000000),
            message=msg)

        return self.__send_message(request)
コード例 #5
0
    def send_file_by_location(self, peer, file, location):
        msg = messaging_pb2.MessageContent()

        msg.documentMessage.CopyFrom(self.get_document_content(file, location))

        return self.messaging.SendMessage(
            messaging_pb2.RequestSendMessage(peer=peer,
                                             deduplication_id=random.randint(
                                                 0, 100000000),
                                             message=msg))
コード例 #6
0
 def send_message(self, peer, text, iteractive_media_groups=None):
     outpeer = self.manager.get_outpeer(peer)
     msg = messaging_pb2.MessageContent()
     msg.textMessage.text = text
     if iteractive_media_groups is not None:
         for g in iteractive_media_groups:
             media = msg.textMessage.media.add()
             g.render(media)
     return self.internal.messaging.SendMessage(
         messaging_pb2.RequestSendMessage(peer=outpeer,
                                          rid=int(time.time()),
                                          message=msg)).mid
コード例 #7
0
ファイル: messaging.py プロジェクト: brailovskiy/grpc-test
    def send_file(self, user, target_outpeer, file):
        """Send file to current peer.

        :param peer: receiver's peer
        :param file: path to file
        :return: value of SendMessage response object
        """
        location = self.upload_file(user, file)
        msg = messaging_pb2.MessageContent()

        msg.documentMessage.CopyFrom(
            content.get_document_content(file, location))

        return user.messaging.SendMessage(
            messaging_pb2.RequestSendMessage(peer=target_outpeer,
                                             deduplication_id=random.randint(
                                                 0, 100000000),
                                             message=msg))
コード例 #8
0
    def send_file(self, peer, file):
        """Send file to current peer.

        :param peer: receiver's peer
        :param file: path to file
        :return: mid value of SendMessage response object
        """

        location = self.internal.uploading.upload_file(file)
        outpeer = self.manager.get_outpeer(peer)
        msg = messaging_pb2.MessageContent()

        msg.documentMessage.CopyFrom(
            content.get_document_content(file, location))

        return self.internal.messaging.SendMessage(
            messaging_pb2.RequestSendMessage(peer=outpeer,
                                             rid=int(time.time()),
                                             message=msg)).mid
コード例 #9
0
    def send_media(self, peer: Peer or AsyncTask,
                   medias: List[MessageMedia]) -> UUID:
        """Send media to peer.

        :param peer: Peer or AsyncTask (in which located User or Group)
        :param medias: medias (list of MessageMedias)
        :return: UUID (message id)
        """
        peer = get_peer(peer)
        medias = self.__get_medias(medias)
        out_peer = self.manager.get_out_peer(peer)
        text_message = messaging_pb2.TextMessage()
        for media in medias:
            text_message.media.append(media)
        msg = messaging_pb2.MessageContent(textMessage=text_message)
        request = messaging_pb2.RequestSendMessage(
            peer=out_peer,
            deduplication_id=random.randint(0, 100000000),
            message=msg)
        return self.__send_message(request)
コード例 #10
0
    def send_image(self, peer, file):
        """Send image as image (not as file) to current peer.

        :param peer: receiver's peer
        :param file: path to image file
        :return: mid value of SendMessage response object
        """

        if imghdr.what(file) not in ['gif', 'jpeg', 'png', 'bmp']:
            raise IOError('File is not an image.')

        location = self.internal.uploading.upload_file(file)
        outpeer = self.manager.get_outpeer(peer)
        msg = messaging_pb2.MessageContent()

        msg.documentMessage.CopyFrom(content.get_image_content(file, location))

        return self.internal.messaging.SendMessage(
            messaging_pb2.RequestSendMessage(peer=outpeer,
                                             rid=int(time.time()),
                                             message=msg)).mid
コード例 #11
0
    def send_message(self, peer, text, interactive_media_groups=None):
        """Send text message to current peer. Message can contain interactive media groups (buttons, selects etc.).

        :param peer: receiver's peer
        :param text: message text (not null)
        :param interactive_media_groups: groups of interactive media components (buttons etc.)
        :return: mid value of SendMessage response object
        """

        if text == '' or text is None:
            raise AttributeError('Text message must contain some text.')

        outpeer = self.manager.get_outpeer(peer)
        msg = messaging_pb2.MessageContent()
        msg.textMessage.text = text
        if interactive_media_groups is not None:
            for g in interactive_media_groups:
                media = msg.textMessage.media.add()
                g.render(media)
        return self.internal.messaging.SendMessage(
            messaging_pb2.RequestSendMessage(peer=outpeer,
                                             rid=int(time.time()),
                                             message=msg)).mid