Skip to main content

Function use cases

The following are some of the use cases of the Functions section in Studio.

Display weather in your bot in a caraousel node

  1. Add the weather API to the platform.
  2. Create a flow and add an API node. Select the relevant API in this node to fetch weather data.

  1. Save the response from the API node into a variable for further use.

  1. Navigate to Functions and pass the variable containing the API response and write a code that matches the dynamic format of the carousel node. Click here for the dynamic format.

  1. Include a Function node and choose the function you created in the previous step. Save the response of this node into another variable.

  1. Add a carousel node to the flow. In the Fetch from field of the carousel node, fetch the variable created in the previous step to populate the carousel with the desired data.

note

Instead of including the Function node, you can also pass the function in the Parse API response field.

Calculate 45 days before the present date

To achieve this, we will retrieve the timestamp from the Date variable in the platform.

{
//Date object

"timestamp": "Wed, 28 Jul 2022 05:04:18 GMT",
"year": 2022,
"month": 7,
"date": 28,
"day": "Thursday",
"hour": 5,
"minute": 4
}
  1. Create a flow and add a variable node. In the node, create a variable, bookindate and set the value as {{{date.timestamp}}}.

  1. Add a text node and pass the variable bookindate in it.

  1. Write the following code and pass the function into a node. Store it's response in a variable.
return new Promise(resolve => {
let bookingDate = data.variables.bookingDate;
let dt = new Date(bookingDate);

dt.setDate(dt.getDate() - 45);
console.log("Final Timestamp ---> ", dt.toLocaleString());

let pdate = dt.toLocaleString()
console.log(pdate,"PDATE IS --->");
const myArraydate = pdate.split(",");
const myArray = myArraydate[0].split("/");
let newDate = myArray[1] + "/" + myArray[0] + "/" + myArray[2]
data.variables.bookingDate = newDate;

resolve(newDate);
});

  1. Pass that variable in the text node to display the result to the end user.

Result:

drawing

Code snippet to perform sentence autocompletion

return new Promise(resolve => {
console.log("inside autoSuggestion");
let result = data.variables.autoComponents;
const { term } = data;
let suggestions = [];
result.forEach((hit) => {
if (hit.component.toLowerCase().includes(term.toLowerCase())) {
suggestions.push([hit.component, hit.component]);
}
});
resolve(suggestions);
});

Code snippet to decode BASE64

Base64 encoding represents data using 64 ASCII characters. When we receive an encrypted string via an API, we decrypt it to obtain the original object. After decryption, we can upload it to a server like Yellow's server. We can then share the URL of the uploaded object to enable access by others. Base64 encoding safeguards data integrity during transmission and simplifies its decoding back to the original format. To know more about BASE64, click here.

 let call_api = await app.executeApi('api_name', { argument: _value});
let api_data = JSON.parse(call_api.body);
app.log(api_data, "#####API DATA");

let buffer = new Buffer.from(api_data.obj_name, "base64"); // Decode the file
let file_url = await app.uploadFile(buffer, 'File.pdf'); // Upload the file to YM server
// await app.sendDocument(file_url, { caption:"FILE", filename: 'File.pdf', mime: 'application/pdf' });
// Share the File URL
await app.sendCards([
{
title: "Kindly download the same as PDF",
actions: [{
title: "DOWNLOAD",
url: file_url
}]
}
]);