Skip to main content

Hangouts Chat Helper

PyPI version Build status Coverage Python versions Github license

Description

Helper Python classes for handling and responding to Hangouts Chat events.

Installation

Install with pip:

pip install hangouts-helper

Message Components

This library contains several component classes to assist with constructing a Hangouts Chat message.

  • Message
  • Section
  • Card
  • CardAction
  • CardHeader
  • TextParagraph
  • KeyValue
  • Image
  • ButtonList
  • ImageButton
  • TextButton

Example

Using the Pizza Bot example from the official Hangouts Chat API documentation, this is how you'd construct the same message using the components above.

from hangouts_helper.message import (Message, Card, CardHeader, Section,
    Image, KeyValue, ButtonList, TextButton)

message = Message()
message.add_card(
    Card(
        CardHeader(
            title='Pizza Bot Customer Support',
            subtitle='pizzabot@example.com',
            image_url='https://goo.gl/aeDtrS'),
        Section(
            KeyValue(top_label='Order No.', content='12345'),
            KeyValue(top_label='Status', content='In Delivery')),
        Section(
            'Location',
            Image(image_url='https://maps.googleapis.com/...')),
        Section(
            ButtonList(
                TextButton(text='OPEN ORDER').add_link(url='https://example.com/orders/...')))))

Calling message.output() produces a dict that can be converted to JSON... perfect for returning a response to a synchronous chat event, or sending a new message via the Hangouts Chat API.

{
    "cards": [
        {
            "header": {
                "imageUrl": "https://goo.gl/aeDtrS",
                "subtitle": "pizzabot@example.com",
                "title": "Pizza Bot Customer Support"
            },
            "sections": [
                {
                    "widgets": [
                        {
                            "keyValue": {
                                "content": "12345",
                                "topLabel": "Order No."
                            }
                        },
                        {
                            "keyValue": {
                                "content": "In Delivery",
                                "topLabel": "Status"
                            }
                        }
                    ]
                },
                {
                    "header": "Location",
                    "widgets": [
                        {
                            "image": {
                                "imageUrl": "https://maps.googleapis.com/..."
                            }
                        }
                    ]
                },
                {
                    "widgets": [
                        {
                            "buttons": [
                                {
                                    "textButton": {
                                        "onClick": {
                                            "openLink": {
                                                "url": "https://example.com/orders/..."
                                            }
                                        },
                                        "text": "OPEN ORDER"
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Chat Handler

The library also includes a class to help with handling incoming chat events. The methods contained in HangoutsChatHandler correspond to the different event types you can expect to receive. Each method should return a message response.

Example

from enum import Enum

from hangouts_helper.handler import HangoutsChatHandler, SpaceType
from hangouts_helper.message import Message


class ActionMethod(Enum):
    BUTTON_CLICKED = 'BUTTON_CLICKED'


class MyHangoutsChatHandler(HangoutsChatHandler):
    ActionMethod = ActionMethod

    def handle_added_to_space(self, space_type, event):
        if space_type == SpaceType.DM:
            return Message(text="Thanks for DM'ing me!")
        elif space_type == SpaceType.ROOM:
            return Message(text="Thanks adding me to your room!")

    def handle_message(self, message, event):
        return Message(text="Thanks for your message!")

    def handle_card_clicked(self, action_method, action_parameters, event):
        if action_method == ActionMethod.BUTTON_CLICKED:
            return Message(text="I've processed your button click!")

    def handle_removed_from_space(self, event):
        pass

A Flask app that repsonds to Hangouts Chat events might look like:

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def bot_handler():
    event = request.json
    handler = MyHangoutsChatHandler()
    response_message = handler.handle_event(event)
    return jsonify(response_message.output())

TODO

  • Add examples for each component type in README
  • Document and add examples for adding OnClick events to widgets
  • Document Enums (SpaceType, EventType, ActionMethod, Icon, ImageStyle, ResponseType)
  • Document usage scenarios for HangoutsChatHandler
  • Add methods for interacting with Hangouts Chat API to HangoutsChatHandler

Metadata

Release files for hangouts-helper 19.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hangouts-helper 19.12.0
File Size Uploaded
hangouts-helper-19.12.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hangouts-helper 19.12.0
File Interpreter ABI Platform
hangouts_helper-19.12.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.5 kB

Release files / hangouts-helper-19.12.0.tar.gz

Download URL hangouts-helper-19.12.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ac2165d2fab4bdce6643e463d5b98e82f3a2264442033b77e348f415ea92b2c8
BLAKE2b-256 checksum
How to use checksums
151f68a094c4eea7a3b5b7cb1be72a9ed610fac9b6a3f43832ff563e0dd2ec19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.7

Release files / hangouts_helper-19.12.0-py2.py3-none-any.whl

Download URL hangouts_helper-19.12.0-py2.py3-none-any.whl
Size 8.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
885c3e5f256c37d49bb0063c735cc31187b32268b020d99e49930b0e51f5c684
BLAKE2b-256 checksum
How to use checksums
f8322f133c8a5fce4760e0d68f655446184a597d96b47071c99a1363097b4f2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.7

Release history Release notifications | RSS feed

This release

19.12.0 This release

2 release files

18.9.0

2 release files

18.8.6

2 release files

18.8.5

2 release files

18.8.4

2 release files

18.8.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page