Msg91 Help Doc
HELLO Contact Center
ChatBots in Hello
MSG91 Bot
Utility

Utility

The utility function includes below three cards:

  • Script

  • Delay Node

  • Condition Node

Let’s understand each of them in detail through below documentation

Calling an API

Based on user input, we retrieve data from an API and display the response to the end user. We support API calls using GET and POST methods, storing the responses in variables and using them in further interactions.

Example Implementation

Here, the user's input is stored in a variable called bookId, which is passed into the API call:

const response = await fetch('https://simple-books-api.glitch.me/books/'+_flow_variables.book_Id, {
       method: 'GET',
       redirect: 'follow'
   });
const result = await response.json();
_flow_variables.single_book_detail = result;
_flow_variables.book_name = result.name;
_flow_variables.book_author = result.author;
_flow_variables.book_type = result.type;
_flow_variables.book_price = result.price;
_flow_variables.book_availability = result.available;

// return JSON.stringify(result)
return "result";

The API response is stored in flow variables and displayed to the end user. MSG91's editor supports the fetch method for API calls.


Sending and Verifying OTP

When a specific event occurs, an OTP is sent to the user's mobile number via SMS. The user then enters the OTP in WhatsApp, and once verified, the bot confirms the authentication.

Sending OTP

const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/JSON");

const requestOptions = {
 method: "POST",
 headers: myHeaders,
 body: {}
};

const response = await fetch('https://control.msg91.com/api/v5/otp?template_id=679a05f7d6fc05753b6d9b82&mobile='+_flow_variables.customer_mobile+'&authkey=', requestOptions);
const result = await response.json();

if(result.type === "success"){
   return "true";
} else {
   return "false";
}

Once the mobile number is passed, the API sends a 4-digit OTP to the recipient.

Verifying OTP

const myHeaders = new Headers();
myHeaders.append("authkey", "");  //enter the auth key

const requestOptions = {
 method: "GET",
 headers: myHeaders,
 redirect: "follow"
};

const response = await fetch('https://control.msg91.com/api/v5/otp/verify?otp='+ _flow_variables.otp+'&mobile='+_flow_variables.customer_mobile, requestOptions);
const result = await response.json();

if(result.message === "OTP verified success"){
   return "true";
} else {
   return "false";
}

Here, the user's OTP input is stored in a variable and verified using the API. If the OTP is correct, the bot proceeds with the conversation flow.


Script Use Cases
Apr 5, 2025

In this tutorial we will be guiding you through how to use the utility script to run APIs. 

UTILITY SCRIPT

  • Use Utilities to add custom logic or scripts using JavaScript. You can execute APIs and display responses based on your logic.

  • To create additional nodes in the utility script, you need to include return statements with string variables. For example, writing return "true"; will generate a node named "true." 

  • The fallback node is a default node created to handle the flow when the utility script fails to run at any situation.

  • Multiple nodes can be created as per the requirement. 

  • In order to display the response of the api in further flow, one can use the _flow_variables.variable_name.

  •  To display this variable in any other card one can write the variable as {{variable_name}} and present it to the end user

  • Also kindly use the fetch function to call apis and perform crud operations.



You can refer this
link for the API documentation 

GET- Return status of the API

  1. In order to run an API we must write the API using the fetch function in javascript.

  2. The API is written within the fetch function along with the method and other headers.

  3. The response of the API is stored in a “result” named variable.

  4. According to the api response the result stores { “status”: “OK”} .

  5. In order to display the value of the status, create a variable using flow variables and store the value of the key you wish to

    _flow_variables.status = result.status;

  6. To create further flow we return string variables which create nodes to connect further cards.

  7. Hence write return “result”;

  8. To display the response of the API , drag Send a Message card.

  9. Use {{status}} to display the value of the status variable received through the api call

GET- Get a single Book

  1. Use Ask a text card to ask the user the Id of the book he wishes to view.

  2. Create a variable as book_Id and store the user input in this variable.

  3. Drag and utility script card and write the below code to execute the API

  4. Store the response in the result variable as shown below

  5. Use flow variables to store the api responses and display in the further cards

  6. Refer the below send a message card to view the variables to the end user

    POST- Submit an Order

    1. In order to place an order create two variables as bookId and customer_name and get their user input values from the user

    2. Once obtained, drag the utility script card and write the below script

    3. Begin by creating a headers method and writing the authentication key as provided in the API doc

    4. Next use the stringify method in json to store the user inputs in the variables as shown below

    5. Now implement the api using the fetch function in json as shown below

    6. Save the results using the flow variables and display them in further cards as shown below

Script Card
Apr 5, 2025
Prev