Module googleapis.chat

ballerinax/googleapis.chat Ballerina library

0.1.0

Overview

Google Chat is a communication platform from Google, designed for teams and businesses as part of Google Workspace.

The ballerinax/googleapis.chat package provides both:

  • A REST client (chat:Client) for the Google Chat API — create spaces, send messages, manage memberships, upload attachments, etc.
  • A webhook listener (chat:Listener) that receives Google Chat interaction events (messages, slash commands, card clicks, dialog submissions, app-home opens) over HTTP and dispatches them to typed remote functions on a chat:ChatService.

The listener runs as a plain HTTPS endpoint that Google Chat POSTs events to directly. It supports three authentication mechanisms: service account (recommended for bots), OAuth 2.0 (for user-scoped actions such as attachment uploads), and short-lived bearer tokens (for quick tests).

Setup guide

To use the Google Chat connector, you must have access to the Google Chat API through a Google Cloud Platform (GCP) account with a project under it. If you do not have a GCP account, you can sign up for one here.

Step 1: Create a Google Cloud Platform project

  1. Open the Google Cloud Platform Console.

  2. Click the project drop-down menu and select an existing project, or create a new one for your Chat app.

    GCP Console Project View

Step 2: Enable the Google Chat API

  1. Navigate to APIs & Services → Library and enable the Google Chat API.

    Enable Google Chat API

Step 3: Expose your local listener (development)

Google Chat must reach the listener over a public HTTPS URL. For local development the easiest option is ngrok:

Copy
ngrok http 8000

Copy the https://<sub>.ngrok-free.app URL it prints — you will use this both in the next step and as the listener's endpointUrl in code.

For production, deploy the listener behind any HTTPS-terminating load balancer or reverse proxy; what matters is that Google Chat can reach a stable HTTPS URL.

Step 4: Configure the Chat app

  1. In the Google Cloud Console, open the Google Chat API page and select the Configuration tab.

    Chat App Configuration

  2. Provide the App name, Avatar URL and Description.

  3. Make sure "Build this Chat app as a Workspace add-on" is unchecked — this connector handles interaction events directly over HTTP, not as a Workspace add-on.

  4. Under Interactive features, enable the features your app needs (receive 1:1 messages, join spaces, slash commands, etc.).

  5. Under Connection settings, choose HTTP endpoint URL and paste the ngrok (or production) HTTPS URL from Step 3.

  6. Set Authentication audience to either:

    • the same HTTP endpoint URL (use HttpEndpointUrlConfig / endpointUrl in the listener), or
    • your Project number (use ProjectNumberConfig / projectNumber in the listener).

    The value you choose here must match what your service annotation declares — the listener uses it to validate the aud claim of the Google-signed bearer token on every incoming request.

    Connection Settings

  7. Under Visibility, add the email addresses of users or Google Workspace domains that can install your app.

Step 5: Choose an authentication method

The connector supports three authentication modes. Pick the one that matches your use case.

A service account lets your app act as itself — ideal for bots that post messages, manage memberships, or run continuously.

  1. Navigate to APIs & Services → Credentials, open the + Create credentials dropdown, and select Service account.

    Create Service Account

  2. Give it a name, click Done, then open the created service account and go to the Keys tab.

  3. Click Add key → Create new key → JSON and save the downloaded JSON file securely. You will reference its path from Config.toml.

    Download Service Account Key

Option B — OAuth 2.0 (for user-scoped actions)

OAuth 2.0 lets your app act on behalf of a signed-in user — required for operations like attachment uploads that need user scopes.

  1. Open APIs & Services → OAuth consent screen and configure your consent screen (provide an app name and support email). You do not need to add scopes here — they are requested at authorisation time in the OAuth Playground.

    OAuth Consent Screen

  2. Open APIs & Services → Credentials → Create credentials → OAuth client ID.

    Create OAuth Client

  3. Fill in the form:

    FieldValue
    Application typeWeb Application
    NameChatConnector
    Authorized Redirect URIshttps://developers.google.com/oauthplayground
  4. Save the Client ID and Client secret.

  5. Use the OAuth 2.0 Playground to obtain a refresh token: open the gear icon → "Use your own OAuth credentials" → enter the client ID and secret → authorise the Chat scopes you need → exchange the authorisation code for tokens.

    OAuth Playground

Option C — Bearer token (for quick tests)

For short-lived experiments you can use a Google access token directly:

Copy
gcloud auth print-access-token

Note: Google access tokens expire in roughly one hour. Bearer-token auth is best for short-lived processes (CI jobs, scripts, manual tests). For long-running services, use service account or OAuth 2.0 — both auto-refresh tokens.

Quickstart

The connector has two independent entry points — a REST client for calling the Chat API and a listener for handling interaction events. Follow the track that matches your use case.

Client

Use this if your app only calls the Chat REST API (no event handling).

Step 1: Import the module

Copy
import ballerinax/googleapis.chat;

Step 2: Initialise a Chat client

Create a chat:ConnectionConfig with the credentials obtained during setup.

Copy
configurable chat:OAuth2Config oauthAuth = ?;

final chat:Client chatClient = check new ({auth: oauthAuth});

Step 3: Invoke connector operations

Copy
// List spaces the app has access to.
chat:ListSpacesResponse spaces = check chatClient->/spaces();

// Send a message to a space.
chat:Message sent = check chatClient->/spaces/["space-id"]/messages.post({
    text: "Hello from Ballerina!"
});

Step 4: Run the Ballerina application

Copy
bal run

Listener

Use this if your app needs to handle interaction events from Google Chat (messages, card clicks, slash commands, etc.). The listener exposes an HTTP endpoint that Google Chat POSTs events to; it provides an internal Chat API client used by the injected caller — no separate client needed for replies.

Step 1: Import the module

Copy
import ballerinax/googleapis.chat;

Step 2: Initialise a Chat listener

Copy
listener chat:Listener chatListener = new (8000, {
    auth: {path: "./service-account-key.json"}
});

@chat:ServiceConfig {
    endpointUrl: "https://<your-subdomain>.ngrok-free.app"
}
service chat:ChatService on chatListener {

    remote function onMessage(chat:MessageEvent event, chat:MessageCaller caller) returns error? {
        check caller->respond({text: "Echo: " + (event.message.text ?: "")});
    }
}

The endpointUrl must exactly match the HTTP endpoint URL configured in your Chat app (Setup Step 4). The listener uses it to validate the aud claim of the incoming Google-signed bearer token. If you configured the Authentication audience as your project number instead, use projectNumber: "<your-project-number>" in the annotation in place of endpointUrl.

Step 3: Implement handlers

chat:ChatService exposes one optional remote function per Chat event type. Implement only the ones you need:

FunctionTriggered by
onMessageA user sends a message, @mentions the app, or invokes a slash command.
onAddedToSpaceThe app is added to a space.
onRemovedFromSpaceThe app is removed from a space.
onCardClickedA user clicks a button or interactive element on a card.
onSubmitFormA user submits a dialog or form.
onAppHomeA user opens the app's home page.
onWidgetUpdatedA widget requests an autocomplete or similar update.

Each handler receives the event and (optionally) an event-specific caller (chat:MessageCaller, chat:CardClickedCaller, chat:AppHomeCaller, etc.) pre-configured with the event's space context. Use the caller to respond (synchronously, within the event window) or to call Chat APIs asynchronously (sendMessage, updateMessage, etc.).

Step 4: Run the Ballerina application

Copy
bal run

In a separate terminal, expose the listener to Google Chat with ngrok (see Setup Step 3):

Copy
ngrok http 8000

Examples

The googleapis.chat connector provides practical examples illustrating usage in various scenarios. Explore these examples.

  1. Echo bot — A minimal Google Chat app that replies to every message with the same text, demonstrating the listener's HTTP delivery mode and replying via the injected chat:MessageCaller.

Import

import ballerinax/googleapis.chat;Copy

Other versions

0.1.0

Metadata

Released date: 3 days ago

Version: 0.1.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.13.0

GraalVM compatible: Yes


Pull count

Total: 0

Current verison: 0


Weekly downloads


Source repository


Keywords

Communication/Google Chat

Cost/Freemium

Vendor/Google

Area/Communication

Type/Connector

Type/Trigger


Contributors