def _request(self, conv, segments=None, media=None, place=None): return hangouts_pb2.SendChatMessageRequest( request_header=self._client.get_request_header(), event_request_header=conv._get_event_request_header(), message_content=hangouts_pb2.MessageContent( segment=segments) if segments else None, existing_media=media, location=hangouts_pb2.Location(place=place) if place else None)
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
async def send_text(self, conversation_id: str, text: 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), message_content=hangouts.MessageContent( segment=[hangups.ChatMessageSegment(text).serialize()], ), )) return resp.created_event.event_id
async def send_message(self, segments, image_file=None, image_id=None, image_user_id=None): """Send a message to this conversation. A per-conversation lock is acquired to ensure that messages are sent in the correct order when this method is called multiple times asynchronously. Args: segments: List of :class:`.ChatMessageSegment` objects to include in the message. image_file: (optional) File-like object containing an image to be attached to the message. image_id: (optional) ID of an Picasa photo to be attached to the message. If you specify both ``image_file`` and ``image_id`` together, ``image_file`` takes precedence and ``image_id`` will be ignored. image_user_id: (optional) Picasa user ID, required only if ``image_id`` refers to an image from a different Picasa user, such as Google's sticker user. Raises: .NetworkError: If the message cannot be sent. Returns: :class:`.ConversationEvent` representing the new message. """ async with self._send_message_lock: if image_file: try: uploaded_image = await self._client.upload_image( image_file, return_uploaded_image=True) except exceptions.NetworkError as e: logger.warning('Failed to upload image: {}'.format(e)) raise image_id = uploaded_image.image_id try: request = hangouts_pb2.SendChatMessageRequest( request_header=self._client.get_request_header(), event_request_header=self._get_event_request_header(), message_content=hangouts_pb2.MessageContent( segment=[seg.serialize() for seg in segments], ), ) if image_id is not None: request.existing_media.photo.photo_id = image_id if image_user_id is not None: request.existing_media.photo.user_id = image_user_id request.existing_media.photo.is_custom_user_id = True response = await self._client.send_chat_message(request) return self._wrap_event(response.created_event) except exceptions.NetworkError as e: logger.warning('Failed to send message: {}'.format(e)) raise
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
def send_message(self, segments, image_file=None, image_id=None, image_user_id=None): """Send a message to this conversation. A per-conversation lock is acquired to ensure that messages are sent in the correct order when this method is called multiple times asynchronously. segments is a list of ChatMessageSegments to include in the message. image_file is an optional file-like object containing an image to be attached to the message. image_id is an optional ID of an Picasa photo to be attached to the message (if you specify both image_file and image_id together, image_file takes precedence and supplied image_id will be ignored). image_user_id is an optional Picasa user ID, required only if image_id refers to an image from another Picasa user (eg. Google's sticker user). Raises hangups.NetworkError if the message can not be sent. """ with (yield from self._send_message_lock): if image_file: try: image_id = yield from self._client.upload_image(image_file) except exceptions.NetworkError as e: logger.warning('Failed to upload image: {}'.format(e)) raise try: request = hangouts_pb2.SendChatMessageRequest( request_header=self._client.get_request_header(), event_request_header=self._get_event_request_header(), message_content=hangouts_pb2.MessageContent( segment=[seg.serialize() for seg in segments], ), ) if image_id is not None: request.existing_media.photo.photo_id = image_id if image_user_id is not None: request.existing_media.photo.user_id = image_user_id request.existing_media.photo.is_custom_user_id = True yield from self._client.send_chat_message(request) except exceptions.NetworkError as e: logger.warning('Failed to send message: {}'.format(e)) raise
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)