Skip to main content

A clean, type-safe interface for generating rich chat responses to the Google Chat API using the CardsV2 schema.

Project description

GChat Card Builder (v2)

Gchat Card Builder is a Python library that assists in the creation and management of chat "cards" for Google Chat responses via API. Supports CardsV2 format.

codecov Python CI with Pytest and Coverage


Table of Contents

  1. Description
  2. Installation
  3. Usage
  4. Documentation
  5. Tests

Description

Gchat Card Builder provides an easy-to-use interface for creating richly formatted cards compatible with Google Chat. The goal is to stop mucking around with deeply nested structures just to send a slightly rich chat message.

Here's an example of how this could help clean up code:

# broken onto several lines for readability
gchat_response = copy.deepcopy(BIG_OLD_DICT_TEMPLATE)
widgets = gchat_response["cardsV2"][0]["card"]["sections"][0]["widgets"]
button = widgets[0]["buttonList"]["buttons"][0]
button["text"] = "click to get rick rolled"
button["onClick"]["openLink"]["url"] = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"

becomes this:

builder = CardBuilder.create()
builder.add_section(
    W.ButtonList(
        buttons=[
            W.Button(
                text="click to get rick rolled",
                onClick=W.OnClick(
                    openLink=W.OpenLink(
                        url='https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                    )
                )
            )
        ]
    ),
)

gchat_response = builder.build()

Installation

Prerequisites

  • Python (versions tested: 3.10, 3.11)
  • may support others

Installation Steps

pip install gchatcardbuilder

Usage

Google Chat Card Builder provides an easy-to-use interface for creating richly formatted cards compatible with Google Chat. Here's how you can quickly start building cards using the factory method:

Basic Example:

from gchatcardbuilder import CardBuilder, CardWidgets as W

# Get builder instance using the factory method
builder = CardBuilder.create()

# Add card details
builder.set_card_id("sample_id")
builder.set_header(W.CardHeader(title="Test Header"))
builder.add_section(W.Section(header="Test Section", widgets=[]))

# Build the card
card = builder.build()

Documentation

You're looking at it.


Tests

If you're planning on contributing or wish to run tests to ensure the library functions as expected in your environment:

  1. Clone the repository:
git clone https://github.com/pkarl/gchatcardbuilder
  1. Navigate to the cloned directory and install the requirements:
pip install -r requirements.txt
  1. Run the tests:
pytest

If you have any questions, issues, or feedback, feel free to open an issue in the repository. Contributions are always welcome!


Maintained by PK2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gchatcardbuilder-0.1.6.tar.gz (13.1 kB view hashes)

Uploaded Source

Built Distribution

gchatcardbuilder-0.1.6-py3-none-any.whl (14.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page