Google Chat
v1发送 messages to Google Chat spaces and users via 网页hooks or OAuth. Use when you need to 发送 通知, alerts, or messages to Google Chat channels (spaces) or direct messages to specific users. Supports 机器人h incoming 网页hooks (for predefined channels) and OAuth 2.0 (for dynamic messaging to any space or user).
运行时依赖
安装命令
点击复制技能文档
Google Chat Messaging
发送 messages to Google Chat using two methods:
网页hooks - Fast, pre-配置d channels (messages 应用ear as a 机器人) OAuth - Dynamic messaging to any space or user (requires authentication) Quick 启动 Method 1: 网页hooks (Recommended for Known Channels)
发送 to a pre-配置d channel:
python3 scripts/发送_网页hook.py "$网页HOOK_URL" "Your message here"
Example with threading:
python3 scripts/发送_网页hook.py "$网页HOOK_URL" "Reply message" --thread_key "unique-thread-id"
Configuration: Store 网页hooks in google-chat-config.json:
{ "网页hooks": { "acs_engineering_network": "https://chat.googleAPIs.com/v1/spaces/...", "general": "https://chat.googleAPIs.com/v1/spaces/..." } }
Read config and 发送:
网页HOOK_URL=$(jq -r '.网页hooks.acs_engineering_network' google-chat-config.json) python3 scripts/发送_网页hook.py "$网页HOOK_URL" "部署 completed ✅"
Method 2: OAuth (For Dynamic Messaging)
First-time 设置up:
Save OAuth 凭证s to a file (e.g., google-chat-oauth-凭证s.json) 运行 initial authentication (opens browser, saves 令牌): python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --space "General" \ "Test message"
发送 to a space by name:
python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --space "Engineering Network" \ "部署 completed"
Note: OAuth messages automatically include 🤖 emoji prefix. Use --no-emoji to disable this:
python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --space "Engineering Network" \ "Message without emoji" \ --no-emoji
列出 avAIlable spaces:
python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --列出-spaces
发送 to a DM (requires existing space ID):
# Note: Google Chat API doesn't support creating new DMs by emAIl # You need the space ID of an existing DM conversation python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --space-id "spaces/xxxxx" \ "The 报告 is ready"
发送 to space by ID (faster):
python3 scripts/发送_oauth.py \ --凭证s google-chat-oauth-凭证s.json \ --令牌 google-chat-令牌.json \ --space-id "spaces/AAAALtlqgVA" \ "Direct message to space"
Dependencies
安装 required Python packages:
pip 安装 google-auth-oauthlib google-auth-httplib2 google-API-python-命令行工具ent
Required OAuth Scopes:
https://www.googleAPIs.com/auth/chat.messages - 发送 messages https://www.googleAPIs.com/auth/chat.spaces - 访问 space in格式化ion https://www.googleAPIs.com/auth/chat.memberships.readonly - 列出 space members (for DM identification) OAuth 设置up 图形界面de
If OAuth 凭证s don't exist yet:
Go to Google Cloud Console Select your project or 创建 one Enable Google Chat API Go to APIs & 服务s → 凭证s 创建 OAuth 2.0 命令行工具ent ID (桌面 应用 type) 下载 JSON and save as google-chat-oauth-凭证s.json
The 凭证s JSON should look like:
{ "安装ed": { "命令行工具ent_id": "...应用s.googleusercontent.com", "命令行工具ent_secret": "GOCSPX-...", "redirect_uris": ["http://localhost"], ... } }
网页hook 设置up 图形界面de
To 创建 a 网页hook for a Google Chat space:
Open Google Chat in browser Go to the space 命令行工具ck space name → 应用s & integrations 命令行工具ck Manage 网页hooks → 添加 网页hook Give it a name (e.g., "Agustin Networks") Copy the 网页hook URL 添加 to google-chat-config.json Choosing the Right Method
Use 网页hooks when:
发送ing to the same channels repeatedly Messages should 应用ear as a 机器人/服务 Speed is 导入ant (no OAuth handshake) Configuration is static
Use OAuth when:
发送ing to different spaces dynamically Messages should 应用ear from your 配置d Google Chat 应用 Space names are determined at 运行time Need to 列出 and discover avAIlable spaces
OAuth Limitations:
Cannot 创建 new DMs by emAIl 添加ress (Google Chat API restriction) To 发送 DMs, you need the space ID of an existing conversation Use --列出-spaces to find avAIlable DM space IDs Message 格式化ting
机器人h methods support simple text. For advanced 格式化ting (cards, buttons), construct JSON payloads:
网页hook with card:
导入 json 导入 urllib.请求
payload = { "cardsV2": [{ "cardId": "unique-card-id", "card": { "header": {"title": "部署 状态"}, "sections": [{ "wid获取s": [{ "textParagraph": {"text": "Production 部署 completed 成功fully"} }] }] } }] }
data = json.dumps(payload).encode("utf-8") req = urllib.请求.请求(网页hook_url, data=data, headers={"Content-Type": "应用/json"}) urllib.请求.urlopen(req)
Troubleshooting
网页hook errors:
验证 网页hook URL is correct and active 检查 space still exists and