Last updated: 2021-10-27

Source: https://support.freshdesk.com/support/solutions/articles/50000000128-using-functions-in-the-bot-builder

Using functions in the bot builder

Setting up dialogs in your bot builder to send out preset responses to customers is pretty straightforward. You can also dynamically populate values in your dialogs to help your customers with contextual responses.

The bot builder provides you with commonly used functions for your daily operations, such as _fetching current date, separating strings by a delimiter, manipulating date and time, etc_. These Functions act as placeholders and take care of populating values for these fields dynamically. What’s more, you can also define how you want each function to behave or collect information by declaring them as Inputs. The inputs are predefined for each function, and you can also use placeholders from other dialogs, API, or custom/default properties for each of these functions.

For example, you can ask your customer their name, and while saving it, you can use a function to ensure that it is saved in the title case. Listed below are the explanations for the functions offered by the bot builder. You can use the corresponding function in your dialog to dynamically populate values in your bot.

Function name and descriptionWhat inputs do you provide?Examples
Split<br>Split a text string at the specified separator and returns the value based on the specified index.Input text: The text that needs to be split<br>Separator:<br>The character at which you need the text to be split<br>Index:<br>The position of the text that needs to be returned. The index position of the string before the separator is zero, and the index position of the string after the separator is one.Input text:<br>support@freshchat.com<br>Separator:<br>@<br>Index:<br>0<br>Output:<br>support
Convert to title case<br>Convert the formatting of a text string so that it is stored in the title case where the first letter of each word is capitalized.Input text:<br>The text that needs to be converted to the title caseInput text:<br>customer service<br>Output:<br>Customer Service
Get days between<br>Returns the count of the number of days between two specified dates (inclusive of the end date)Start date:<br>The date from when the counting should start (not included)<br>End date:<br>The date till when the counting should continue (included in the count)<br>Format:<br>The format in which you are specifying the datesStart date:<br>01-01-21<br>End date:<br>01-01-22<br>Format:<br>dd-MM-yy<br>Output:<br>365
Add minutes to date<br>Adds the specified number of minutes to the specified dateInput date:<br>The date to which the minutes need to be added<br>Minutes:<br>The number of minutes that need to be added to the input date<br>Format:<br>The format in which you are specifying the date and timeInput date:<br>01-01-21 00:15:00<br>Minutes:<br>51<br>Format:<br>dd-MM-yy HH:mm:ss<br>Output:<br>01-01-21 01:07:00
Add hours to date<br>Adds the specified number of minutes to the specified dateInput date:<br>The date to which the hours need to be added<br>Hours:<br>The number of hours that need to be added to the input date<br>Format:<br>The format in which you are specifying the date and timeInput date:<br>01-01-21 07:04:00 AM<br>Hours:<br>12<br>Format:<br>dd-MM-yy hh:mm:ss a<br>Output:<br>01-01-21 19:04:00
Add days to date<br>Adds the specified number of days to the specified dateInput date:<br>The date to which the days need to be added<br>Days:<br>The number of days that need to be added to the input date<br>Format:<br>The format in which you are specifying the dateInput date:<br>01-01-21<br>Days:<br>12<br>Format:<br>dd-MM-yy<br>Output:<br>13-01-21
Add days to date with timezone<br>Adds the specified number of days to the specified date that is denoted along with the timezoneInput date:<br>The date to which the days need to be added<br>Days:<br>The number of days that need to be added to the input date<br>Format:<br>The format in which you are specifying the date<br>Timezone:<br>The timezone that needs to be denoted along with the dateInput date:<br>01-01-2021 04:15:00 AM IST<br>Days:<br>7<br>Format:<br>dd-MM-yyyy hh:mm:ss a z<br>Timezone:<br>IST<br>Output:<br>08-01-2021 04:15:00 AM IST
Add working days to date with timezone<br>Adds the specified number of working days to the specified date that is denoted along with the timezone<br>Please note that, as of today, this function will only account for five working days per week — and will not take into account other holidays or weekday schedules.Input date:<br>The date to which the working days need to be added<br>Working days:<br>The number of working days that need to be added to the input date<br>Format:<br>The format in which you are specifying the date<br>Timezone:<br>The timezone that needs to be denoted along with the dateInput date:<br>01-01-2021 04:15:00 AM IST<br>Days:<br>7<br>Format:<br>dd-MM-yyyy hh:mm:ss a z<br>Timezone:<br>IST<br>Output:<br>12-01-2021 04:15:00 AM IST
Add working days to date<br>Adds the specified number of working days to the specified date. <br>Please note that, as of today, this function will only account for five working days per week — and will not take into account other holidays or weekday schedules.Input date:<br>The date to which the working days need to be added<br>Working days:<br>The number of working days that need to be added to the input date<br>Format:<br>The format in which you are specifying the dateInput date:<br>2021-31-01<br>Days:<br>7<br>Format:<br>yyyy-dd-MM<br>Output:<br>2021-09-02
Format date:<br>Changes the format of the date present, to a different, specified formatInput date:<br>The date whose format needs to be changed<br>Format:<br>The existing format of the specified date<br>Format:<br>The format to which the date needs to be updatedInput date:<br>2021-31-01<br>Format:<br>yyyy-dd-MM<br>Format:<br>dd-MM-yyyy<br>Output:<br>31-01-2021
Extract from list elements:<br>Split each element in a specified list based on the specified delimiter and returns one value for each element, based on the index value specifiedInput list:<br>The list of elements from which a value needs to be extracted for each list element<br>Delimiter:<br>The character or sequence present in each list element. The function will split each list element at this character.<br>Index: The position of the text that needs to be returned. The index position of the string before the delimiter is zero, and the index position of the string after the delimiter is one.Input list:<br>Room#101, Room#102, Room#103, Room#104<br>Delimiter:<br>\# <br>Index: 1 <br>Output:<br>\[101, 102, 103, 104\]
Append content:<br>It takes the specified appended content and appends it to the specified main contentMain content:<br>The content to which the text needs to be appended<br>Appended content:<br>The content which needs to get appendedMain content:<br>Hello<br>Appended content:<br>&nbsp;World<br>Output:<br>Hello World
Split into list:<br>Splits the specified string into a list based on the specified delimiterMain content:<br>The content that needs to be split into a list.<br>Delimiter:<br>The character/string that needs to be used to split the main content into a list.Main content:<br>Option 1 and Option 2 and Option 3 and Option 4<br>Delimiter:<br>and<br>Output:<br>\[Option 1, Option 2, Option 3, Option 4\]
Extract from list:<br>Fetches any element from a list based on the specified position (index) of the elementInput list:<br>The list of elements from which the element needs to be extracted<br>Index:<br>The position of the text that needs to be returned. The index value starts from zero for the first element.Main content:<br>Option 1, Option 2, Option 3, Option 4<br>Index:<br>2<br>Output:<br>Option 3
Count working days:<br>Counts the number of working days between the two specified days. <br>Please note that, as of today, this function will only account for five working days per week — and will not take into account other holidays or weekday schedules.Date 1:<br>The date from when the counting should start Date 2: The date till when the counting should continue (included in the count) Format: The format in which you are specifying the datesDate 1:<br>24-12-2021<br>Date 2: 06-01-2021<br>Format: dd-MM-yyyy<br>Output:<br>11
Add integers<br>Adds any two specified integersInput integer 1:<br>The first integer to which the second integer needs to get added.<br>Input integer 2:<br>The second integer which will get added to the first integer.Input integer 1: 123<br>Input integer 2: 4<br>Output:<br>127
Get length<br>Returns the length of the specified stringEntity:<br>The string whose length is requiredEntity:<br>Customer Service<br>Output:<br>16
Get current date:<br>Returns the current date in the specified formatFormat:<br>Specify the format in which you need the current daFormat:<br>dd-MMM-yyyy hh:mm:ss a z<br>Output:<br>12-Feb-2021 02:11:23 PM UTC
Compare dates:<br>The function will compare the primary date with the secondary date and then returns:<br>1, if the primary date is greater than the secondary date.<br>0, if both dates are the same<br>-1, if the primary date is lesser than the secondary dateDate 1:<br>The primary date<br>Date 2:<br>The secondary date<br>Format:<br>The format in which the dates have been enteredDate 1:<br>11-02-2021 Date 2:<br>03-02-2021 Format: dd-MM-yyyy<br>Output:<br>1
Compute:<br>The function calculates the value of the specified expression and returns the output with up to two decimal pointsInput expression:<br>The expression that needs to be computedInput expression:<br>8/(2\4) + (8/2)\4<br>Output:<br>17.00
Get JWT:<br>The function will take the respective inputs and give a JWT token as outputClaims:<br>A valid JSON expression that needs to be encoded. These claims include but are not limited to the issuer, subject, audience, etc.<br>JWT secret key:<br>Use a string to sign the header and payload content that is being encoded. <br>Expiry (seconds):<br>Define the period of time (in seconds) for which the JWT will be valid.<br>Algorithm identifier:<br>Choose between HS256, HS384, or HS512 as the algorithm (signing or encryption) to use to process the data contained in the JWT.Claims:<br>{<br>"sub": "1234567890",<br>"name": "John Doe",<br>"iat": 1516239022<br>}<br>JWT secret key:<br>your-256-bit-secret<br>Expiry (seconds):<br>120<br>Algorithm identifier:<br>HS256
Get JWE:<br>The function will take the respective inputs and give a JWE token as outputClaims:<br>A valid JSON expression that needs to be encoded. These claims include but are not limited to the issuer, subject, audience, etc.<br>JWT secret key:<br>Use a string to sign the header and payload content that is being encoded. <br>Expiry (seconds):<br>Define the period of time (in seconds) for which the JWT will be valid.<br>Algorithm identifier:<br>Choose between HS256, HS384, or HS512 as the algorithm (signing or encryption) to use to process the data.<br>JWE secret key:<br>Use a string to sign the header and payload content that is being encoded.<br>Source header:<br>Use a string to specify the source headerClaims:<br>"sub": "1234567890",<br>"name": "John Doe",<br>"iat": 1516239022<br>}<br>JWT secret key:<br>your-256-bit-secret<br>Expiry (seconds):<br>120<br>Algorithm identifier:<br>HS256<br>JWE secret key:<br>your-256-bit-secret-part-two<br>Source header:<br>"alg": "HS256",<br>"typ": "JWT"
Encode to base64:<br>The function will encode Username and Password with base64 encoding.Username:<br>The username that needs to be encoded<br>Password The password that needs to be encodedUsername:<br>John.doe@saulsstore.com<br>PasswordAlwaysUseStrongLongPasswords
Ternary Operator:<br>This function behaves exactly like the ternary operator, using three values. It compares the first two values, and assigns one value if true and another value if false.LHS&RHS:<br>LHS and RHS can be any inputs, and this function will compare them.<br>Operator:<br>Define what the comparison between the LHS and the RHS needs to be using one of the following operators: Equals, Not equal, Greater than, Lesser than, Greater than or equals, Lesser than or equals.<br>If true<br>Define the value that needs to be output if the comparison is true<br>Else<br>Define the value that needs to be output if the comparison is not trueLHS:<br>Order status<br>RHS:<br>Delivered<br>Operator:<br>Equals<br>If true<br>Your order has been delivered<br>Else<br>Your delivery is still pending.

``` We have only used a few formats in our examples. You can make use of any of the standardized date and time formats.

All the input fields can take inputs from existing placeholders, such as dialogs, APIs, and parameters. Similarly, you can use the output from any function as a placeholder elsewhere. ```