如何同步Hangouts对话与聊天室?Python脚本消息发送问题求助
Hey there, let's tackle your two Hangouts-related questions one by one:
First off, it's important to note that Hangouts对话(hangouts.google.com) and Hangouts聊天室(chat.google.com) are technically separate Google services, so there's no built-in one-click sync feature. But here are a few workarounds you can use:
- Manual forwarding: For critical messages, you can right-click on a message in Hangouts dialogue, select "Forward", and pick the target Hangouts chat room. This is straightforward but only practical for small volumes of messages.
- Automated scripts: If you need ongoing sync, you can build a custom script to listen for new messages in Hangouts dialogue and auto-send them to the chat room. Keep in mind that Google's API support for classic Hangouts (the dialogue interface) is more limited than for Google Chat, so you'll need to use the older Hangouts API and handle OAuth 2.0 authorization properly.
- Migrate to Google Chat: Long-term, Google is phasing out classic Hangouts in favor of Google Chat. If you move your conversations to Google Chat entirely, you won't need sync at all—all messages will live within the same ecosystem.
The root issue here is that you're using the wrong API for classic Hangouts group dialogues. Let's fix this:
1. Understand the API difference
- The script you're using now likely targets the Google Chat API (either webhooks or REST API), which works for Hangouts chat rooms (chat.google.com).
- Classic Hangouts group dialogues require the Hangouts API (not the newer Google Chat API), which has different authorization and endpoint requirements.
2. Adjust your script setup
First, make sure you have the right dependencies installed:
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
3. Configure API credentials
- Go to the Google Cloud Console, enable the Hangouts API, and create OAuth 2.0 credentials (download the
credentials.jsonfile to your script directory). Note: This usually requires a Google Workspace (formerly G Suite) account—personal Google accounts may have limited access to this API.
4. Example script for classic Hangouts groups
Here's a simplified script to send messages to a classic Hangouts group:
from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import pickle import os.path # Required API scope for Hangouts SCOPES = ['https://www.googleapis.com/auth/hangouts.chat'] def get_hangouts_service(): creds = None # Load saved credentials if available if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # Refresh or fetch new credentials if needed if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save credentials for future use with open('token.pickle', 'wb') as token: pickle.dump(creds, token) # Build and return the Hangouts service return build('hangouts', 'v1', credentials=creds) def send_message_to_group(group_id, message_text): service = get_hangouts_service() # Send message to the target group service.spaces().messages().create( parent=f'spaces/{group_id}', body={'text': message_text} ).execute() # Usage example if __name__ == '__main__': # Replace with your classic Hangouts group ID target_group_id = 'your_group_unique_id' # Replace with your extracted data from the third-party API api_data_message = "Here's the data from the third-party API: XYZ123" send_message_to_group(target_group_id, api_data_message)
5. Find your classic Hangouts group ID
- Open the classic Hangouts group dialogue, go to group settings, and check the group link. The last segment of the URL is your group ID. Alternatively, use the
spaces.listmethod in the Hangouts API to list all your groups and retrieve the ID programmatically.
Alternative: Migrate to Google Chat groups
If you can switch your group to Google Chat (chat.google.com), you can keep using your original script with Google Chat webhooks, which is simpler and has better ongoing support from Google.
内容的提问来源于stack exchange,提问作者Subramania Prasad

