If you don't want to control your conversation using logic, you can create a custom script like the example below to redirect your conversation flow based on the value of a variable.
You can also redirect your conversation using a data store.
To add a custom redirection script to your chatbot:
- Click Create in the left navigation, then click Scripts.
- Click +Script.
- Select to create the script From blank.
- Type a Script Name to describe the script.
- Click Create.
- In the Parameters section, create two String parameters:
- varID, with a description ID of variable to test.
- val, with a description Value that triggers redirect.
- Then create a Passage parameter:
- passage, with a description Passage to redirect to.
- In the JavaScript Code section, copy and paste the example script below.
- Click Save.
Add the script to a passage
The script in the example below will start the a specified passage if the selected variable is set to the selected value for that chatbot user. If the variable does not match that value, the script continues into the current passage.
To use the custom redirect script:
- Click Create in the left navigation, then click Conversations.
- Click the conversation of the passage where you want to add the script.
- Click the passage you want to use to redirect the conversation.
- Add a script to the bot message.
- Configure the script parameters with:
- The variable ID of the variable that will control the flow.
You can copy the variable ID from the menu next to the variable name in Variables. - The value that should trigger the redirect.
- The passage to redirect to.
- The variable ID of the variable that will control the flow.
- Click Save.
Example script
const varId = params.varID
const val = params.val
const passage = params.passage
//retrieve the user's variables. To use a constant value instead, use bot.user.getConstants();
var uservars = bot.user.getVariables();
//Check if the variable has been set, compare an empty string if it hasn't.
const varVal = uservars[varId] ? uservars[varId] : '';
// direct the flow to the passage. Use sensitivity: accent to ignore upper/lower case
if(val.localeCompare(varVal, undefined, {sensitivity: 'accent'}) === 0){
bot.flow.startPassage(passage);
}
done();