Skip to content

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

License

Notifications You must be signed in to change notification settings

pkarl/gchatcardbuilder

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages