Skip to main content

Function

Functions allow you to implement custom logic and perform various actions within your bot. It offers flexibility beyond the bot's built-in capabilities.

Some of the common examples for writing custom logic include extracting specific information from API responses, performing mathematical calculations, decoding and much more. The possibilities extend far beyond these examples, making Functions a versatile feature to use the bot to the fullest.

Create a function

This section covers the format for writing custom logic, the available arguments for data retrieval, and how to create functions using this information.

Format of functions

In the following snippet, you need to replace the line 'Your logic goes here' with your own code to implement the desired functionality.

return new Promise(resolve => {
// Your logic goes here
resolve();
});

When you click + Add new function, the platform automatically provides you with this pre-built snippet.

Function arguments

In your code, you can use the following arguments to access specific pieces of information.

argData typeUse
data.variables.<variable_name>StringTo access any bot variable in the function.
data.channelStringTo access channel names like whatsapp, yellowmessenger, facebook, etc.
data.profileObjectContains user properties such as name, number, email, city , country, language.
data.senderStringUser ID
data.botStringBot ID
data.messageStringTo access Last/latest user message in the conversation.
data.event.<event_name>ObjectTo access events in the function.
ymLib.args.apiResponseanyTo access API response in API transformation function.
ymLib.args.loggerObjectCan be used to add logs.
context.historyObjectContains history of nodes visited by user.
prediction.intentsStringTo get intents predicted from the user message.
prediction.entitiesStringTo get entities predicted from the user message.

To access user specific information, use the arguments mentioned in the following table:

User property nameargData typeDescription
userIddata.user.userIdstringUnique identifier for a user.
As per your setting, this could be a mobile number, email address, or any unique identifier. For more details, see here.
firstNamedata.user.firstNamestringThe first name of the user
lastNamedata.user.lastNamestringLast name of the user
genderdata.user.genderstringGender of the user
countrydata.user.countrystringCountry of the user. In bot conversation, it identifies this property based on the location of the user’s IP address
citydata.user.citystringCity of the user. In bot conversation, it identifies this property based on the location of the user’s IP address
languagedata.user.languagestringPreferred language of the user
timezonedata.user.timezonestringIdentifies based on the location of user's IP address
emaildata.user.emailemailEmail address of the user
phonedata.user.phonephonePhone number of the user with country code
lastChanneldata.user.lastChannelstringRecent channel (set up on yellow.ai) where the user had an active session
You can update this property to cover touch-points/channels outside yellow.ai.
dobdata.user.dobdateDate of birth of the user.
emailOptindata.user.emailOptinbooleanSubscription status of the user to emails. The value is true if a user has subscribed to your emails, else it is false
smsOptindata.user.smsOptinbooleanSubscription status of the user to SMS. The value is true if a user has subscribed to your SMS, else it is false
whatsAppOptindata.user.whatsAppOptinbooleanWhatsApp subscription status of the user. This is true if a user has subscribed to your WhatsApp messages, else it is false.
tagsdata.user.tagslistList of groups associated with the user. You can add tags to users manually.

Create a new function

  1. Go to Studio > Functions > + Add new function.

  1. Provide a name to your function and click Add function.

  1. Type your custom logic and click Save.

  1. Click the warnings option at the bottom to rectify the displayed warnings/errors.

Tools and other settings

This section has some funcionalities common to the entire bot.

Tools

This is a global testing and settings tab. Click here to learn more.

Logs

This option shows all the function related logs, you can learn more about this by clicking here.

Compare

This option compares the function between staging and production environments depending on the environment the bot is currently in.

Flows

Flows allow quick copying and accessing of flows using their names. You can easily copy the flow name and click the flow link to be redirected to the specific flow. This feature also allows you to view the nodes used in each flow.

Localization

The Localization feature lets you add a code that handles the translation process. To know more, click here.

Using functions in flows

Functions are used in flows via the Function node.

The following is an example where the function node is used to convert minutes to seconds.

  1. Create a flow with the Question node to take an input from the user and store the response in a variable, pizzamin.

  1. Go to Functions, pass this variable and write a code to convert minutes to seconds.

For example,

return new Promise(resolve => {
let minutesTaken = data.variables.pizzamin;
let secondsTaken = minutesTaken * 60;
let stringSeconds = secondsTaken.toString();
resolve(stringSeconds);
});

  1. Include a function node, pass the function created in the previous step, and store the response in another variable, pizzasec.

  1. To display the result to the end user, include a text node and choose the pizzasec variable.

Result:

drawing

What Next?

  • Create your own function and use it by adding an API node in the flow.
  • You can visit our community and share your ideas with other fellow bot builders.