💎 ChatSDK Ruby

Google Chat Adapter

Google Chat API adapter with Card v2 via the chat_sdk-gchat gem.

The Google Chat adapter (chat_sdk-gchat) integrates with Google Chat using the Google Chat API and Card v2.

Installation

# Gemfile
gem "chat_sdk"
gem "chat_sdk-gchat"

Configuration

require "chat_sdk"
require "chat_sdk/gchat"

gchat = ChatSDK::GChat::Adapter.new(
  project_number: ENV["GOOGLE_CHAT_PROJECT_NUMBER"],
  credentials: nil  # Uses Application Default Credentials
)

Credentials Options

The credentials parameter accepts several formats:

# 1. nil -- Use Application Default Credentials (ADC)
gchat = ChatSDK::GChat::Adapter.new(project_number: "123456")

# 2. String -- Path to a service account JSON file
gchat = ChatSDK::GChat::Adapter.new(
  project_number: "123456",
  credentials: "/path/to/service-account.json"
)

# 3. Hash -- Service account JSON as a Ruby hash
gchat = ChatSDK::GChat::Adapter.new(
  project_number: "123456",
  credentials: {
    "type" => "service_account",
    "project_id" => "my-project",
    # ...
  }
)

# 4. Pre-built credentials object
gchat = ChatSDK::GChat::Adapter.new(
  project_number: "123456",
  credentials: my_credentials_object
)

Environment Variables

VariableDescription
GOOGLE_CHAT_PROJECT_NUMBERYour Google Cloud project number
GOOGLE_APPLICATION_CREDENTIALSPath to service account JSON (for ADC)

Google Cloud Setup

  1. In the Google Cloud Console, enable the Google Chat API.
  2. Create a service account with the Chat Bot scope.
  3. Configure your Chat app in the Google Cloud Console:
    • Set the app URL to your webhook endpoint.
    • Configure slash commands if needed.
  4. Publish the app to your organization or make it available in Google Workspace Marketplace.

Webhook URL

https://your-domain.com/webhooks/gchat
map "/webhooks/gchat" do
  run bot.webhooks[:gchat]
end

Capabilities

CapabilitySupportedNotes
edit_messagesYesUses update_message
delete_messagesYesUses delete_message
ephemeral_messagesYesUses private_message_viewer
file_uploadsNoRaises NotSupportedError
reactionsYesUses unicode emoji
modalsNoRaises NotSupportedError
typing_indicatorNoRaises NotSupportedError
streaming_editYes
threadsYesUses Google Chat thread names
direct_messagesYesUses setup_space with DIRECT_MESSAGE
message_historyYesUses list_messages

Resource Names

Google Chat uses resource names like spaces/SPACE_ID/messages/MESSAGE_ID. ChatSDK extracts the last segment as the ID, so message.id returns just MESSAGE_ID and channel_id is SPACE_ID.

Cards Rendering

Cards are rendered as Google Chat Card v2 JSON using CardV2Renderer.

Mention Formatting

Google Chat uses the format <users/USER_ID>:

thread.mention_user("12345")  # => "<users/12345>"

Direct Client Access

gchat_adapter = bot.adapter(:gchat)
client = gchat_adapter.client  # Google::Apps::Chat::V1::ChatService::Client

Pub/Sub Events

For apps using Google Workspace Events with a Cloud Pub/Sub subscription, use parse_pubsub_event to decode incoming push messages. The Chat event payload is base64-encoded inside the Pub/Sub envelope's message.data field:

# In your Pub/Sub push endpoint
post "/pubsub/gchat" do
  events = gchat.parse_pubsub_event(request)
  events.each { |event| handle(event) }
  status 200
end

This is an alternative to the direct webhook approach -- configure the Pub/Sub subscription in Google Cloud Console or Terraform, and point its push endpoint to your app. The adapter decodes the envelope and delegates to the same EventParser used for direct webhooks.

On this page