شرایط
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
شما می توانید منطق شرطی را در صحنه ها با استفاده از مقادیر اشیاء مدل Data انجام دهید. بخش های زیر نحو معتبر برای شرایط را شرح می دهند.
عملگرهای منطقی
اپراتور | توضیحات |
---|
&& | منطقی و. عبارات درونی به صورت تکراری ارزیابی میشوند و اگر هر عبارتی نادرست ارزیابی شود، ارزیابی کوتاه میشود. |
|| | منطقی OR. عبارات درونی به صورت تکراری ارزیابی می شوند و اگر هر عبارتی درست ارزیابی شود، ارزیابی کوتاه می شود. | ! | منطقی نه. ارزیابی بیان درونی نفی می شود |
عملگرهای عددی و رشته ای
عملگرهای عددی و رشته ای زیر پشتیبانی می شوند:
اپراتور | توضیحات |
---|
+ | اعداد یا الحاق رشته ها را اضافه کنید |
- | اعداد را کم کنید |
* | اعداد را ضرب کن |
/ | اعداد را تقسیم کنید |
بولین ها
بولین های ثابت زیر پشتیبانی می شوند:
ثابت | توضیحات |
---|
true | باید با حروف کوچک باشد |
false | باید با حروف کوچک باشد |
!false | به true ارزیابی می کند. باید با حروف کوچک باشد. |
عملگرهای مقایسه
عملگرهای مقایسه زیر ارائه شده است:
اپراتور | توضیحات |
---|
== | برابر است |
!= | برابر نیست |
< | کمتر از |
<= | کمتر از مساوی |
> | بزرگتر از |
>= | بزرگتر از مساوی |
فهرست ها و نقشه ها
لیستی به نام session.params.myList
داده می شود:
نحو | توضیحات |
---|
x in session.params.myList | اگر مقدار x در session.params.myList باشد true برمی گرداند |
myList[x] | مقدار شاخص x از myList را برمیگرداند |
size(session.params.myList) | اندازه یک لیست را برمی گرداند |
نقشه ای به نام session.params.myMap
داده می شود:
نحو | توضیحات |
---|
session.params.myMap == {"one": 1, "two":2} | اگر نقشهها برابر باشند، true را برمیگرداند |
session['params']['myMap']['one'] | مقدار را با کلید مشخص شده برمی گرداند |
size(session.params.myMap) | اندازه نقشه را برمی گرداند |
مدل داده
اشیاء زیر را می توان در شرایط صحنه استفاده کرد:
نمونه زیر نمونه ای از مدل داده کامل در JSON است:
{
"intent": {
"params": {
"<param_name>": {
"original": "five people",
"resolved": 5
}
}
},
"session": {
"params": {
"<session_params_key>": "<session_params_value>"
}
},
"scene": {
"slots": {
"status": "FINAL",
"params": {
"<slot_name>": "<slot_value>"
}
}
},
"user": {
"params": {
"<user_params_key>": "<user_params_value>"
},
"permissions": [
"DEVICE_PRECISE_LOCATION"
],
"accountLinkingStatus": "LINKED",
"verificationStatus": "VERIFIED",
"lastSeenTime": {
"seconds": 123,
"nanos": 456
},
"engagement": {
"pushNotificationIntents": [
"intent1",
"intent2"
]
}
},
"home": {
"params": {
"<home_params_key>": "<home_params_value>"
}
},
"canvas": {
"state": {
"<canvas_state_key>": "<canvas_state_value>"
}
},
"device": {
"capabilities": [
"SPEECH",
"RICH_RESPONSE",
"LONG_FORM_AUDIO",
"INTERACTIVE_CANVAS"
],
"currentLocation": {
"coordinates": {
"latitude": 37.422,
"longitude": -122.084
},
"postalAddress": {
"revision": 0,
"regionCode": "US",
"languageCode": "en",
"postalCode": "94043",
"sortingCode": "",
"administrativeArea": "California",
"locality": "Mountain View",
"sublocality": "",
"addressLines": ["1600 Amphitheatre Parkway"],
"recipients": [],
"organization": ""
}
}
},
"resources": {
"strings": {
"<resource_string_key>": "<resource_string_value>"
},
"images": {
"<resource_image_key>": "<resource_image_value>"
}
}
}
مرجع استفاده
مثالهای دستوری زیر فرض میکنند که شما با شی session.params
کار میکنید:
session.params = {
"flag": true,
"count": 10,
"name": "john smith",
"myList": [1, 2, 3],
"myMap": {"one": 1, "two":2}
}
می توانید عملیات مشروط زیر را انجام دهید:
// numbers and boolean logic
session.params.count > 0 && session.params.count < 100 // AND
session.params.count == 0 || session.params.count != 5 // OR
!(session.params.count <= 0) // NOT
// booleans
!false // true constant
session.params.flag // boolean variable
session.params.flag == true // explicitly compare with true constant
// strings
session.params.name == "john smith" // double quotes supported
session.params.name == 'john smith' // single quotes supported
session.params.name.contains("john") // substring
session.params.name + "!!!" == "john smith!!!" // string concatenation
session.params.name < "abc" // compares lexicographically
size(session.params.name) == 10 // length of string
// lists
1 in session.params.myList // "contains in list" operator
session.params.myList[0] == 1 // "index into list" operator
size(session.params.myList) == 3 // returns number of elements in the list
// maps
session.params.myMap == {"one": 1, "two":2} // compare map with json
session['params']['myMap']['one'] == 1 // index using square brackets
size(session.params.myMap) == 2 // number of entries in the map
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eDefine conversational flow logic with conditions based on data from the intent, scene, session, user, home, device, canvas, and resources objects.\u003c/p\u003e\n"],["\u003cp\u003eUtilize logical operators (\u003ccode\u003e&&\u003c/code\u003e, \u003ccode\u003e||\u003c/code\u003e, \u003ccode\u003e!\u003c/code\u003e), numerical and string operators (\u003ccode\u003e+\u003c/code\u003e, \u003ccode\u003e-\u003c/code\u003e, \u003ccode\u003e*\u003c/code\u003e, \u003ccode\u003e/\u003c/code\u003e), and comparison operators (\u003ccode\u003e==\u003c/code\u003e, \u003ccode\u003e!=\u003c/code\u003e, \u003ccode\u003e<\u003c/code\u003e, \u003ccode\u003e<=\u003c/code\u003e, \u003ccode\u003e>\u003c/code\u003e, \u003ccode\u003e>=\u003c/code\u003e) to construct conditional expressions.\u003c/p\u003e\n"],["\u003cp\u003eAccess and manipulate data within lists and maps using syntax like \u003ccode\u003ex in list\u003c/code\u003e, \u003ccode\u003elist[x]\u003c/code\u003e, \u003ccode\u003esize(list)\u003c/code\u003e, and specific notations for maps.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the comprehensive data model and usage reference for detailed information on available objects, their properties, and example syntax.\u003c/p\u003e\n"]]],["Conditional logic in scenes uses values from data model objects. Supported operators include logical (`&&`, `||`, `!`), numerical/string (`+`, `-`, `*`, `/`), and comparison (`==`, `!=`, `\u003c`, `\u003c=`, `\u003e`, `\u003e=`). Booleans (`true`, `false`) are supported. Lists use `in`, index, and `size()`, while maps allow for comparison, indexing, and `size()`. Data model objects (`intent`, `scene`, `session`, `user`, `home`, `device`, `canvas`, `resources`) can be referenced. Example syntax provided for numbers, booleans, strings, lists and maps.\n"],null,["# Conditions\n\nYou can carry out conditional logic in scenes using values from the\n[Data model](#data_model) objects. The following sections describe\nthe valid syntax for conditions.\n\nLogical operators\n-----------------\n\n| Operator | Description |\n|----------|---------------------------------------------------------------------------------------------------------------------------------------|\n| **`&&`** | Logical AND. Inner expressions are evaluated iteratively, and the evaluation is short-circuited if any expression evaluates to false. |\n| **`||`** | Logical OR. Inner expressions are evaluated iteratively, and the evaluation is short-circuited if any expression evaluates to true |\n| **`!`** | Logical NOT. The evaluation of the inner expression is negated |\n\n### Numerical and string operators\n\nThe following numerical and string operators are supported:\n\n| Operator | Description |\n|----------|-------------------------------------|\n| **`+`** | Add numbers or string concatenation |\n| **`-`** | Subtract numbers |\n| **`*`** | Multiply numbers |\n| **`/`** | Divide numbers |\n\n### Booleans\n\nThe following constant booleans are supported:\n\n| Constant | Description |\n|--------------|-----------------------------------------|\n| **`true`** | Must be lowercase |\n| **`false`** | Must be lowercase |\n| **`!false`** | Evaluates to `true`. Must be lowercase. |\n\n### Comparison operators\n\nThe following comparison operators are provided:\n\n| Operator | Description |\n|----------|---------------------|\n| **`==`** | Equals |\n| **`!=`** | Not equals |\n| **`\u003c`** | Less than |\n| **`\u003c=`** | Less than equals |\n| **`\u003e`** | Greater than |\n| **`\u003e=`** | Greater than equals |\n\n### Lists and maps\n\nGiven a list named `session.params.myList`:\n\n| Syntax | Description |\n|-----------------------------------|-------------------------------------------------------------|\n| **`x in session.params.myList`** | Returns true if the value `x` is in `session.params.myList` |\n| **`myList[x]`** | Returns the value at index `x` of `myList` |\n| **`size(session.params.myList)`** | Returns the size of a list |\n\nGiven a map named `session.params.myMap`:\n\n| Syntax | Description |\n|---------------------------------------------------|--------------------------------------|\n| **`session.params.myMap == {\"one\": 1, \"two\":2}`** | Returns `true` if maps are equal |\n| **`session['params']['myMap']['one']`** | Returns the value with specified key |\n| **`size(session.params.myMap)`** | Returns the map size |\n\nData model\n----------\n\nThe following objects can be used within scene conditions:\n\n| Syntax | Description |\n|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **`intent`** | [Matched intent parameter](/assistant/conversational/intents) data |\n| **`scene`** | [Slot-filling](/assistant/conversational/scenes#slot_filling) data |\n| **`session`** | [Session storage](/assistant/conversational/storage-session) data |\n| **`user`** | [User storage](/assistant/conversational/storage-user) data |\n| **`home`** | [Home storage](/assistant/conversational/storage-home) data |\n| **`device`** | Device [capability](/assistant/conversational/webhooks?tool=sdk#check_device_capabilities) and [location](/assistant/conversational/permissions) data |\n| **`canvas`** | [Canvas state](/assistant/interactivecanvas/web-apps#setcanvasstate) data |\n| **`resources`** | [Localized project resources](/assistant/conversational/build/projects?tool=sdk#add_resources) (audio, images, strings, etc.) data |\n\n| **Note:** These data values can also be referenced within [static prompts](/assistant/conversational/prompts).\n\nThe following is an example snippet of the full data model in JSON: \n\n {\n \"intent\": {\n \"params\": {\n \"\u003cparam_name\u003e\": {\n \"original\": \"five people\",\n \"resolved\": 5\n }\n }\n },\n \"session\": {\n \"params\": {\n \"\u003csession_params_key\u003e\": \"\u003csession_params_value\u003e\"\n }\n },\n \"scene\": {\n \"slots\": {\n \"status\": \"FINAL\",\n \"params\": {\n \"\u003cslot_name\u003e\": \"\u003cslot_value\u003e\"\n }\n }\n },\n \"user\": {\n \"params\": {\n \"\u003cuser_params_key\u003e\": \"\u003cuser_params_value\u003e\"\n },\n \"permissions\": [\n \"DEVICE_PRECISE_LOCATION\"\n ],\n \"accountLinkingStatus\": \"LINKED\",\n \"verificationStatus\": \"VERIFIED\",\n \"lastSeenTime\": {\n \"seconds\": 123,\n \"nanos\": 456\n },\n \"engagement\": {\n \"pushNotificationIntents\": [\n \"intent1\",\n \"intent2\"\n ]\n }\n },\n \"home\": {\n \"params\": {\n \"\u003chome_params_key\u003e\": \"\u003chome_params_value\u003e\"\n }\n },\n \"canvas\": {\n \"state\": {\n \"\u003ccanvas_state_key\u003e\": \"\u003ccanvas_state_value\u003e\"\n }\n },\n \"device\": {\n \"capabilities\": [\n \"SPEECH\",\n \"RICH_RESPONSE\",\n \"LONG_FORM_AUDIO\",\n \"INTERACTIVE_CANVAS\"\n ],\n \"currentLocation\": {\n \"coordinates\": {\n \"latitude\": 37.422,\n \"longitude\": -122.084\n },\n \"postalAddress\": {\n \"revision\": 0,\n \"regionCode\": \"US\",\n \"languageCode\": \"en\",\n \"postalCode\": \"94043\",\n \"sortingCode\": \"\",\n \"administrativeArea\": \"California\",\n \"locality\": \"Mountain View\",\n \"sublocality\": \"\",\n \"addressLines\": [\"1600 Amphitheatre Parkway\"],\n \"recipients\": [],\n \"organization\": \"\"\n }\n }\n },\n \"resources\": {\n \"strings\": {\n \"\u003cresource_string_key\u003e\": \"\u003cresource_string_value\u003e\"\n },\n \"images\": {\n \"\u003cresource_image_key\u003e\": \"\u003cresource_image_value\u003e\"\n }\n }\n }\n\nUsage reference\n---------------\n\nThe following syntax examples assumes you are working\nwith `session.params` object: \n\n session.params = {\n \"flag\": true,\n \"count\": 10,\n \"name\": \"john smith\",\n \"myList\": [1, 2, 3],\n \"myMap\": {\"one\": 1, \"two\":2}\n }\n\nYou can carry out the following conditional operations: \n\n // numbers and boolean logic\n session.params.count \u003e 0 && session.params.count \u003c 100 // AND\n session.params.count == 0 || session.params.count != 5 // OR\n !(session.params.count \u003c= 0) // NOT\n\n // booleans\n !false // true constant\n session.params.flag // boolean variable\n session.params.flag == true // explicitly compare with true constant\n\n // strings\n session.params.name == \"john smith\" // double quotes supported\n session.params.name == 'john smith' // single quotes supported\n session.params.name.contains(\"john\") // substring\n session.params.name + \"!!!\" == \"john smith!!!\" // string concatenation\n session.params.name \u003c \"abc\" // compares lexicographically\n size(session.params.name) == 10 // length of string\n\n // lists\n 1 in session.params.myList // \"contains in list\" operator\n session.params.myList[0] == 1 // \"index into list\" operator\n size(session.params.myList) == 3 // returns number of elements in the list\n\n // maps\n session.params.myMap == {\"one\": 1, \"two\":2} // compare map with json\n session['params']['myMap']['one'] == 1 // index using square brackets\n size(session.params.myMap) == 2 // number of entries in the map"]]