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.
The Conversation Builder appears.
Click New Bot. The Choose a Bot Template window appears.
Select Custom > Custom Bot. The Bot details page appears.
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.
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.
Once done click Create Bot. The Bot Dialog page appears.
Click Integrations. The Integration page appears.
Provide the following information:
- Integration Name: Input the name to be used for your integration.
- Response Data Variable Name: Assign a variable name to store data from the API call for easy access to returned values.
- Integration Type: Select API from the dropdown menu.
- Method: Select the required API method from the dropdown menu.
- URL (http/https): Provide the full URL for the API.
- Credentials: If applicable, select the credential to use for authentication.
- 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
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.
Click Save. The Integration is saved.
Add the integration to the dialog by clicking Integrations in the right panel.
Select the added API integration from the dropdown menu to add it to the dialog flow.
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.
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.
Once done, click Preview. The integration appears in the conversation window.
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.