@@ -12,7 +12,14 @@ import {
1212} from "./api.js" ;
1313import { verifyGoogleChatRequest , type GoogleChatAudienceType } from "./auth.js" ;
1414import { 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
1724export 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 } ) ;
0 commit comments