@chat-adapter/gchat
v4.8.0
Published
Google Chat adapter for chat
Maintainers
Readme
@chat-adapter/gchat
Google Chat adapter for the chat SDK.
Installation
npm install chat @chat-adapter/gchatUsage
import { Chat } from "chat";
import { createGoogleChatAdapter } from "@chat-adapter/gchat";
const chat = new Chat({
userName: "mybot",
adapters: {
gchat: createGoogleChatAdapter({
credentials: JSON.parse(process.env.GOOGLE_CHAT_CREDENTIALS!),
}),
},
});
// Handle @mentions
chat.onNewMention(async (thread, message) => {
await thread.post("Hello from Google Chat!");
});Configuration
| Option | Required | Description |
|--------|----------|-------------|
| credentials | Yes* | Service account credentials JSON |
| useADC | No | Use Application Default Credentials instead |
| pubsubTopic | No | Pub/Sub topic for Workspace Events |
| impersonateUser | No | User email for domain-wide delegation |
*Either credentials or useADC: true is required.
Environment Variables
GOOGLE_CHAT_CREDENTIALS={"type":"service_account",...}
# Optional: for receiving ALL messages, not just @mentions
GOOGLE_CHAT_PUBSUB_TOPIC=projects/your-project/topics/chat-events
[email protected]Google Chat Setup
1. Create a GCP Project
- Go to console.cloud.google.com
- Click the project dropdown → New Project
- Enter project name and click Create
2. Enable Required APIs
- Go to APIs & Services → Library
- Search and enable:
- Google Chat API
- Google Workspace Events API (for receiving all messages)
- Cloud Pub/Sub API (for receiving all messages)
3. Create a Service Account
- Go to IAM & Admin → Service Accounts
- Click Create Service Account
- Enter name and description
- Click Create and Continue
- Skip the optional steps, click Done
4. Create Service Account Key
Note: If your organization has the
iam.disableServiceAccountKeyCreationconstraint enabled, you'll need to:
- Go to IAM & Admin → Organization Policies
- Find
iam.disableServiceAccountKeyCreation- Click Manage Policy → Override parent's policy
- Set to Not enforced (or add an exception for your project)
- Click on your service account
- Go to Keys tab
- Click Add Key → Create new key
- Select JSON and click Create
- Save the downloaded file
- Copy the entire JSON content →
GOOGLE_CHAT_CREDENTIALS(as a single line)
5. Configure Google Chat App
- Go to console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat
- Click Configuration
- Fill in:
- App name: Your bot's display name
- Avatar URL: URL to your bot's avatar image
- Description: What your bot does
- Interactive features:
- Enable Receive 1:1 messages
- Enable Join spaces and group conversations
- Connection settings: Select App URL
- App URL:
https://your-domain.com/api/webhooks/gchat - Visibility: Choose who can discover and install your app
- Click Save
Important for button clicks: The same App URL receives both message events and interactive events (card button clicks). Google Chat sends CARD_CLICKED events to this URL when users click buttons in cards.
6. Add Bot to a Space
- Open Google Chat
- Create or open a Space
- Click the space name → Manage apps & integrations (or Apps & integrations)
- Click Add apps
- Search for your app name
- Click Add
(Optional) Pub/Sub for All Messages
By default, Google Chat only sends webhooks for @mentions. To receive ALL messages in a space (for conversation context), you need to set up Workspace Events with Pub/Sub.
1. Create Pub/Sub Topic
- Go to Pub/Sub → Topics
- Click Create Topic
- Enter topic ID (e.g.,
chat-events) - Uncheck Add a default subscription
- Click Create
- Copy the full topic name →
GOOGLE_CHAT_PUBSUB_TOPIC- Format:
projects/your-project-id/topics/chat-events
- Format:
2. Grant Chat Service Account Access
Note: If your organization has the
iam.allowedPolicyMemberDomainsconstraint, you may need to temporarily relax it or use the console workaround below.
- Go to your Pub/Sub topic
- Click Permissions tab (or Show Info Panel → Permissions)
- Click Add Principal
- Enter:
[email protected] - Select role: Pub/Sub Publisher
- Click Save
If you get a policy error, try via Cloud Console:
- Go to Pub/Sub → Topics
- Check the box next to your topic
- Click Permissions in the info panel
- Click Add Principal
- Add
[email protected]with Pub/Sub Publisher role
3. Create Push Subscription
- Go to Pub/Sub → Subscriptions
- Click Create Subscription
- Enter subscription ID (e.g.,
chat-messages-push) - Select your topic
- Delivery type: Push
- Endpoint URL:
https://your-domain.com/api/webhooks/gchat - Click Create
4. Enable Domain-Wide Delegation
To create Workspace Events subscriptions and initiate DMs, you need domain-wide delegation:
Step 1: Enable delegation on the Service Account (GCP Console)
- Go to IAM & Admin → Service Accounts
- Click on your service account
- Go to Details tab
- Check Enable Google Workspace Domain-wide Delegation
- Click Save
- Go to Advanced settings (or click on the service account again)
- Copy the Client ID - this is a numeric ID (e.g.,
123456789012345678901), NOT the email address
Step 2: Authorize the Client ID (Google Admin Console)
- Go to Google Admin Console
- Go to Security → Access and data control → API controls
- Click Manage Domain Wide Delegation
- Click Add new
- Enter:
- Client ID: The numeric ID from Step 1 (e.g.,
123456789012345678901) - OAuth Scopes (all on one line, comma-separated):
https://www.googleapis.com/auth/chat.spaces.readonly,https://www.googleapis.com/auth/chat.messages.readonly,https://www.googleapis.com/auth/chat.spaces,https://www.googleapis.com/auth/chat.spaces.create
- Client ID: The numeric ID from Step 1 (e.g.,
- Click Authorize
Step 3: Set environment variable
Set GOOGLE_CHAT_IMPERSONATE_USER to an admin user email in your domain (e.g., [email protected]). This user will be impersonated when creating DM spaces and Workspace Events subscriptions.
Features
- Message posting and editing
- Thread subscriptions
- Reaction events (via Workspace Events)
- File attachments
- Rich cards (Google Chat Cards)
- Action callbacks (card buttons)
- Direct messages
- Space management
Limitations
- Typing indicators: Not supported by Google Chat API
- Adding reactions: Requires domain-wide delegation (appears from impersonated user, not bot)
Troubleshooting
No webhook received
- Verify the App URL is correct in Google Chat configuration
- Check that the Chat API is enabled
- Ensure the service account has the necessary permissions
Pub/Sub not working
- Verify
[email protected]has Pub/Sub Publisher role - Check that the push subscription URL is correct
- Verify domain-wide delegation is configured with correct scopes
- Check
GOOGLE_CHAT_IMPERSONATE_USERis a valid admin email
"Permission denied" for Workspace Events
- Ensure domain-wide delegation is configured
- Verify the OAuth scopes are exactly as specified
- Check that the impersonated user has access to the spaces
"Insufficient Permission" for DMs (openDM)
- DMs require domain-wide delegation with
chat.spacesandchat.spaces.createscopes - Add these scopes to your domain-wide delegation configuration in Google Admin Console
- Set
GOOGLE_CHAT_IMPERSONATE_USERto an admin email in your domain - Scope changes can take up to 24 hours to propagate
"unauthorized_client" error
- The Client ID is not registered in Google Admin Console
- Or domain-wide delegation is not enabled on the service account
Button clicks (CARD_CLICKED) not received
- Verify "Interactive features" is enabled in the Google Chat app configuration
- Check that the App URL is correctly set and accessible
- Button clicks go to the same webhook URL as messages
- Ensure your button elements have valid
idattributes (these become theactionId)
License
MIT
