Rasa与Google Chat集成后,如何从Hangout消息中获取用户邮箱ID?
Got it, let's break this down step by step. Since you've already got Rasa integrated with Google Chat, grabbing the user's email ID is totally doable by tapping into the webhook payload Google Chat sends to your Rasa server. Here's how to make it work:
1. Understand the Google Chat Webhook Payload
When a user sends a message in Google Chat, Google fires a POST request to your Rasa server's Google Chat webhook endpoint (usually /webhooks/google_chat/webhook). The JSON payload includes a user object that holds the user's email. A simplified version of the payload looks like this:
{
"message": {
"text": "Hi there!"
},
"user": {
"name": "John Doe",
"email": "john.doe@example.com",
"avatarUrl": "https://example.com/avatar.jpg"
},
// Additional fields like space details, thread info, etc.
}
2. Extract the Email in Rasa (Two Practical Methods)
Option 1: Use a Custom Action (Recommended for Most Scenarios)
Rasa automatically attaches the full Google Chat payload to the metadata field of the latest message in the tracker. You can access this in a custom action to pull out the email easily:
from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher from rasa_sdk.events import SlotSet class ActionGetUserEmail(Action): def name(self) -> str: return "action_get_user_email" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: dict) -> list: # Fetch Google Chat-specific metadata from the latest message google_chat_meta = tracker.latest_message.get("metadata", {}).get("google_chat", {}) user_details = google_chat_meta.get("user", {}) user_email = user_details.get("email", "Email not available") # You can either reply directly with the email or store it in a slot for later use dispatcher.utter_message(text=f"Your email is: {user_email}") # Optional: Save the email to a slot for persistent access return [SlotSet("user_email", user_email)]
Just add this action to your domain.yml under the actions section, then trigger it in your stories or rules whenever you need to retrieve the user's email.
Option 2: Customize the Google Chat Channel (For Advanced Use Cases)
If you're using a custom implementation of the Google Chat channel (instead of Rasa's built-in one), you can parse the incoming POST request directly to extract the email:
from flask import Flask, request, jsonify from rasa.core.channels.channel import InputChannel, UserMessage from rasa.core.agent import Agent app = Flask(__name__) class CustomGoogleChatChannel(InputChannel): @classmethod def name(cls) -> str: return "custom_google_chat" def blueprint(self, agent: Agent): @app.route("/webhooks/custom_google_chat/webhook", methods=["POST"]) async def webhook(): payload = request.get_json() # Extract email directly from the incoming payload user_email = payload.get("user", {}).get("email") user_text = payload.get("message", {}).get("text") # Pass the email to Rasa as metadata for use in actions await agent.handle_message( UserMessage( text=user_text, sender_id=payload.get("user", {}).get("name"), metadata={"user_email": user_email} ) ) return jsonify({"status": "success"}) return app if __name__ == "__main__": app.run(port=5005)
3. Verify Bot Permissions
Don't skip this step! Make sure your Google Chat bot has the right permissions in the Google Cloud Console:
- Navigate to your Chat bot's configuration page
- Under the "Permissions" section, enable access to user profile information (including email)
- Without this permission, the
emailfield will be missing from the payload entirely
内容的提问来源于stack exchange,提问作者sowmya s

