對話動作已於 2023 年 6 月 13 日淘汰。詳情請參閱「
對話動作已淘汰」。
工作階段儲存
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以將特定使用者的參數值儲存在對話中
不同的工作階段儲存空間您的動作之後可以在提示中使用這些儲存的值
您的 Webhook 程式碼即可存取工作階段儲存空間中的值
在必要時加入對話
在對話期間,使用 types 收集的資料都會儲存在工作階段中
如果 30 天內讀取資料不到一次
建議使用 Coldline Storage您也可以使用 Webhook 呼叫,與工作階段儲存空間中的資料互動。
如果是 Webhook 呼叫,工作階段儲存空間的狀態會透過 app.handle()
傳遞
並儲存在 session
物件中。
對話結束時,儲存在工作階段儲存空間中的資料就會失效。
讀取資料並將其寫入工作階段儲存空間
如要在工作階段儲存空間中更新或設定新值,請將該值指派給
Webhook 呼叫中 session
物件的 params
欄位。以下範例
設定「exampleColor」變更為「紅色」工作階段儲存空間:
Node.js
// Assign color to session storage
app.handle('storeColor', conv => {
let color = 'red';
conv.session.params.exampleColor = color;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {
"exampleColor": "red"
}
},
"prompt": {
"override": false
}
}
}
如要存取儲存在工作階段儲存空間中的資料,請將 Webhook 指派給變數中的變數
呼叫。以下範例會從「exampleColor」擷取值工作階段中
儲存空間:
Node.js
// Retrieve color from session storage
app.handle('getStoredColor', conv => {
let color = conv.session.params.exampleColor;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {
"exampleColor": "red"
}
},
"prompt": {
"override": false
}
}
}
如要清除先前儲存的值,請在 Webhook 呼叫中將值設為 null
。
以下範例會清除「exampleColor」的值工作階段儲存空間:
Node.js
// Clear color from session storage
app.handle('clearStoredColor', conv => {
conv.session.params.exampleColor = null;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {}
},
"prompt": {
"override": false
}
}
}
在提示中參照已儲存的值
您可以在提示中參照儲存在工作階段儲存空間中的值。如要參照
值,請使用 $session.params.PARAMETER_NAME
語法,其中
PARAMETER_NAME
是參數時,在 Webhook 中指定的名稱
設定。
例如,您先前在工作階段儲存空間中儲存了色彩值做為
參數 exampleColor
。如要在提示中存取該值,
值使用 $session.params.exampleColor
:
JSON
{
"candidates": [{
"first_simple": {
"variants": [{
"speech": "Your favorite color is $session.params.exampleColor."
}]
}
}]
}
在條件中參照已儲存的值
您也可以參照在條件中儲存在工作階段儲存空間中的值。目的地:
參照該值,請使用 session.params.PARAMETER_NAME
語法,其中 PARAMETER_NAME
是 Webhook 中指定的名稱
參數。
例如,您先前在工作階段儲存空間中儲存了色彩值做為
參數 exampleColor
,而您想要與值「red」進行比對風格
值。在條件中,您可以使用
session.params.exampleColor
。條件運算式如下所示:
條件語法
session.params.exampleColor == "red"
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eYou can utilize session storage to save user-specific parameter values during a conversation, enabling your Action to reuse them in prompts and conditions, and your webhook code to access them when needed.\u003c/p\u003e\n"],["\u003cp\u003eSession storage automatically stores data gathered using types during a conversation, and you can interact with this data via webhook calls using the \u003ccode\u003esession\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eTo modify or add values in session storage, assign them to the \u003ccode\u003eparams\u003c/code\u003e field of the \u003ccode\u003esession\u003c/code\u003e object within a webhook call; to retrieve them, assign them to a variable within a webhook call.\u003c/p\u003e\n"],["\u003cp\u003eStored values in session storage can be referenced in prompts using the \u003ccode\u003e$session.params.<PARAMETER_NAME>\u003c/code\u003e syntax and in conditions using the \u003ccode\u003esession.params.<PARAMETER_NAME>\u003c/code\u003e syntax.\u003c/p\u003e\n"],["\u003cp\u003eData stored in session storage is ephemeral and automatically deleted when the conversation concludes.\u003c/p\u003e\n"]]],[],null,["# Session storage\n\nYou can store parameter values for a specific user within a conversation in\nsession storage. Your Action can then use those stored values later in prompts\nand conditions, and your webhook code can access values in session storage for\nthe conversation when necessary.\n\nDuring a conversation, any data collected using [types](/assistant/conversational/types) is stored in session\nstorage. You can also interact with data in session storage using webhook calls.\nFor webhook calls, the state of session storage is passed in an `app.handle()`\nrequest and is stored in the [`session`](/assistant/conversational/reference/rest/v1/TopLevel/fulfill#Session) object.\n\nData stored in session storage expires when a conversation ends.\n\nRead and write data to session storage\n--------------------------------------\n\nTo update or set a new value in session storage, assign the value to the\n`params` field of the `session` object in a webhook call. The following example\nsets \"exampleColor\" to \"red\" in session storage: \n\n### Node.js\n\n```javascript\n// Assign color to session storage\napp.handle('storeColor', conv =\u003e {\n let color = 'red';\n conv.session.params.exampleColor = color;\n});\n \n```\n\n### JSON\n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {\n \"exampleColor\": \"red\"\n }\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nTo access data stored in session storage, assign it to a variable in a webhook\ncall. The following example retrieves a value from \"exampleColor\" in session\nstorage: \n\n### Node.js\n\n```javascript\n// Retrieve color from session storage\napp.handle('getStoredColor', conv =\u003e {\n let color = conv.session.params.exampleColor;\n});\n \n```\n\n### JSON\n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {\n \"exampleColor\": \"red\"\n }\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nTo clear a previously saved value, set the value to `null` in a webhook call.\nThe following example clears the value of \"exampleColor\" in session storage: \n\n### Node.js\n\n```javascript\n// Clear color from session storage\napp.handle('clearStoredColor', conv =\u003e {\n conv.session.params.exampleColor = null;\n});\n \n```\n\n### JSON\n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {}\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nReference stored values within prompts\n--------------------------------------\n\nYou can reference values stored in session storage in a [prompt](/assistant/conversational/prompts). To reference the\nvalue, use `$session.params.`\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e syntax, where\n\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e is the name given in the webhook when the parameter\nwas set.\n\nFor example, you previously stored a color value in session storage as the\nparameter `exampleColor`. To access that value in a prompt, you reference that\nvalue using `$session.params.exampleColor`: \n\n### JSON\n\n```gdscript\n{\n \"candidates\": [{\n \"first_simple\": {\n \"variants\": [{\n \"speech\": \"Your favorite color is $session.params.exampleColor.\"\n }]\n }\n }]\n}\n \n```\n\nReference stored values within conditions\n-----------------------------------------\n\nYou can also reference values stored in session storage in [conditions](/assistant/conversational/conditions). To\nreference the value, use the `session.params.`\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e\nsyntax, where \u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e is the name given in the webhook when\nthe parameter was set.\n\nFor example, you previously stored a color value in session storage as the\nparameter `exampleColor`, and you want to match it with the value \"red\" in a\ncondition. In your condition, you reference the stored value using\n`session.params.exampleColor`. Your condition expression then looks like this: \n\n### Condition syntax\n\n```text\nsession.params.exampleColor == \"red\"\n \n```"]]