Exemplo n.º 1
0
 async def send_image(self, conversation_id: str, id: str) -> str:
     resp = await self.client.send_chat_message(hangouts.SendChatMessageRequest(
         request_header=self.client.get_request_header(),
         event_request_header=self._get_event_request_header(conversation_id),
         existing_media=hangouts.ExistingMedia(
             photo=hangouts.Photo(photo_id=id),
         ),
     ))
     return resp.created_event.event_id
Exemplo n.º 2
0
 async def _upload(self, attach):
     async with (await attach.get_content(self.session)) as img_content:
         # Hangups expects a file-like object with a synchronous read() method.
         # NB. The whole file is read into memory by Hangups anyway.
         # Filename must be present, else Hangups will try (and fail) to read the path.
         photo = await self._client.upload_image(
             BytesIO(await img_content.read()),
             filename=attach.title or "image.png")
     return hangouts_pb2.ExistingMedia(photo=hangouts_pb2.Photo(
         photo_id=photo))
Exemplo n.º 3
0
    def sendchatmessage(
            self, conversation_id, segments, image_id=None,
            otr_status=hangouts_pb2.OFF_THE_RECORD_STATUS_ON_THE_RECORD,
            delivery_medium=None):
        """Send a chat message to a conversation.

        conversation_id must be a valid conversation ID. segments must be a
        list of message segments to send, in pblite format.

        otr_status determines whether the message will be saved in the server's
        chat history. Note that the OTR status of the conversation is
        irrelevant, clients may send messages with whatever OTR status they
        like.

        image_id is an option ID of an image retrieved from
        Client.upload_image. If provided, the image will be attached to the
        message.

        Raises hangups.NetworkError if the request fails.
        """
        segments_pb = []
        for segment_pblite in segments:
            segment_pb = hangouts_pb2.Segment()
            pblite.decode(segment_pb, segment_pblite)
            segments_pb.append(segment_pb)
        if delivery_medium is None:
            delivery_medium = hangouts_pb2.DeliveryMedium(
                medium_type=hangouts_pb2.DELIVERY_MEDIUM_BABEL,
            )

        request = hangouts_pb2.SendChatMessageRequest(
            request_header=self._get_request_header_pb(),
            message_content=hangouts_pb2.MessageContent(
                segment=segments_pb,
            ),
            event_request_header=hangouts_pb2.EventRequestHeader(
                conversation_id=hangouts_pb2.ConversationId(
                    id=conversation_id,
                ),
                client_generated_id=self.get_client_generated_id(),
                expected_otr=otr_status,
                delivery_medium=delivery_medium,
                event_type=hangouts_pb2.EVENT_TYPE_REGULAR_CHAT_MESSAGE,
            ),
        )

        if image_id is not None:
            request.existing_media = hangouts_pb2.ExistingMedia(
                photo=hangouts_pb2.Photo(photo_id=image_id)
            )

        response = hangouts_pb2.SendChatMessageResponse()
        yield from self._pb_request('conversations/sendchatmessage', request,
                                    response)
        return response