Google Actions开发咨询:如何在会话全程持久化存储用户输入信息
Hey there! I totally get why Contexts aren't cutting it for you—their lifecycle limits make them tricky for keeping data around the whole conversation. Luckily, Session Attributes are exactly what you need here, since they're designed to persist data from the moment the Action starts until the session ends (whether the user exits or times out). Let's break down how to implement this depending on your development setup:
Using Actions Builder (Visual Interface)
If you're working with the no-code/low-code Actions Builder:
- Save data to Session Attributes: When handling an Intent where the user provides input (e.g., a "ProvideName" Intent), head to the Fulfillment tab for that Intent. Use the
conv.session.paramsobject to store the input. For example:// Extract the user's input from the intent parameters const userName = conv.intent.params.name.resolved; // Save it to session attributes conv.session.params.savedUserName = userName; conv.add(`Got it, I'll remember your name is ${userName}!`); - Retrieve data anywhere in the session: In any other Intent's Fulfillment code, you can directly access the saved attribute like this:
const storedName = conv.session.params.savedUserName; if (storedName) { conv.add(`Hey ${storedName}, nice to chat with you again!`); }
Using Actions SDK (Node.js)
If you're building with the Node.js SDK (@assistant/conversation package):
- Save user input to session attributes: In your handler for the input-capturing Intent, store the data in
conv.session.params:app.handle('CaptureUserInput', (conv) => { // Get the resolved value from the user's voice input const userText = conv.intent.params.user_input.resolved; // Store it in session attributes conv.session.params.persistedInput = userText; conv.add(`I've saved what you said. You can ask me to recall it anytime!`); }); - Access the saved data in any handler: Whenever you need to use the stored info (even in a completely different Intent), just pull it from
conv.session.params:app.handle('RecallUserInput', (conv) => { const savedData = conv.session.params.persistedInput; if (savedData) { conv.add(`You told me: ${savedData}`); } else { conv.add(`I don't have any saved input from this session yet.`); } });
Key Notes to Remember
- Session Attributes only last for the current active session—once the user closes the Action or the session times out (default is ~20 minutes of inactivity), the data is cleared. This aligns perfectly with your requirement to keep data from start to end of the Action session.
- Keep the data you store lightweight—avoid saving large chunks of text or complex objects. Stick to key values like IDs, short strings, or numbers.
- If you ever need data to persist across multiple sessions (e.g., remembering a user's preferences next time they open the Action), you'd use User Storage instead—but that's a different use case than what you're asking for here.
Hope this clears things up and helps you implement persistent session data smoothly! If you hit any specific code hiccups or have questions about edge cases, feel free to dive deeper.
内容的提问来源于stack exchange,提问作者AvidDev

