Redirect your flow with a variable

You can create a custom script like the example below to redirect your conversation flow based on the value of a variable.

To add a custom redirection script to your chatbot:

  1. Select your team and chatbot.
  2. In the Create menu, click Scripts.
  3. Click +Script.
  4. Select to create the script From blank.
  5. Type a Script Name to describe the script.
  6. Click Create.
  7. 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.
  8. Then create a Passage parameter:
    • passage, with a description Passage to redirect to.
  9. In the JavaScript Code section, copy and paste the example script below.
  10. Click Save.

Add the script to a passage

The script in the example below will redirect the chatbot to the selected passage if the selected variable is set to the selected value for that user. If the variable does not match that value, the script continues into the current passage.

To use the custom redirect script:

  1. Open the passage where you want to redirect based on the variable value.
  2. Add a script to the Bot Message.
  3. 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.
  4. 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();
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.