How to Use Modifiers for Variables

With SendPulse, you can use modifiers for chatbot variables to transform their value according to the rules you set. For example, you can format text, dates, phone numbers, and URLs.

Let's talk about variable modifiers and how to use them in your chatbots.

Modifier Syntax

You can modify data using {{ var_name | function(parameter="value") }}, where:

var_name A variable name.
function() A modifier function.
parameter A function parameter.
value A parameter value.

You can use the modifier syntax in dates, letter cases, string length, phone numbers, and URLs.

String Modifiers

str_to_lower() Converts all characters into lowercase.

For example: variable title = "TITLE."

{{ title | str_to_lower() }}

Result: title.

str_to_upper() Converts all characters into upper case.

For example, the title variable = "title."

{{ title | str_to_upper() }}

Result: TITLE.

str_capitalize() Converts all characters into a string and capitalizes its first character.

For example, the title variable = "title."

{{ title | str_capitalize() }}

Result: Title.

str_length() Returns the length of a string.

For example: title = "title".

{{ title | str_length() }}

Result: 5.

str_limit(limit="100", end="...") Truncates a string to the specified length.
  • limit is an optional parameter that defines the maximum length of a string (the default length is 100 characters).
  • end is an optional parameter that passes the characters at the end of a delimited string (the default one is "...").

    You can limit the number of characters in your message to 1000 to make sure it is delivered accurately. To do this, type in {{ info | str_limit(limit="1000") }}.

implode(separator="") Combines the elements of an array using a string separator.
  • separator is an optional parameter that defaults to an empty string.
For example, {{ $["list"] | implode(separator=", ") }} displays comma-separated data from an array obtained as a result of an external data request.
array_get(key="*.keyname")

Retrieves a value from a nested array or object using the dot notation, which is a way to access object properties and array values. A dot (.) and an asterisk (*) appear as an array slice and element number. It can look like this: array.*.object.id.

For example, if you need to get names from an array, you can use {{ $["users"] | array_get(key="*.name") | implode("\n ") }} to output the name fields from an array of objects line by line.

{{str|match(pattern="/.*some(regex).*/")}}

Outputs the string corresponding to the expression pattern. If the regular expression contains a group (), it outputs the first one.

For example, to extract a part of the link https://calendly.com/d/1n7-w04-ars/my-lesson between "d/" and "/", specify {{link | match(pattern="/.+\/d\/(.+)\/.+/")}}

Result: 1n7-w04-ars.

{{str|match_all(pattern="/.*(some)\s(regex).*/")}}

Outputs an array of strings corresponding to the expression pattern. If the regular expression contains arrays, it outputs an array of groups.

For example, to extract a part of the link https://calendly.com/d/1n7-w04-ars/my-lesson between "d/" and "/" and select the first value from the obtained results, specify {{link|match_all(pattern="/.+\/d\/(.+)\/(.+)/")| array_get(key="0")}}

Result: 1n7-w04-ars.

{{str| explode(separator="/")}}

Splits the string into an array using a separator.

For example, to split the string https://calendly.com/d/1n7-w04-ars/my-lesson using the "/" character and extract the fifth element of this array, specify {{ link | explode(separator="/")|array_get(key="4")}}

Result: 1n7-w04-ars.

{{str| substr(start="", length="")}}

Outputs a string starting from the specified character with the specified length.

For example, to output a part of the link https://calendly.com/d/1n7-w04-ars/my-lesson starting from the 23rd character and taking the next 11 characters, specify {{link | substr(start="23", length="11")}}

Result: 1n7-w04-ars.

Number and Date

number_round(precision="0", mode="ROUND_HALF_UP") Rounds a number to the specified precision (the number of digits after a decimal point).

precision can be negative or zero (by default, it is 0).

If precision is positive, the number is rounded up to the specified precision number after a decimal point.

If precision is negative, the number is rounded to the specified precision before the decimal point (rounding to the nearest power of ten). For example, if your precision equals to -1, the number is rounded to tens, and if your precision equals to -2, the number is rounded to hundreds.

You can learn more about possible values of the rounding mode from the PHP documentation.

number_format(decimals="0", decimal_separator=".", thousands_separator=",") Formats a number with grouped thousands and decimal digits.
  • decimals sets the number of decimal places. If it is 0, decimal_separator is omitted from the return value.
  • decimal_separator sets the decimal separator.
  • thousands_separator sets the thousands' separator.

    For example, to make large numbers easier to read, you can separate thousands with commas. To do this, type in number_format(thousands_separator=",")

date(from_format="d.m.Y", format="d.m.Y", modify="") Formats a date string according to the given format.
  • from_format is an optional parameter that specifies the format from which you want to convert the date data. (default: 'd.m.Y').
  • format is an optional parameter that specifies the format to which you want to convert the date data (default: 'd.m.Y').
  • modify is an optional parameter that modifies the date itself, for example, "+1 day" will add one day from the original date.

Only the following modifiers are supported: +/-day(s), +/-month(s), +/-year(s).

Read more about date modifiers in the PHP documentation.

For example, if you want to get the date in the "name of month and date" format, you can use {{ signup_date | date(format="F jS")}}.

Result: January 5th.

date_time(from_format="d.m.Y H:i", format="d.m.Y H:i", modify="")

Returns a value with the date_time type.

  • from_format is an optional parameter that specifies the format from which you want to convert the date data (default: 'd.m.Y H:i').
  • format is an optional parameter that specifies in what format the date and time data is displayed (default: 'd.m.Y H:i').
  • modify is an optional parameter that changes the date itself, for example, +1 hour will add one hour from the starting date and time.

Supported modifiers: +/-minute(s), +/-hour(s), +/-day(s), +/-month(s), +/-year(s)

Other Modifiers

phone(formig-fix-handle_api_exceptionat="INTERNATIONAL") Formats a string with a phone number according to the given format.
  • format is an optional parameter that specifies the format to which you want to convert a phone number (the default one is INTERNATIONAL).

Possible format options are:

  • INTERNATIONAL (+380 61 33 3333)
  • NATIONAL (061 333 3333)
  • E164 (+380613333333)
  • RFC3966 (tel:+380-61-333-333)
urlencode() Returns a string where all non-alphanumeric characters except for hyphens (-), underscores (_), and dots (.) must be replaced with the percent sign (%) followed by two hexadecimal numbers and spaces encoded as the plus sign (+). The string is encoded the same as web form POST data, i.e. the application/x-www-form-urlencoded content type.

The modifier can be used to pass the values received from the subscriber into the API Request, for example, as a get parameter.

Features of Use

Let's talk about possible errors that can occur when using variable modifiers and ways to combine modifiers.

You can apply modifiers to the "Message" element in a flow or campaign and all other elements where you can insert and use variables. You can get variables using the "User Variable Input" function or a response to an "API Request" element.

When you are calling a modifier function, and it takes no parameters, the "()" brackets can be omitted.

If a variable name contains the }} and | symbols, then they must be escaped with \}\} and \| in the name.

If a parameter value contains the }}, | and " symbols, they must be escaped with \}\}, \| and \". The parameter value is always given in "" as a string.

Possible Errors

If a modifier cannot process a variable, then it returns it in the same form as it was before the modifier was applied. For example, if we apply the {{ name|phone(format="INTERNATIONAL") }} modifier to the name variable = “John,” we get "John" as an answer because the variable is not a phone number.

If a modifier cannot be applied to a variable, then the user will not see an error.

Errors are displayed only if there are problems with the modifier syntax, not with the data received from the user.

Combining Modifiers

You can combine several modifiers, for example:

{{variable_name|modifier_name1(parameter_name1="value",parameter_name2="value",parameter_name3="value",)|modifier_name2()|modifier_name_N()}}

If you want to truncate your variable string length up to 10 characters and then convert all of the string characters to uppercase, this expression will look like this:

{{ String | str_limit(limit="10") | str_to_upper }}

Using Modifiers in the Chatbot Builder

You can use modifiers in the flow elements described in the table below.

The “Message” element Add a modifier to your text element to send the modified message.
The “API Request” element Add a modifier to your link field or request body to send the modified request.
The “Action” element Add a modifier to the variable value field in the "Set Variable" action to store the modified variable.

We will show you a few examples of how to use modifiers in SendPulse’s chatbot builder.

Subscriber Name Formatting Example

Think about your flow scenario. In our example, we will be collecting visitor feedback, so we want to get our guest’s name first and then give them an opportunity to share their impression.

Go to your flow or create it, add the "Message" element, and type in your text where you ask the subscriber to share their personal data. Enable the "Wait for subscriber's response" option. In the "Validate as" field, select "String" and choose a variable.

Click Apply and add the next “Message” element.

The subscriber can enter their name with a lowercase letter by mistake, but we want to address them correctly, so we use the str_title() variable modifier.

Here's what it looks like for a subscriber.

Date Formatting Example

Think about a scenario where a visitor wants to book a table for a specific date. At the same time, the client should receive a reservation confirmation after they select a date.

Add the "Message" element and type in your text where you ask the subscriber to share their personal data. Enable the "Wait for subscriber's response" option. In the "Validate as" field, select "Date" and choose a variable.

Click Apply and add the next “Message” element.

We want the reservation date to always be displayed in the same format, so we add the {{ order_date | date(format="d.m.Y") }} variable modifier.

This is the response the user will receive when they select a date.

Creating a Link to Google Calendar Example

If you want to offer users to schedule an event on the calendar, you can add available time slots using buttons or quick replies, or you can prompt for their own date and time in the "Message" element.

Store the resulting value in a variable of the "Date and time" type.

Now to get the link to create a meeting in Google Calendar, add the string to your message:

https://www.google.com/calendar/render?action=TEMPLATE&text={{title|urlencode()}}&dates={{booked_datetime|date_time(format="YmdTHi00", modify="-2 hours")}}Z%2F{{booked_datetime|date_time(format="YmdTHi00", modify="-1 hours")}}Z&ctz=Europe%2FKyiv

Where title is a variable from the chatbot audience for the name of the meeting.

booked_datetime is a variable for the selected date and time of the meeting start.

Ymd are a year, month, and day without spaces.

T is a static date and time separator.

Hi are hours and minutes without spaces.

00 are seconds set statically.

The first date and time is the start time of the meeting adjusted for the time zone (-2 hours for Kyiv time). The second date is its finish time, in an hour.

Z is the date indicator in the UTC timezone.

ctz=Europe%2FKyiv is a pointer of the time zone in which the Calendar generates the event time.

Suppose the user specifies the date and time: "28.02.2023 11:00".

And the chatbot generates a link like this:

https://www.google.com/calendar/render?action=TEMPLATE&text=%D0%9F%D1%80%D0%B5%D0%B7%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F+%C2%ABUnidragon%C2%BB&dates=20230228T090000WITH%2F20230228T100000Z&ctz=Europe%2FKyiv

The link appears in the dialog as a part of the message.

After clicking on the link, the user receives a ready-made meeting template in their calendar.

    Rate this article about "How to Use Modifiers for Variables"

    User Rating: 5 / 5

    Popular in Our Blog

    Try creating a chatbot for Facebook Messenger for free