Chatbot Overview
Conversational Bots
Intents & Entities
Intelligent Bots
Kore.ai's Approach
Kore.ai Conversational Platform
Bot Concepts and Terminology
Natural Language Processing (NLP)
Bot Types
Bot Tasks
Starting with Kore.ai Platform
How to Access Bot Builder
Working with Kore.ai Bot Builder
Building your first Bot
Getting Started with Building Bots
Using the Dialog Builder Tool
Creating a Simple Bot
Release Notes
Latest Updates
Older Releases
Deprecations
Bot Builder
Creating a Bot
Design
Develop
Storyboard
Dialog Task
User Intent Node
Dialog Node
Entity Node
Supported Entity Types
Composite Entities
Supported Time Zones
Supported Colors
Supported Company Names
Form Node
Logic Node
Message Nodes
Confirmation Nodes
Service Node
Custom Authentication
2-way SSL for Service nodes
Script Node
Agent Transfer Node
WebHook Node
Grouping Nodes
Connections & Transitions
Managing Dialogs
User Prompts
Alert Tasks
Alert Tasks
Ignore Words and Field Memory
Digital Forms
Digital Views
Knowledge Graph
Terminology
Building
Generation
Importing and Exporting
Analysis
Knowledge Extraction
Small Talk
Action & Information Task
Action Tasks
Information Tasks
Establishing Flows
Natural Language
Overview
Machine Learning
ML Model
Fundamental Meaning
NLP Settings and Guidelines
Knowledge Graph Training
Traits
Ranking and Resolver
NLP Detection
Advanced NLP Configurations
Bot Intelligence
Overview
Context Management
Session and Context Variables
Context Object
Dialog Management
Sub-Intents
Amend Entity
Multi-Intent Detection
Sentiment Management
Tone Analysis
Sentiment Management
Default Conversations
Default Standard Responses
Channel Enablement
Test & Debug
Talk to Bot
Utterance Testing
Batch Testing
Record Conversations
Publishing your Bot
Analyzing your Bot
Overview
Dashboard
Custom Dashboard
Conversation Flows
Bot Metrics
Advanced Topics
Bot Authorization
Language Management
Collaborative Development
IVR Integration
Data Table
Universal Bots
Defining
Creating
Training
Customizing
Enabling Languages
Smart Bots
Defining
Sample Bots
Github
Asana
Travel Planning
Flight Search
Event Based Bot Actions
koreUtil Libraries
Bot Settings
Bot Functions
General Settings
PII Settings
Customizing Error Messages
Manage Sessions
Bot Management
Bot Versioning
Using Bot Variables
API Guide
API Overview
API List
API Collection
SDKs
SDK Overview
SDK Security
SDK App Registration
Web SDK Tutorial
Message Formatting and Templates
Mobile SDK Push Notification
Widget SDK Tutorial
Widget SDK – Message Formatting and Templates
Web Socket Connect & RTM
Using the BotKit SDK
Installing
Configuring
Events
Functions
BotKit SDK Tutorial – Agent Transfer
BotKit SDK Tutorial – Flight Search Sample Bot
Using an External NLP Engine
Bot Administration
Bots Admin Console
Dashboard
User Management
Managing Users
Managing Groups
Managing Role
Bots Management
Enrollment
Inviting Users
Bulk Invites
Importing Users
Synchronizing Users from AD
Security & Compliance
Using Single Sign-On
Security Settings
Cloud Connector
Analytics
Billing
How Tos
Creating a Simple Bot
Creating a Banking Bot
Transfer Funds Task
Update Balance Task
Context Switching
Using Traits
Schedule a Smart Alert
Configure Digital Forms
Add Form Data into Data Tables
Configuring Digital Views
Add Data to Data Tables
Update Data in Data Tables
Custom Dashboard
Custom Tags to filter Bot Metrics
Patterns for Intents & Entities
Build Knowledge Graph
Global Variables
Content Variables
Using Bot Functions
Configure Agent Transfer
  1. Home
  2. Docs
  3. Bots
  4. How Tos
  5. How to use Bot Functions

How to use Bot Functions

In this How-To, we will explore a scenario in a Banking Bot, where Bot Functions can be used to reuse a piece of functionality. We will see how the expenditure report can be programmed as a function and can be used for both Savings Account and Credit Card.

For details on what Bot Functions are and how they are implemented in the Kore.ai Bots platform, refer here.

Problem Statement

In our Banking Bot, we have a reporting task:

  1. Monthtly Statement which gives the monthly balance amounts for a given Account number.
  2. Credit Card Statement gives an overview of the expenses for a given Credit Card.
  3. Both the reports are presented as pie chart and the input in both the cases is an array with month, amount values.

In this document, we will see how Bot Function can be used to avoid duplication of coding effort and ensure uniform presentation.

Pre-requisites

  • Bot building knowledge
  • A working bot. We will be using the Banking Bot from here.
  • Following is the Script file with two functions using the message templates to display the data (see here for more on message templates):
    • tableTemplate – takes an two-dimensional array and displays the content in a tabular format
    • pieTemplate – takes an two-dimensional array and displays the content in a pie format

    Copy the following content and save it as a .js file (say, functionEX.js)

    function tableTemplate(data){
     var message = {
       "type": "template",
       "payload": {
       "template_type": "mini_table",
       "layout": "vertical",
       "text":"Monthly Balance Statement",
       "elements": []
      }
     };
     for (i=0; i < data.length; i++) {
       var j=0;
       var element = {
         "primary":[[data[i][j]],[data[i][j+1],"right"]],
         "additional":[[data[i][j+2],data[i][j+3]],[data[i][j+4],data[i][j+5]]]
       };
     message.payload.elements.push(element);
     }
     return JSON.stringify(message);
    };
    
    function pieTemplate(data){
      var message = {
       "type": "template",
       "payload": {
       "template_type": "piechart",
       "pie_type": "regular",
       "text": "Monthly Expense Report",
       "elements": []
       }
      };
     for (i=0; i < data.length; i++) {
      var element = {
       "title": data[i][0],
       "value": data[i][1]
       };
      message.payload.elements.push(element);
     }
     return JSON.stringify(message);
    };

Implementation

  1. Open the Banking Bot
  2. From Settings -> Config Settings select Bot Functions section.
  3. Click Import to open the Import Custom Script window.
  4. Drag and drop or Browse the script file that you saved from the Pre-requisites section and click Import.
  5. Once the success message is displayed click Done.
  6. Your Script file is ready to use.
  7. Open the Bot Tasks page and create a New Dialog Task called “Monthly Statement”.
    1. Add entities to capture the Account Number and Account Type.
    2. Ideally, there would be a service call to fetch the monthly balance details. Here we will use static values for demonstration purposes.
    3. Add a Script node and enter the following – we are declaring an array carrying the balances for the months of January, February and March:
      context.monthlyBal = [["Jan",100],["Feb",200],["Mar", 300]];
    4. Add a Message node to send this data to the Bot Function declared earlier and get the report.
      • From Bot Responses section, click Manage Responses
      • Add Bot Response, select Web/Mobile Client as the Channel, switch to JavaScript tab and enter  the following code to display the values in a tabular format:
        var info = context.monthlyBal;
        print(tableTemplate(info));
    5. Add another Message node to send this data to the Bot Function declared earlier and get the report.
      • From Bot Responses section, click Manage Responses
      • Add Bot Response, select Web/Mobile Client as the Channel, switch to JavaScript tab and enter  the following code to display the values in a pie chart:
        var info = context.monthlyBal;
        print(pieTemplate(info));
  8. Close the dialog task and return to the Bot Tasks page
  9. Create a New Dialog Task called “Credit Card Statement”.
    1. Add entities to capture the Account Number.
    2. Ideally, there would be a service call to fetch the monthly expenditure values. Here we will use static values for demonstration purposes.
    3. Add a Script node and enter the following – we are declaring an array carrying the expenditure values for all the months:
      context.monthlyExpenses = [["Jan",100],["Feb",200],["Mar", 300],["Apr",100],["May",400],["June", 500],["Jul",100],["Aug",200],["Sept", 300],["Oct",100],["Nov",100],["Dec", 300]];
    4. Add a Message node to send this data to the Bot Function declared earlier and get the report.
      • From Bot Responses section, click Manage Responses
      • Add Bot Response, select Web/Mobile Client as the Channel, switch to JavaScript tab and enter  the following code to display the values in a pie chart. As you can see we are using the same function that was invoked in the previous dialog task.
        var info = context.monthlyExpenses;
        print(pieTemplate(info));
  10. Close the Dialog Task
  11. Talk to Bot and try both the dialogs. Thus we have used the same function to display the reports in multiple places.
Menu