High-Level APIs¶
This page documents high-level APIs that expose some of hangups’ low-level functionality in a simpler way.
Conversation List¶
-
coroutine
hangups.build_user_conversation_list(client)[source]¶ Build
UserListandConversationList.This method requests data necessary to build the list of conversations and users. Users that are not in the contact list but are participating in a conversation will also be retrieved.
Parameters: client (Client) – Connected client. Returns: Tuple of built objects. Return type: ( UserList,ConversationList)
-
class
hangups.ConversationList(client, conv_states, user_list, sync_timestamp)[source]¶ Maintains a list of the user’s conversations.
Using
build_user_conversation_list()to initialize this class is recommended.Parameters: - client – The connected
Client. - conv_states – List of
ConversationStatemessages used to initialize the list of conversations. - user_list –
UserListobject. - sync_timestamp (datetime.datetime) – The time when
conv_stateswas synced.
-
on_event= None¶ Eventfired when an event occurs in any conversation.Parameters: conv_event – ConversationEventthat occurred.
-
on_typing= None¶ Eventfired when a users starts or stops typing in any conversation.Parameters: typing_message – TypingStatusMessagethat occurred.
-
on_watermark_notification= None¶ Eventfired when a watermark (read timestamp) is updated for any conversation.Parameters: watermark_notification – WatermarkNotificationthat occurred.
-
get_all(include_archived=False)[source]¶ Get all the conversations.
Parameters: include_archived (bool) – (optional) Whether to include archived conversations. Defaults to False.Returns: List of all Conversationobjects.
-
get(conv_id)[source]¶ Get a conversation by its ID.
Parameters: conv_id (str) – ID of conversation to return. Raises: KeyError– If the conversation ID is not found.Returns: Conversationwith matching ID.
- client – The connected
Conversation¶
-
class
hangups.conversation.Conversation(client, user_list, conversation, events=[], event_cont_token=None)[source]¶ A single chat conversation.
Use
ConversationListmethods to get instances of this class.-
on_event= None¶ Eventfired when an event occurs in this conversation.Parameters: conv_event – ConversationEventthat occurred.
-
on_typing= None¶ Eventfired when a users starts or stops typing in this conversation.Parameters: typing_message – TypingStatusMessagethat occurred.
-
on_watermark_notification= None¶ Eventfired when a watermark (read timestamp) is updated for this conversation.Parameters: watermark_notification – WatermarkNotificationthat occurred.
-
last_modified¶ When conversation was last modified (
datetime.datetime).
-
latest_read_timestamp¶ Timestamp of latest read event (
datetime.datetime).
-
events¶ Loaded events sorted oldest to newest.
(list of
ConversationEvent).
-
watermarks¶ Participant watermarks.
(dict of
UserID,datetime.datetime).
-
unread_events¶ Loaded events which are unread sorted oldest to newest.
Some Hangouts clients don’t update the read timestamp for certain event types, such as membership changes, so this may return more unread events than these clients will show. There’s also a delay between sending a message and the user’s own message being considered read.
(list of
ConversationEvent).
-
is_archived¶ Trueif this conversation has been archived.
-
is_quiet¶ Trueif notification level for this conversation is quiet.
-
is_off_the_record¶ Trueif conversation is off the record (history is disabled).
-
is_group_link_sharing_enabled¶ Trueif group link sharing (joining by link) is enabled.
-
update_conversation(conversation)[source]¶ Update the internal state of the conversation.
This method is used by
ConversationListto maintain this instance.Parameters: conversation – Conversationmessage.
-
add_event(event_)[source]¶ Add an event to the conversation.
This method is used by
ConversationListto maintain this instance.Parameters: event – Eventmessage.Returns: ConversationEventrepresenting the event.
-
get_user(user_id)[source]¶ Get user by its ID.
Parameters: user_id (UserID) – ID of user to return. Raises: KeyError– If the user ID is not found.Returns: Userwith matching ID.
-
coroutine
send_message(segments, image_file=None, image_id=None, image_user_id=None)[source]¶ 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.
Parameters: - segments – List of
ChatMessageSegmentobjects 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_fileandimage_idtogether,image_filetakes precedence andimage_idwill be ignored. - image_user_id – (optional) Picasa user ID, required only if
image_idrefers to an image from a different Picasa user, such as Google’s sticker user.
Raises: NetworkError– If the message cannot be sent.Returns: ConversationEventrepresenting the new message.- segments – List of
-
coroutine
add_users(user_ids)[source]¶ Add one or more users to this conversation.
Parameters: user_ids – List of IDs of the new users to be added; accepts
UserID,InviteeIDor juststrIDs.Raises: ConversationTypeError– If conversation is not a group.NetworkError– If conversation cannot be invited to.
Returns: ConversationEventrepresenting the change.
-
coroutine
remove_user(user_id)[source]¶ Remove a user from this conversation.
Parameters: user_id – ID of the user to be removed; accepts
UserID,ParticipantIdor juststrIDs.Raises: ConversationTypeError– If conversation is not a group.NetworkError– If conversation cannot be removed from.
Returns: ConversationEventrepresenting the change.
-
coroutine
leave()[source]¶ Leave this conversation.
Raises: NetworkError– If conversation cannot be left.
-
coroutine
rename(name)[source]¶ Rename this conversation.
Hangouts only officially supports renaming group conversations, so custom names for one-to-one conversations may or may not appear in all first party clients.
Parameters: name (str) – New name. Raises: NetworkError– If conversation cannot be renamed.
-
coroutine
set_notification_level(level)[source]¶ Set the notification level of this conversation.
Parameters: level – NOTIFICATION_LEVEL_QUIETto disable notifications, orNOTIFICATION_LEVEL_RINGto enable them.Raises: NetworkError– If the request fails.
-
coroutine
modify_otr_status(off_record)[source]¶ Set the OTR mode of this conversation.
Parameters: off_record – Trueto disable history, orFalseto enable it.Raises: NetworkError– If the request fails.Returns: ConversationEventrepresenting the change.
-
coroutine
set_group_link_sharing_enabled(enabled)[source]¶ Set the link sharing mode of this conversation.
Parameters: enabled –
Trueto allow joining the conversation by link, orFalseto prevent it.Raises: ConversationTypeError– If conversation is not a group.NetworkError– If the request fails.
Returns: ConversationEventrepresenting the change.
-
coroutine
set_typing(typing=1)[source]¶ Set your typing status in this conversation.
Parameters: typing – (optional) TYPING_TYPE_STARTED,TYPING_TYPE_PAUSED, orTYPING_TYPE_STOPPEDto start, pause, or stop typing, respectively. Defaults toTYPING_TYPE_STARTED.Raises: NetworkError– If typing status cannot be set.
-
coroutine
update_read_timestamp(read_timestamp=None)[source]¶ Update the timestamp of the latest event which has been read.
This method will avoid making an API request if it will have no effect.
Parameters: read_timestamp (datetime.datetime) – (optional) Timestamp to set. Defaults to the timestamp of the newest event. Raises: NetworkError– If the timestamp cannot be updated.
-
coroutine
get_events(event_id=None, max_events=50)[source]¶ Get events from this conversation.
Makes a request to load historical events if necessary.
Parameters: Returns: List of
ConversationEventinstances, ordered oldest-first.Raises: KeyError– Ifevent_iddoes not correspond to a known event.NetworkError– If the events could not be requested.
-
next_event(event_id, prev=False)[source]¶ Get the event following another event in this conversation.
Parameters: Raises: KeyError– If no suchConversationEventis known.Returns: ConversationEventorNoneif there is no following event.
-
get_event(event_id)[source]¶ Get an event in this conversation by its ID.
Parameters: event_id (str) – ID of the event. Raises: KeyError– If no suchConversationEventis known.Returns: ConversationEventwith the given ID.
-
Conversation Event¶
-
class
hangups.ConversationEvent(event)[source]¶ An event which becomes part of the permanent record of a conversation.
This is a wrapper for the
Eventmessage, which may contain one of many subtypes, represented here as other subclasses.Parameters: event – Eventmessage.-
timestamp¶ When the event occurred (
datetime.datetime).
-
-
class
hangups.ChatMessageEvent(event)[source]¶ An event that adds a new message to a conversation.
Corresponds to the
ChatMessagemessage.-
segments¶ List of
ChatMessageSegmentin message (list).
-
-
class
hangups.OTREvent(event)[source]¶ An event that changes a conversation’s OTR (history) mode.
Corresponds to the
OTRModificationmessage.-
new_otr_status¶ The conversation’s new OTR status.
May be either
OFF_THE_RECORD_STATUS_OFF_THE_RECORDorOFF_THE_RECORD_STATUS_ON_THE_RECORD.
-
old_otr_status¶ The conversation’s old OTR status.
May be either
OFF_THE_RECORD_STATUS_OFF_THE_RECORDorOFF_THE_RECORD_STATUS_ON_THE_RECORD.
-
-
class
hangups.RenameEvent(event)[source]¶ An event that renames a conversation.
Corresponds to the
ConversationRenamemessage.
-
class
hangups.MembershipChangeEvent(event)[source]¶ An event that adds or removes a conversation participant.
Corresponds to the
MembershipChangemessage.-
type_¶ The type of membership change.
May be either
MEMBERSHIP_CHANGE_TYPE_JOINorMEMBERSHIP_CHANGE_TYPE_LEAVE.
-
-
class
hangups.HangoutEvent(event)[source]¶ An event that is related to a Hangout voice or video call.
Corresponds to the
HangoutEventmessage.-
event_type¶ The Hangout event type.
May be one of
HANGOUT_EVENT_TYPE_START,HANGOUT_EVENT_TYPE_END,HANGOUT_EVENT_TYPE_JOIN,HANGOUT_EVENT_TYPE_LEAVE,HANGOUT_EVENT_TYPE_COMING_SOON, orHANGOUT_EVENT_TYPE_ONGOING.
-
-
class
hangups.GroupLinkSharingModificationEvent(event)[source]¶ An event that modifies a conversation’s group link sharing status.
Corresponds to the
GroupLinkSharingModificationmessage.-
new_status¶ The new group link sharing status.
May be either
GROUP_LINK_SHARING_STATUS_ONorGROUP_LINK_SHARING_STATUS_OFF.
-
Chat Message Segment¶
-
class
hangups.ChatMessageSegment(text, segment_type=None, is_bold=False, is_italic=False, is_strikethrough=False, is_underline=False, link_target=None)[source]¶ A segment of a chat message in
ChatMessageEvent.Parameters: - text (str) – Text of the segment.
- segment_type – (optional) One of
SEGMENT_TYPE_TEXT,SEGMENT_TYPE_LINE_BREAK, orSEGMENT_TYPE_LINK. Defaults toSEGMENT_TYPE_TEXT, orSEGMENT_TYPE_LINKiflink_targetis specified. - is_bold (bool) – (optional) Whether the text is bold. Defaults to
False. - is_italic (bool) – (optional) Whether the text is italic. Defaults to
False. - is_strikethrough (bool) – (optional) Whether the text is struck through.
Defaults to
False. - is_underline (bool) – (optional) Whether the text is underlined.
Defaults to
False. - link_target (str) – (option) URL to link to. Defaults to
None.
-
static
from_str(text)[source]¶ Construct
ChatMessageSegmentlist parsed from a string.Parameters: text (str) – Text to parse. May contain line breaks, URLs and formatting markup (simplified Markdown and HTML) to be converted into equivalent segments. Returns: List of ChatMessageSegmentobjects.
-
static
deserialize(segment)[source]¶ Construct
ChatMessageSegmentfromSegmentmessage.Parameters: segment – Segmentmessage to parse.Returns: ChatMessageSegmentobject.
Notifications¶
-
class
hangups.parsers.TypingStatusMessage(conv_id, user_id, timestamp, status)¶ A notification about a user’s typing status in a conversation.
Parameters: - conv_id (str) – ID of the conversation.
- user_id (hangups.user.UserID) – ID of the affected user.
- timestamp (datetime.datetime) – When the notification was generated.
- status – The new status; one of
TYPING_TYPE_STARTED,TYPING_TYPE_PAUSED, orTYPING_TYPE_STOPPED.
-
class
hangups.parsers.WatermarkNotification(conv_id, user_id, read_timestamp)¶ A notification about a user’s watermark (read timestamp).
Parameters: - conv_id (str) – ID of the conversation.
- user_id (hangups.user.UserID) – ID of the affected user.
- read_timestamp (datetime.datetime) – The new watermark.
User List¶
-
class
hangups.UserList(client, self_entity, entities, conv_parts)[source]¶ Maintains a list of all the users.
Using
build_user_conversation_list()to initialize this class is recommended.Parameters: - client – The connected
Client. - self_entity –
Entitymessage for the current user. - entities – List of known
Entitymessages. - conv_parts – List of
ConversationParticipantDatamessages. These are used as a fallback in case any users are missing.
- client – The connected
User¶
-
class
hangups.user.NameType¶ Indicates which type of name a user has.
DEFAULTindicates that only a first name is available.NUMERICindicates that only a numeric name is available.REALindicates that a real full name is available.-
DEFAULT= 0¶
-
NUMERIC= 1¶
-
REAL= 2¶
-
-
class
hangups.user.UserID(chat_id, gaia_id)¶ A user ID, consisting of two parts which are always identical.
-
class
hangups.user.User(user_id, full_name, first_name, photo_url, canonical_email, emails, is_self)[source]¶ A chat user.
Use
UserListorConversationListmethods to get instances of this class.-
upgrade_name(user_)[source]¶ Upgrade name type of this user.
Google Voice participants often first appear with no name at all, and then get upgraded unpredictably to numbers (“+12125551212”) or names.
Parameters: user (User) – User to upgrade with.
-
static
from_entity(entity, self_user_id)[source]¶ Construct user from
Entitymessage.Parameters: Returns: Userobject.
-