Table of Contents

How to encode a variable

Pau Sanchez Updated by Pau Sanchez

It is common that some inputs from the users need to be processed so that they can be recognised as valid URLs by any API.

You will see in some URLs, that when you are using two words with a space, it becomes something like this: firstname%20lastname, you can do it manually but it will be painful. To fix it, here I have a solution step by step.

In Landbot 3

  1. Once you selected the variable in Landbot that needs to be encoded, we will add a Code block:

  1. In the Code block we will add the following code, where "toencode" is the variable that you want to have encoded, and "encodedvariable" is the new variable where the encoded value will be set. You can also change the names as you need.

this.setCustomData({encodedvariable:encodeURI("@{toencode}")});

  1. Now, we have a new variable called @encodedvariable, but it's value cannot be used inside Landbot yet. To be used we need to add a user input, like a Buttons type of question. Here we have an example of how it can be set:

  1. Once we pass the user input, then when we can use our new encode variable in a url or a webhook

In Landbot 2

  1. Once you selected the variable in Landbot that needs to be encoded, we will add a Code block:

  1. In the Code block we will add the following code, where "toencode" is the variable that you want to have encoded, and "encodedvariable" is the new variable where the encoded value will be set. You can also change the names as you need.

Landbot.exec('landbot-custom-data',{'encodedvariable': encodeURI("@{toencode}")})

  1. Now, we have a new variable called @encodedvariable, but it's value cannot be used inside Landbot yet. To be used we need to add a user input, like a Buttons type of question. Here we have an example of how it can be set:

  1. Once we pass the user input, then when we can use our new encode variable in a url or a webhook

How did we do?

Get the array's index of the user selection and extract information from array

How to TEST your Http Request (Webhook block)

Contact