Skip to content

Commit 5991bed

Browse files
iHildysteipete
authored andcommitted
feat(googlechat): support Google Workspace Add-on event format
1 parent 0f6e39b commit 5991bed

4 files changed

Lines changed: 91 additions & 16 deletions

File tree

‎docs/channels/googlechat.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,42 @@ Notes:
162162
- Attachments are downloaded through the Chat API and stored in the media pipeline (size capped by `mediaMaxMb`).
163163

164164
## Troubleshooting
165+
166+
### 405 Method Not Allowed
167+
If Google Cloud Logs Explorer shows errors like:
168+
```
169+
status code: 405, reason phrase: HTTP error response: HTTP/1.1 405 Method Not Allowed
170+
```
171+
172+
This means the webhook handler isn't registered. Common causes:
173+
1. **Channel not configured**: The `channels.googlechat` section is missing from your config. Verify with:
174+
```bash
175+
clawdbot config get channels.googlechat
176+
```
177+
If it returns "Config path not found", add the configuration (see [Config highlights](#config-highlights)).
178+
179+
2. **Plugin not enabled**: Check plugin status:
180+
```bash
181+
clawdbot plugins list | grep googlechat
182+
```
183+
If it shows "disabled", add `plugins.entries.googlechat.enabled: true` to your config.
184+
185+
3. **Gateway not restarted**: After adding config, restart the gateway:
186+
```bash
187+
clawdbot gateway restart
188+
```
189+
190+
Verify the channel is running:
191+
```bash
192+
clawdbot channels status
193+
# Should show: Google Chat default: enabled, configured, ...
194+
```
195+
196+
### Other issues
165197
- Check `clawdbot channels status --probe` for auth errors or missing audience config.
166-
- If no messages arrive, confirm the Chat app’s webhook URL + event subscriptions.
167-
- If mention gating blocks replies, set `botUser` to the app’s user resource name and verify `requireMention`.
198+
- If no messages arrive, confirm the Chat app's webhook URL + event subscriptions.
199+
- If mention gating blocks replies, set `botUser` to the app's user resource name and verify `requireMention`.
200+
- Use `clawdbot logs --follow` while sending a test message to see if requests reach the gateway.
168201

169202
Related docs:
170203
- [Gateway configuration](/gateway/configuration)

‎extensions/googlechat/src/auth.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { ResolvedGoogleChatAccount } from "./accounts.js";
44

55
const CHAT_SCOPE = "https://www.googleapis.com/auth/chat.bot";
66
const CHAT_ISSUER = "[email protected]";
7+
// Google Workspace Add-ons use a different service account pattern
8+
const ADDON_ISSUER_PATTERN = /^service-\d+@gcp-sa-gsuiteaddons\.iam\.gserviceaccount\.com$/;
79
const CHAT_CERTS_URL =
810
"https://www.googleapis.com/service_accounts/v1/metadata/x509/[email protected]";
911

@@ -87,8 +89,9 @@ export async function verifyGoogleChatRequest(params: {
8789
audience,
8890
});
8991
const payload = ticket.getPayload();
90-
const ok = Boolean(payload?.email_verified) && payload?.email === CHAT_ISSUER;
91-
return ok ? { ok: true } : { ok: false, reason: "invalid issuer" };
92+
const email = payload?.email ?? "";
93+
const ok = payload?.email_verified && (email === CHAT_ISSUER || ADDON_ISSUER_PATTERN.test(email));
94+
return ok ? { ok: true } : { ok: false, reason: `invalid issuer: ${email}` };
9295
} catch (err) {
9396
return { ok: false, reason: err instanceof Error ? err.message : "invalid token" };
9497
}

‎extensions/googlechat/src/monitor.ts‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import {
1212
} from "./api.js";
1313
import { verifyGoogleChatRequest, type GoogleChatAudienceType } from "./auth.js";
1414
import { getGoogleChatRuntime } from "./runtime.js";
15-
import type { GoogleChatAnnotation, GoogleChatAttachment, GoogleChatEvent } from "./types.js";
15+
import type {
16+
GoogleChatAnnotation,
17+
GoogleChatAttachment,
18+
GoogleChatEvent,
19+
GoogleChatSpace,
20+
GoogleChatMessage,
21+
GoogleChatUser,
22+
} from "./types.js";
1623

1724
export type GoogleChatRuntimeEnv = {
1825
log?: (message: string) => void;
@@ -168,44 +175,76 @@ export async function handleGoogleChatWebhookRequest(
168175
return true;
169176
}
170177

171-
const raw = body.value;
178+
let raw = body.value;
172179
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
173180
res.statusCode = 400;
174181
res.end("invalid payload");
175182
return true;
176183
}
177184

178-
const eventType = (raw as { type?: string; eventType?: string }).type ??
179-
(raw as { type?: string; eventType?: string }).eventType;
185+
// Transform Google Workspace Add-on format to standard Chat API format
186+
const rawObj = raw as {
187+
commonEventObject?: { hostApp?: string };
188+
chat?: {
189+
messagePayload?: { space?: GoogleChatSpace; message?: GoogleChatMessage };
190+
user?: GoogleChatUser;
191+
eventTime?: string;
192+
};
193+
authorizationEventObject?: { systemIdToken?: string };
194+
};
195+
196+
if (rawObj.commonEventObject?.hostApp === "CHAT" && rawObj.chat?.messagePayload) {
197+
const chat = rawObj.chat;
198+
const messagePayload = chat.messagePayload;
199+
raw = {
200+
type: "MESSAGE",
201+
space: messagePayload?.space,
202+
message: messagePayload?.message,
203+
user: chat.user,
204+
eventTime: chat.eventTime,
205+
};
206+
207+
// For Add-ons, the bearer token may be in authorizationEventObject.systemIdToken
208+
const systemIdToken = rawObj.authorizationEventObject?.systemIdToken;
209+
if (!bearer && systemIdToken) {
210+
Object.assign(req.headers, { authorization: `Bearer ${systemIdToken}` });
211+
}
212+
}
213+
214+
const event = raw as GoogleChatEvent;
215+
const eventType = event.type ?? (raw as { eventType?: string }).eventType;
180216
if (typeof eventType !== "string") {
181217
res.statusCode = 400;
182218
res.end("invalid payload");
183219
return true;
184220
}
185221

186-
const rawObj = raw as Record<string, unknown>;
187-
if (!rawObj.space || typeof rawObj.space !== "object" || Array.isArray(rawObj.space)) {
222+
if (!event.space || typeof event.space !== "object" || Array.isArray(event.space)) {
188223
res.statusCode = 400;
189224
res.end("invalid payload");
190225
return true;
191226
}
192227

193228
if (eventType === "MESSAGE") {
194-
if (!rawObj.message || typeof rawObj.message !== "object" || Array.isArray(rawObj.message)) {
229+
if (!event.message || typeof event.message !== "object" || Array.isArray(event.message)) {
195230
res.statusCode = 400;
196231
res.end("invalid payload");
197232
return true;
198233
}
199234
}
200235

201-
const event = raw as GoogleChatEvent;
236+
// Re-extract bearer in case it was updated from Add-on format
237+
const authHeaderNow = String(req.headers.authorization ?? "");
238+
const effectiveBearer = authHeaderNow.toLowerCase().startsWith("bearer ")
239+
? authHeaderNow.slice("bearer ".length)
240+
: bearer;
202241

203242
let selected: WebhookTarget | undefined;
204243
for (const target of targets) {
205244
const audienceType = target.audienceType;
206245
const audience = target.audience;
207246
const verification = await verifyGoogleChatRequest({
208-
bearer,
247+
bearer: effectiveBearer,
209248
audienceType,
210249
audience,
211250
});

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)