"""Example of using hangups to query presence of a user.""" import asyncio import hangups from common import run_example @asyncio.coroutine def query_presence(client, args): request = hangups.hangouts_pb2.QueryPresenceRequest( request_header=client.get_request_header(), participant_id=[ hangups.hangouts_pb2.ParticipantId(gaia_id=args.user_id), ], field_mask=[ hangups.hangouts_pb2.FIELD_MASK_REACHABLE, hangups.hangouts_pb2.FIELD_MASK_AVAILABLE, hangups.hangouts_pb2.FIELD_MASK_MOOD, hangups.hangouts_pb2.FIELD_MASK_DEVICE, hangups.hangouts_pb2.FIELD_MASK_LAST_SEEN, ], ) res = yield from client.query_presence(request) print(res) if __name__ == '__main__': run_example(query_presence, '--user-id')
return # Load events from the server all_events = await _get_events(conversation) # Load events cached in the conversation all_events_cached = await _get_events(conversation) assert ([event.timestamp for event in all_events ] == [event.timestamp for event in all_events_cached]) # Print the events oldest to newest for event in all_events: print('{} {} {!r}'.format(event.timestamp.strftime('%c'), event.__class__.__name__, getattr(event, 'text'))) async def _get_events(conversation): all_events = [] # newest-first event_id = None for _ in range(MAX_REQUESTS): events = await conversation.get_events(event_id=event_id, max_events=MAX_EVENTS) event_id = events[0].id_ # oldest event all_events.extend(reversed(events)) return list(reversed(all_events)) # oldest-first if __name__ == '__main__': run_example(get_events, '--conversation-id')
"""Example of using hangups to query presence of a user.""" import hangups from common import run_example async def query_presence(client, args): request = hangups.hangouts_pb2.QueryPresenceRequest( request_header=client.get_request_header(), participant_id=[ hangups.hangouts_pb2.ParticipantId(gaia_id=args.user_id), ], field_mask=[ hangups.hangouts_pb2.FIELD_MASK_REACHABLE, hangups.hangouts_pb2.FIELD_MASK_AVAILABLE, hangups.hangouts_pb2.FIELD_MASK_MOOD, hangups.hangouts_pb2.FIELD_MASK_DEVICE, hangups.hangouts_pb2.FIELD_MASK_LAST_SEEN, ], ) res = await client.query_presence(request) print(res) if __name__ == '__main__': run_example(query_presence, '--user-id')
"""Example of using hangups to set presence settings.""" import hangups from common import run_example async def set_presence(client, args): request = hangups.hangouts_pb2.SetPresenceRequest( request_header=client.get_request_header(), status_message_setting=hangups.hangouts_pb2. StatusMessageSetting(status_message=[ hangups.hangouts_pb2.ChatMessageSpec( segment=[hangups.ChatMessageSegment(args.status).serialize()]) ]), ) await client.set_presence(request) if __name__ == "__main__": run_example(set_presence, "--status")
# Load events cached in the conversation all_events_cached = await _get_events(conversation) assert ( [event.timestamp for event in all_events] == [event.timestamp for event in all_events_cached] ) # Print the events oldest to newest for event in all_events: print('{} {} {!r}'.format( event.timestamp.strftime('%c'), event.__class__.__name__, getattr(event, 'text') )) async def _get_events(conversation): all_events = [] # newest-first event_id = None for _ in range(MAX_REQUESTS): events = await conversation.get_events( event_id=event_id, max_events=MAX_EVENTS ) event_id = events[0].id_ # oldest event all_events.extend(reversed(events)) return list(reversed(all_events)) # oldest-first if __name__ == '__main__': run_example(get_events, '--conversation-id')
"""Example of using hangups to create a new group conversation.""" import hangups from common import run_example async def send_message(client, args): request = hangups.hangouts_pb2.CreateConversationRequest( request_header=client.get_request_header(), type=hangups.hangouts_pb2.CONVERSATION_TYPE_GROUP, client_generated_id=client.get_client_generated_id(), invitee_id=[ hangups.hangouts_pb2.InviteeID(gaia_id=gaia_id) for gaia_id in args.gaia_ids.split(",") ], name=args.conversation_name) res = await client.create_conversation(request) print(res) # --gaia-ids: list of participant gaia_id, separated by comma (excluding self) # --conversation-name: the group conversation name to specify/customize if __name__ == '__main__': run_example(send_message, '--gaia-ids', '--conversation-name')
"""Example of using hangups to get information about your account.""" import asyncio import hangups from common import run_example async def get_self_info(client, _): request = hangups.hangouts_pb2.GetSelfInfoRequest( request_header=client.get_request_header(), ) res = await client.get_self_info(request) print(res) if __name__ == "__main__": run_example(get_self_info)
"""Example of using hangups to get conversation messages.""" import asyncio import hangups from common import run_example @asyncio.coroutine def get_conversation(client, args): request = hangups.hangouts_pb2.GetConversationRequest( request_header=client.get_request_header(), conversation_spec=hangups.hangouts_pb2.ConversationSpec( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id), ), include_event=True, max_events_per_conversation=10, ) res = yield from client.get_conversation(request) print(res) if __name__ == '__main__': run_example(get_conversation, '--conversation-id')
Uses the high-level hangups API. """ import asyncio import hangups from common import run_example async def receive_messages(client, args): print('loading conversation list...') user_list, conv_list = ( await hangups.build_user_conversation_list(client) ) conv_list.on_event.add_observer(on_event) print('waiting for chat messages...') while True: await asyncio.sleep(1) def on_event(conv_event): if isinstance(conv_event, hangups.ChatMessageEvent): print('received chat message: {!r}'.format(conv_event.text)) if __name__ == '__main__': run_example(receive_messages)
from common import run_example @asyncio.coroutine def send_map_location(client, args): request = hangups.hangouts_pb2.SendChatMessageRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id), client_generated_id=client.get_client_generated_id(), ), location=hangups.hangouts_pb2. Location(place=hangups.hangouts_pb2.Place( name=args.name, address=hangups.hangouts_pb2.EmbedItem( postal_address=hangups.hangouts_pb2.EmbedItem.PostalAddress( street_address=args.address), ), geo=hangups.hangouts_pb2.EmbedItem( geo_coordinates=hangups.hangouts_pb2.EmbedItem.GeoCoordinates( latitude=float(args.latitude), longitude=float(args.longitude)), ), ), ), ) yield from client.send_chat_message(request) if __name__ == '__main__': run_example(send_map_location, '--conversation-id', '--latitude', '--longitude', '--name', '--address')
@asyncio.coroutine def lookup_entities(client, args): """Search for entities by phone number, email, or gaia_id.""" lookup_spec = _get_lookup_spec(args.entity_identifier) request = hangups.hangouts_pb2.GetEntityByIdRequest( request_header=client.get_request_header(), batch_lookup_spec=[lookup_spec], ) res = yield from client.get_entity_by_id(request) # Print the list of entities in the response. for entity_result in res.entity_result: for entity in entity_result.entity: print(entity) def _get_lookup_spec(identifier): """Return EntityLookupSpec from phone number, email address, or gaia ID.""" if identifier.startswith('+'): return hangups.hangouts_pb2.EntityLookupSpec( phone=identifier, create_offnetwork_gaia=True) elif '@' in identifier: return hangups.hangouts_pb2.EntityLookupSpec( email=identifier, create_offnetwork_gaia=True) else: return hangups.hangouts_pb2.EntityLookupSpec(gaia_id=identifier) if __name__ == '__main__': run_example(lookup_entities, '--entity-identifier')
"""Example of using hangups to send a chat message to a conversation.""" import hangups from common import run_example async def send_message(client, args): request = hangups.hangouts_pb2.SendChatMessageRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), client_generated_id=client.get_client_generated_id(), ), message_content=hangups.hangouts_pb2.MessageContent( segment=[ hangups.ChatMessageSegment(args.message_text).serialize() ], ), ) await client.send_chat_message(request) if __name__ == '__main__': run_example(send_message, '--conversation-id', '--message-text')
"""Example of using hangups to get conversation messages.""" import hangups from common import run_example async def get_conversation(client, args): request = hangups.hangouts_pb2.GetConversationRequest( request_header=client.get_request_header(), conversation_spec=hangups.hangouts_pb2.ConversationSpec( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), ), include_event=True, max_events_per_conversation=10, ) res = await client.get_conversation(request) print(res) if __name__ == '__main__': run_example(get_conversation, '--conversation-id')
async def enable_group_link_sharing(client, args): request = hangups.hangouts_pb2.SetGroupLinkSharingEnabledRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), client_generated_id=client.get_client_generated_id(), ), group_link_sharing_status=( hangups.hangouts_pb2.GROUP_LINK_SHARING_STATUS_ON ), ) await client.set_group_link_sharing_enabled(request) print('enabled group link sharing for conversation {}'.format( args.conversation_id )) request = hangups.hangouts_pb2.GetGroupConversationUrlRequest( request_header=client.get_request_header(), conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id, ) ) response = await client.get_group_conversation_url(request) print(response.group_conversation_url) if __name__ == '__main__': run_example(enable_group_link_sharing, '--conversation-id')
def enable_group_link_sharing(client, args): request = hangups.hangouts_pb2.SetGroupLinkSharingEnabledRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), client_generated_id=client.get_client_generated_id(), ), group_link_sharing_status=( hangups.hangouts_pb2.GROUP_LINK_SHARING_STATUS_ON ), ) yield from client.set_group_link_sharing_enabled(request) print('enabled group link sharing for conversation {}'.format( args.conversation_id )) request = hangups.hangouts_pb2.GetGroupConversationUrlRequest( request_header=client.get_request_header(), conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id, ) ) response = yield from client.get_group_conversation_url(request) print(response.group_conversation_url) if __name__ == '__main__': run_example(enable_group_link_sharing, '--conversation-id')
"""Example of using hangups to retrieve suggested contacts.""" import hangups from common import run_example async def retrieve_suggested_contacts(client, _): request = hangups.hangouts_pb2.GetSuggestedEntitiesRequest( request_header=client.get_request_header(), max_count=100, ) res = await client.get_suggested_entities(request) # Print the list of entities in the response. for entity in res.entity: print('{} ({})'.format(entity.properties.display_name, entity.id.gaia_id)) if __name__ == '__main__': run_example(retrieve_suggested_contacts)
conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), client_generated_id=client.get_client_generated_id(), ), location=hangups.hangouts_pb2.Location( place=hangups.hangouts_pb2.Place( name=args.name, address=hangups.hangouts_pb2.EmbedItem( postal_address=hangups.hangouts_pb2.EmbedItem.PostalAddress( street_address=args.address ), ), geo=hangups.hangouts_pb2.EmbedItem( geo_coordinates=hangups.hangouts_pb2.EmbedItem.GeoCoordinates( latitude=float(args.latitude), longitude=float(args.longitude) ), ), ), ), ) await client.send_chat_message(request) if __name__ == '__main__': run_example( send_map_location, '--conversation-id', '--latitude', '--longitude', '--name', '--address' )
""" Created on Tue Jun 18 09:50:05 2019 @author: brain """ """Example of using hangups to send a chat message to a conversation.""" import hangups from common import run_example async def send_message(client, args): request = hangups.hangouts_pb2.SendChatMessageRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id), client_generated_id=client.get_client_generated_id(), ), message_content=hangups.hangouts_pb2.MessageContent(segment=[ hangups.ChatMessageSegment( args.message_text, link_target=args.message_text).serialize() ], ), ) await client.send_chat_message(request) if __name__ == '__main__': run_example(send_message, '--conversation-id', '--message-text')
from common import run_example async def upload_image(client, args): # Upload image to obtain image_id: image_file = open(args.image, 'rb') uploaded_image = await client.upload_image( image_file, return_uploaded_image=True ) # Send a chat message referencing the uploaded image_id: request = hangups.hangouts_pb2.SendChatMessageRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), client_generated_id=client.get_client_generated_id(), ), existing_media=hangups.hangouts_pb2.ExistingMedia( photo=hangups.hangouts_pb2.Photo( photo_id=uploaded_image.image_id, ), ), ) await client.send_chat_message(request) if __name__ == '__main__': run_example(upload_image, '--conversation-id', '--image')
"""Example of using hangups to set focus to a conversation.""" import hangups from common import run_example async def set_focus(client, args): request = hangups.hangouts_pb2.SetFocusRequest( request_header=client.get_request_header(), conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id), type=hangups.hangouts_pb2.FOCUS_TYPE_FOCUSED, timeout_secs=int(args.timeout_secs), ) await client.set_focus(request) if __name__ == '__main__': run_example(set_focus, '--conversation-id', '--timeout-secs')
import asyncio import hangups from common import run_example @asyncio.coroutine def sync_recent_conversations(client, _): user_list, conversation_list = ( yield from hangups.build_user_conversation_list(client) ) all_users = user_list.get_all() all_conversations = conversation_list.get_all(include_archived=True) print('{} known users'.format(len(all_users))) for user in all_users: print(' {}: {}'.format(user.full_name, user.id_.gaia_id)) print('{} known conversations'.format(len(all_conversations))) for conversation in all_conversations: if conversation.name: name = conversation.name else: name = 'Unnamed conversation ({})'.format(conversation.id_) print(' {}'.format(name)) if __name__ == '__main__': run_example(sync_recent_conversations)
"""Example of using hangups to receive chat messages. Uses the high-level hangups API. """ import asyncio import hangups from common import run_example async def receive_messages(client, args): print('loading conversation list...') user_list, conv_list = (await hangups.build_user_conversation_list(client)) conv_list.on_event.add_observer(on_event) print('waiting for chat messages...') while True: await asyncio.sleep(1) def on_event(conv_event): if isinstance(conv_event, hangups.ChatMessageEvent): print('received chat message: {!r}'.format(conv_event.text)) if __name__ == '__main__': run_example(receive_messages)
"""Example of using hangups to retrieve suggested contacts.""" import hangups from common import run_example async def retrieve_suggested_contacts(client, _): request = hangups.hangouts_pb2.GetSuggestedEntitiesRequest( request_header=client.get_request_header(), max_count=100, ) res = await client.get_suggested_entities(request) # Print the list of entities in the response. for entity in res.entity: print('{} ({})'.format( entity.properties.display_name, entity.id.gaia_id )) if __name__ == '__main__': run_example(retrieve_suggested_contacts)
"""Example of using hangups to create a new group conversation.""" import hangups from common import run_example async def send_message(client, args): request = hangups.hangouts_pb2.CreateConversationRequest( request_header=client.get_request_header(), type=hangups.hangouts_pb2.CONVERSATION_TYPE_GROUP, client_generated_id=client.get_client_generated_id(), invitee_id=[ hangups.hangouts_pb2.InviteeID( gaia_id=gaia_id ) for gaia_id in args.gaia_ids.split(",") ], name=args.conversation_name ) res = await client.create_conversation(request) print(res) # --gaia-ids: list of participant gaia_id, separated by comma (excluding self) # --conversation-name: the group conversation name to specify/customize if __name__ == '__main__': run_example(send_message, '--gaia-ids', '--conversation-name')
"""Search for entities by phone number, email, or gaia_id.""" lookup_spec = _get_lookup_spec(args.entity_identifier) request = hangups.hangouts_pb2.GetEntityByIdRequest( request_header=client.get_request_header(), batch_lookup_spec=[lookup_spec], ) res = await client.get_entity_by_id(request) # Print the list of entities in the response. for entity_result in res.entity_result: for entity in entity_result.entity: print(entity) def _get_lookup_spec(identifier): """Return EntityLookupSpec from phone number, email address, or gaia ID.""" if identifier.startswith('+'): return hangups.hangouts_pb2.EntityLookupSpec( phone=identifier, create_offnetwork_gaia=True ) elif '@' in identifier: return hangups.hangouts_pb2.EntityLookupSpec( email=identifier, create_offnetwork_gaia=True ) else: return hangups.hangouts_pb2.EntityLookupSpec(gaia_id=identifier) if __name__ == '__main__': run_example(lookup_entities, '--entity-identifier')
sync_filter=[hangups.hangouts_pb2.SYNC_FILTER_INBOX], ) res = await client.sync_recent_conversations(request) # Sort the returned conversations by recency. conv_states = sorted( res.conversation_state, key=lambda conv_state: ( conv_state.conversation.self_conversation_state.sort_timestamp ), reverse=True ) # Print the list of conversations and their participants. for conv_state in conv_states: if conv_state.conversation.name: conv_name = repr(conv_state.conversation.name) else: conv_name = 'Unnamed Hangout' print(' - {} ({})'.format(conv_name, conv_state.conversation_id.id)) for participant in conv_state.conversation.participant_data: if participant.fallback_name: name = repr(participant.fallback_name) else: name = 'No fallback name' print(' - {} ({})'.format(name, participant.id.gaia_id)) if __name__ == '__main__': run_example(sync_recent_conversations)
"""Example of using hangups to upload an image.""" import hangups from common import run_example async def upload_image(client, args): # Upload image to obtain image_id: image_file = open(args.image, 'rb') uploaded_image = await client.upload_image(image_file, return_uploaded_image=True) # Send a chat message referencing the uploaded image_id: request = hangups.hangouts_pb2.SendChatMessageRequest( request_header=client.get_request_header(), event_request_header=hangups.hangouts_pb2.EventRequestHeader( conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id), client_generated_id=client.get_client_generated_id(), ), existing_media=hangups.hangouts_pb2.ExistingMedia( photo=hangups.hangouts_pb2.Photo( photo_id=uploaded_image.image_id, ), ), ) await client.send_chat_message(request) if __name__ == '__main__': run_example(upload_image, '--conversation-id', '--image')
"""Example of using hangups to set focus to a conversation.""" import hangups from common import run_example async def set_focus(client, args): request = hangups.hangouts_pb2.SetFocusRequest( request_header=client.get_request_header(), conversation_id=hangups.hangouts_pb2.ConversationId( id=args.conversation_id ), type=hangups.hangouts_pb2.FOCUS_TYPE_FOCUSED, timeout_secs=int(args.timeout_secs), ) await client.set_focus(request) if __name__ == '__main__': run_example(set_focus, '--conversation-id', '--timeout-secs')