Ejemplo n.º 1
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
Ejemplo n.º 2
0
    def _get_default_delivery_medium(self):
        """Return default DeliveryMedium to use for sending messages.

        Use the first option, or an option that's marked as the current
        default.
        """
        medium_options = (
            self._conversation.self_conversation_state.delivery_medium_option)
        try:
            default_medium = medium_options[0].delivery_medium
        except IndexError:
            logger.warning('Conversation %r has no delivery medium', self.id_)
            default_medium = hangouts_pb2.DeliveryMedium(
                medium_type=hangouts_pb2.DELIVERY_MEDIUM_BABEL)
        for medium_option in medium_options:
            if medium_option.current_default:
                default_medium = medium_option.delivery_medium
        return default_medium
Ejemplo n.º 3
0
def _send_new_conversation_welcome_message(conv_id, text):
    global last_newly_created_conv_id
    global client
    last_newly_created_conv_id = conv_id
    segments = hangups.ChatMessageSegment.from_str(text)

    request = hangouts_pb2.SendChatMessageRequest(
        request_header=client.get_request_header(),
        message_content=hangouts_pb2.MessageContent(
            segment=[seg.serialize() for seg in segments],
        ),
        event_request_header=hangouts_pb2.EventRequestHeader(
            conversation_id=hangouts_pb2.ConversationId(
                id=conv_id,
            ),
            client_generated_id=client.get_client_generated_id(),
            expected_otr=hangouts_pb2.OFF_THE_RECORD_STATUS_ON_THE_RECORD,
            delivery_medium=hangouts_pb2.DeliveryMedium(medium_type=hangouts_pb2.DELIVERY_MEDIUM_BABEL),
            event_type=hangouts_pb2.EVENT_TYPE_REGULAR_CHAT_MESSAGE,
        ),
    )
    asyncio.async(
        client.send_chat_message(request)
    ).add_done_callback(on_new_conversation_welcome_message_sent)