Add space caching and auto-resolution for proactive Google Chat messages:
- Auto-cache space IDs when users message the bot (knownSpaces)
- Auto-resolve users/{id} to cached spaces for outbound messages
- Fallback to findDirectMessage API when not cached
- Add comprehensive documentation for proactive messaging patterns
This enables cron jobs, skills, and agents to send proactive messages
to users who have previously interacted with the bot.
Fixes: Users can now send messages via 'users/{id}' target instead of
needing to know the space ID beforehand.
3.6 KiB
3.6 KiB
Google Chat Proactive Messaging - Implementation Summary
This document summarizes the changes made to enable proactive messaging in the Google Chat extension.
Files Modified
1. src/config/types.googlechat.ts
Added:
GoogleChatKnownSpacetype - Structure for cached space entriesGoogleChatKnownSpacestype - Map of user IDs to space infoknownSpacesfield toGoogleChatAccountConfig- Stores cached space mappings
2. src/config/zod-schema.providers-core.ts
Added:
GoogleChatKnownSpaceSchema- Zod validation schema for space cache entriesknownSpacesfield toGoogleChatAccountSchema- Config validation
3. extensions/googlechat/src/space-cache.ts (NEW)
Created:
getKnownSpaces()- Retrieve cached spaces for an accountgetCachedSpaceForUser()- Look up space by user IDhasCachedSpace()- Check if space is cachedbuildSpaceCachePatch()- Create config patch for new cache entriesextractSpaceInfoFromEvent()- Extract space info from incoming messages
4. extensions/googlechat/src/targets.ts
Modified:
- Updated
resolveGoogleChatOutboundSpace()to accept config and options - Added cache lookup before calling
findDirectMessageAPI - Added
ResolveSpaceOptionstype foruseCacheanduseFindDirectMessageflags - Improved error messages with actionable guidance
5. extensions/googlechat/src/monitor.ts
Modified:
- Added import for
space-cacheutilities - Added space caching logic in
processMessageWithPipeline() - Spaces are now cached automatically when users message the bot
6. extensions/googlechat/src/channel.ts
Modified:
- Updated
sendTextto pass config toresolveGoogleChatOutboundSpace() - Updated
sendMediato pass config toresolveGoogleChatOutboundSpace() - Updated
notifyApprovalto pass config toresolveGoogleChatOutboundSpace()
7. extensions/googlechat/PROACTIVE-MESSAGING.md (NEW)
Created:
- Comprehensive documentation for proactive messaging
- Usage examples for all 4 methods
- Troubleshooting guide
- Best practices
How It Works
- Incoming Message →
monitor.tsextracts space info and caches it - Outgoing Message →
channel.tscallsresolveGoogleChatOutboundSpace() - Resolution →
targets.tschecks cache first, then API fallback - Delivery → Message sent via
api.ts
Usage Examples
CLI
# Using user ID (auto-resolves to cached space)
moltbot message send --channel googlechat --to "users/123" --text "Hello!"
# Using space ID (direct)
moltbot message send --channel googlechat --to "spaces/AAA" --text "Hello!"
Config (Cron)
{
"cron": {
"jobs": [{
"schedule": "0 9 * * *",
"text": "Morning!",
"target": "users/123",
"channel": "googlechat"
}]
}
}
Config Structure
{
"channels": {
"googlechat": {
"knownSpaces": {
"users/123456": {
"spaceId": "spaces/AAAAxxxx",
"displayName": "John Doe",
"type": "DM",
"lastSeenAt": 1706515200000
}
}
}
}
}
Benefits
- Automatic Caching - No manual space ID management needed
- Multiple Fallbacks - Cache → API → Error with guidance
- Backward Compatible - Existing space ID targeting still works
- Well Documented - Clear patterns for developers
Testing Recommendations
- Test incoming messages cache the space
- Test proactive send using cached user ID
- Test proactive send using space ID directly
- Test error case when user hasn't messaged first
- Test with multiple accounts