In addition to the default variables provided by Telerivet, you can also create your own custom variables.
There are two types of custom variables in Telerivet:
- Temporary variables, which only exist while processing a single message, and can be used for performing basic numeric computations and creating complex if/then conditions.
Temporary variables start with $ (dollar sign), for example $amount.
- Stored variables, which are stored between multiple requests to your service, and can be can be associated with a particular contact, service, phone, or your entire project.
Stored variables start with contact.vars., state.vars, service.vars, phone.vars., or project.vars.
To create a variable, add a Set variable rule.
Click (select variable) to open the list of variables:
- If you want your variable to store a different value for each contact, click contact ..., and type a variable name in place of contact.vars.example.
- If you only want your variable to be available to the current service while interacting with this contact, click state ..., and type a variable name in place of state.vars.example.
- If you want your variable to have the same value for everyone, click project ..., and type a variable name in place of project.vars.example.
- If you want your variable to have a different value for each message, type a variable name in place of $temp.
Temporary variable names always start with a dollar sign ($) so that you can tell them apart from built-in variables such as content and word1.
Custom variable names can contain letters, digits, and underscores, and should describe the meaning of the value stored in the variable.
For example, a variable that keeps track of the contact's email address could be called contact.vars.email.
After choosing a variable name, you can enter a value for the variable in the box to the right of the equals sign (=). The value can be a number or text, and can also reference other variables.
To set a variable's value as the result of a numeric calculation such as adding, subtracting, multiplying, or dividing numeric values or variables, click the small circle on the right side of the "set variable" action, and select the operation from the drop down list:
Then, enter the values or variables for the calculation in the boxes on the left and right hand side.
Suppose you want to create an SMS service that converts currency between U.S. dollars (USD) and Tanzanian shillings (TZS).
Assume the current exchange rate is $1 = TZS 1585. If someone sends the message "USD 40", your service should respond with "USD 40 = TZS 63400". If someone sends the message "TZS 10000", your service should respond with "TZS 10000 = USD 6.309...".
We can create the following temporary variables:
- $exch_rate = the current exchange rate TZS/USD.
- $tzs = The value expressed in Tanzanian shillings
- $usd = The value expressed in U.S. dollars