Rivescript条件语法逻辑理解及代码逐行执行流程咨询
First, let's format your RiveScript code properly for clarity:
! var name = Alex + my name is * * <formal> == <bot name> => Wow, we have the same name!<set name=<formal>> * <get name> == undefined => <set name=<formal>>Nice to meet you! - <set oldname=<get name>><set name=<formal>> I thought your name was <get oldname>?
Let's walk through each line's purpose and the execution flow, including the specific scenario where a user first says their name is Alex, then John.
Line 1: ! var name = Alex
This initializes a global default variable name with the value "Alex". In RiveScript, variables set with <set> are tied to the individual user's session (per-user variables), and these take priority over global variables when using <get name>.
Line 2: + my name is *
This is the trigger pattern: it matches any user input that starts with "my name is " (the * wildcard captures the rest of the input, referenced here as <formal>—this assumes you've mapped the wildcard to <formal> or it's a shorthand for <star> in your setup).
Line 3: * <formal> == <bot name> => Wow, we have the same name!<set name=<formal>>
This is a conditional response branch:
- It checks if the captured name (
<formal>) matches the bot's own name (<bot name>). - If true: it sends "Wow, we have the same name!" and updates the user's session
namevariable to the captured<formal>value. - If false: this branch is skipped, and the interpreter moves to the next condition.
Line 4: * <get name> == undefined => <set name=<formal>>Nice to meet you!
This is the second conditional branch:
- It checks if the user's session
namevariable is undefined (meaning the user hasn't shared their name before, or no session-specificnamewas set). - If true: first, it sets the user's session
nameto the captured<formal>value, then sends "Nice to meet you!". - If false: this branch is skipped, and we fall back to the default response.
Line 5: - <set oldname=<get name>><set name=<formal>> I thought your name was <get oldname>?
This is the default response (triggered when none of the above conditions are met):
- First, it saves the current value of the user's session
nameinto a new variableoldname—this is the key step you're confused about. - Next, it updates the user's session
nameto the new captured<formal>value. - Finally, it sends the message referencing the old name: "I thought your name was [old name]?".
Let's Walk Through Your Specific Scenario (Alex → John)
First input: "my name is Alex"
- The trigger pattern matches.
- Line 3: If the bot's name isn't Alex, this branch is skipped.
- Line 4:
<get name>is undefined (since no session-specificnamewas set yet—remember the globalnameis Alex, but session variables take priority, and we haven't set one). So this branch runs:<set name=<formal>>sets the user's sessionnameto "Alex".- Bot sends: "Nice to meet you!".
Second input: "my name is John"
- The trigger pattern matches again.
- Line 3: If the bot's name isn't John, this branch is skipped.
- Line 4:
<get name>now returns "Alex" (the session variable we set earlier), which is not undefined. So this branch is skipped. - Line 5 (default response) runs:
<set oldname=<get name>>saves the current sessionname("Alex") intooldname.<set name=<formal>>updates the sessionnameto "John".- Bot sends: "I thought your name was Alex?".
Why Not Just <set name=<formal>> Directly?
If we skipped saving oldname and just updated name to <formal>, we'd lose the reference to the user's previous name. The purpose of this line is to capture the old name before overwriting it, so the bot can reference it in the response. Without storing oldname, there's no way to recall what the user's name was prior to this update.
内容的提问来源于stack exchange,提问作者parminder kaur

