พื้นที่เก็บข้อมูลเซสชัน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสามารถจัดเก็บค่าพารามิเตอร์สำหรับผู้ใช้ที่ระบุภายในการสนทนาได้ใน
พื้นที่เก็บข้อมูลเซสชัน จากนั้นการดำเนินการของคุณจะใช้ค่าที่จัดเก็บไว้เหล่านั้นภายหลังในข้อความแจ้ง
และโค้ดเว็บฮุคจะเข้าถึงค่าในพื้นที่เก็บข้อมูลของเซสชันได้
การสนทนาเมื่อจำเป็น
ระหว่างการสนทนา ระบบจะจัดเก็บข้อมูลที่เก็บรวบรวมโดยใช้ประเภทไว้ในเซสชัน
พื้นที่เก็บข้อมูล คุณยังโต้ตอบกับข้อมูลในพื้นที่เก็บข้อมูลเซสชันโดยใช้การเรียกเว็บฮุคได้ด้วย
สำหรับการเรียกเว็บฮุค ระบบจะส่งสถานะของพื้นที่เก็บข้อมูลเซสชันใน app.handle()
และจัดเก็บไว้ในออบเจ็กต์ session
ข้อมูลที่จัดเก็บไว้ในพื้นที่เก็บข้อมูลของเซสชันจะหมดอายุเมื่อการสนทนาสิ้นสุดลง
อ่านและเขียนข้อมูลไปยังพื้นที่เก็บข้อมูลของเซสชัน
หากต้องการอัปเดตหรือตั้งค่าใหม่ในพื้นที่เก็บข้อมูลเซสชัน ให้กำหนดค่าให้กับ
ฟิลด์ params
ของออบเจ็กต์ session
ในการเรียกเว็บฮุค ตัวอย่างต่อไปนี้
ตั้งค่า "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
}
}
}
หากต้องการเข้าถึงข้อมูลที่จัดเก็บไว้ในพื้นที่เก็บข้อมูลเซสชัน ให้กำหนดข้อมูลดังกล่าวให้กับตัวแปรในเว็บฮุค
การโทร ตัวอย่างต่อไปนี้ดึงค่าจาก "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
}
}
}
หากต้องการล้างค่าที่บันทึกไว้ก่อนหน้านี้ ให้ตั้งค่าเป็น 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
คือชื่อที่ระบุในเว็บฮุคเมื่อพารามิเตอร์
ได้รับการตั้งค่าแล้ว
ตัวอย่างเช่น ก่อนหน้านี้คุณได้จัดเก็บค่าสีไว้ในพื้นที่เก็บข้อมูลเซสชันเป็น
พารามิเตอร์ exampleColor
หากต้องการเข้าถึงค่าดังกล่าวในพรอมต์ คุณต้องอ้างอิงว่า
ที่ใช้ $session.params.exampleColor
:
JSON
{
"candidates": [{
"first_simple": {
"variants": [{
"speech": "Your favorite color is $session.params.exampleColor."
}]
}
}]
}
อ้างอิงค่าที่จัดเก็บไว้ภายในเงื่อนไข
นอกจากนี้ คุณยังอ้างอิงค่าที่จัดเก็บไว้ในพื้นที่เก็บข้อมูลเซสชันในเงื่อนไขได้ด้วย ถึง
อ้างอิงค่าโดยใช้ session.params.PARAMETER_NAME
โดย PARAMETER_NAME
คือชื่อที่ให้ไว้ในเว็บฮุคเมื่อ
ตั้งค่าพารามิเตอร์แล้ว
ตัวอย่างเช่น ก่อนหน้านี้คุณได้จัดเก็บค่าสีไว้ในพื้นที่เก็บข้อมูลเซสชันเป็น
พารามิเตอร์ exampleColor
และคุณต้องการจับคู่กับค่า "สีแดง" ใน
ในเงื่อนไขของคุณ ให้อ้างอิงค่าที่จัดเก็บไว้โดยใช้
session.params.exampleColor
นิพจน์เงื่อนไขของคุณจะมีลักษณะดังนี้
ไวยากรณ์เงื่อนไข
session.params.exampleColor == "red"
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\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```"]]