Build a Bot with an API Integration

Learn how to integrate an API with your Bot.

  • Messaging

Welcome to the guide on integrating an API with your bot using Conversational Builder. This guide will walk you through the process of incorporating an API into your bot to fetch and utilize external data seamlessly.

To build the bot:

Log in to your Conversational Cloud account and navigate to Automate > Conversation Builder.

Screenshot 2024-02-15 at 2.57.08 PM.png

The Conversation Builder appears.

Screenshot 2024-02-15 at 3.05.13 PM.png

Click New Bot. The Choose a Bot Template window appears.

Screenshot 2024-02-15 at 3.10.42 PM.png

Select Custom > Custom Bot. The Bot details page appears.

Screenshot 2024-02-15 at 9.20.03 PM.png

Provide the following information:

    1. Name: Input the name for your bot into the field.

    2. Description: Input information describing your bot.

    3. Channel Type: Select the messaging option.

    4. Small Talk: Select to turn on/off the small talk option.

Small Talk provides a human-like experience by providing chitchat, pleasantries, and other trivial conversation. For more information about Small Talk see, here.

    5. Bot Language: Select the language you require the bot to use from the dropdown menu.

    6. Bot Group: Select a bot group if required, from the dropdown menu.

Bot Group is a collection of bots that can be grouped together for easier management and organization on the Bots dashboard. For more information on Bot Groups see, here.

Once done click Create Bot. The Bot Dialog page appears.

Screenshot 2024-02-15 at 9.22.08 PM.png

Click Integrations. The Integration page appears.

Screenshot 2024-02-19 at 11.13.40 AM.png

Provide the following information:

  1. Integration Name: Input the name to be used for your integration.
  2. Response Data Variable Name: Assign a variable name to store data from the API call for easy access to returned values.
  3. Integration Type: Select API from the dropdown menu.
  4. Method: Select the required API method from the dropdown menu.
  5. URL (http/https): Provide the full URL for the API.
  6. Credentials: If applicable, select the credential to use for authentication.
  7. Use default headers: If enabled, the following request headers are included automatically in the API request:
  • Accept: application/json;charset=UTF-8
  • Content-Type: application/json;charset=UTF-8
Screenshot 2024-02-19 at 11.39.11 AM.png

Scroll below and provide the remaining information:

     1. Request Headers: Provide the request parameters, if any, required by the web service.

     2. Request Parameters: Provide the request parameters, if any, required by the web service.

     3. Post Body: Provide any content body parameters required by the web service.

     4. Transform Result Script: Leverage Conversation Builder to access non-LivePerson APIs and integrate      JSON responses into your bot's dialogs using JavaScript.

You can store the API data into a Bot Variable. To do this you can use this code on the Transform Result Script section.

javascript:
var getContextData = botContext.getBotVariable("api_apiIntegration"); var getContextJsonResponse = JSON.parse(getContextData.jsonData); botContext.printDebugMessage("apiIntegrationData: " + JSON.stringify(getContextJsonResponse.api_apiIntegration)); botContext.setBotVariable("apiIntegrationData", JSON.stringify(getContextJsonResponse.api_apiIntegration), true, false);

When using the above code it is imperative that you replace "apiIntegration" with the name you provided under Integration Name.

     5. Custom Data Fields: These fields can hold integration results in key/value pairs for bot developers to reference and display in interactions.

Screenshot 2024-02-19 at 12.07.48 PM.png

Click Save. The Integration is saved.

Screenshot 2024-02-19 at 12.10.54 PM.png

Add the integration to the dialog by clicking Integrations in the right panel.

Screenshot 2024-02-19 at 12.13.22 PM.png

Select the added API integration from the dropdown menu to add it to the dialog flow. 

Screenshot 2024-02-19 at 12.40.18 PM.png

Click the custom code icon to add Pre-Process or Post-Process code as required.

With this, the bot can utilize and access the API data stored in the previously defined Bot Variable.

You can store Bot Variables with API Data to show them in the Bot responses. You can do this on the Pre-Process Code as seen in the screenshot below.

Screenshot 2024-02-19 at 12.35.57 PM.png

For the example used in the current document the Pre-Process custom code is not used. Instead the current example employs Post-Process custom code as seen in the screenshot below.

Screenshot 2024-02-19 at 12.41.49 PM.png

Once done, click Preview. The integration appears in the conversation window.

ScreenRecording2024-02-19at12.45.16PM-ezgif.com-video-to-gif-converter.gif

Congratulations! You have successfully integrated an API with your bot using Conversational Builder. Your bot is now capable of fetching and utilizing external data seamlessly, enhancing its functionality and user experience.

For further information and advanced techniques on API integrations see, here.