koreUtil libraries provide pre-written JavaScript functions which make common or complex tasks easy to implement. These libraries often target specific tasks such as setting up recurring rules for calendars, parsing and manipulation of dates, conversion of dates between timezones, tools to simplify programming with strings, numbers, arrays, functions, and objects, etc.
How to Use
koreUtil libraries can be used anywhere in the AI Agent where there is flexibility to write JavaScript code such as script node, message node, confirmation node, entity node, standard responses, answers to FAQs, small talk, event handlers, etc.
List of koreUtil Libraries
Below is the list of koreUtil libraries provided by the Platform:
| Library | Description |
|---|
| koreUtil.rrule | Creates and interprets recurring calendar rules in human-readable format. |
| koreUtil.moment | Validates, manipulates, and formats dates. |
| koreUtil.intl | Provides locale-sensitive string comparison, number formatting, and date/time formatting. |
| koreUtil.momenttz | Formats dates in any timezone and converts dates between timezones. |
| koreUtil.xml2js | Parses XML to JSON and converts JSON back to XML. |
| koreUtil.hash | Supports SHA cryptographic hashing in JavaScript. |
| koreUtil._ | Provides utility functions for common programming tasks such as array manipulation and object filtering. |
| koreUtil.getCurrentOptions | Retrieves the current template for language selection standard response options and authorization URLs. |
| koreUtil.getAmbiguousIntents | Returns the list of ambiguous intents with details when the Ambiguity Intents Identified event is triggered. |
| koreUtil.getSessionId | Retrieves the current conversation session ID. |
| koreUtil.closeConversationSession | Force-closes any active conversation session from any JavaScript-enabled component. |
| koreUtil.autoTranslate | Automatically translates AI Agent responses to the user’s language or a specified language. |
| koreUtil.getFormDefinition | Retrieves the form definition including form meta, components, and messages. |
koreUtil.rrule
koreUtil.rrule is a platform-offered JS library for creating new recurrence rules, interpreting and reading them in human-readable format.
Usage Example:
context.rule = new koreUtil.rrule({
freq: koreUtil.rrule.WEEKLY,
interval: 5,
byweekday: [koreUtil.rrule.MO, koreUtil.rrule.FR],
dtstart: new Date(2012, 1, 1, 10, 30),
until: new Date(2012, 12, 31)
});
context.between=context.rule.between(new Date(2012, 7, 1), new Date(2012, 8, 1));
context.readableFormat=context.rule.toText();
Output:
-
For
context.between:
[
"2012-08-27T10:30:00.000Z",
"2012-08-31T10:30:00.000Z"
]
-
For
context.readableFormat:
every 5 weeks on Monday, Friday until January 31, 2013
koreUtil.moment
koreUtil.moment is a platform-offered JS library for validating, manipulating, and formatting dates.
Usage Example:
context.german=koreUtil.moment().locale('de').format('LLLL');
Output:
For context.german:
Montag, 18. November 2019 01:43
koreUtil.intl
koreUtil.intl is a platform-offered JS library for language-specific string comparison, number formatting, and date and time formatting.
Usage Example:
context.formattedNumberUK = koreUtil.intl.NumberFormat('en-GB').format(123456.789);
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
context.USdate=koreUtil.intl.DateTimeFormat('en-US').format(date);
Output:
- For
context.formattedNumberUK: 123,456.789
- For
context.USdate: 12/20/2012
koreUtil.momenttz
koreUtil.momenttz is a platform-offered JS library for the formatting of dates in any timezone and converting dates between timezones.
Usage Example:
var jun = koreUtil.moment("2014-06-01T12:00:00Z");
context.newyork= koreUtil.momenttz(jun,'America/New_York').format('ha z');
context.tokyo = koreUtil.moment().tz('Asia/Tokyo').format('ha z');
context.sydney = koreUtil.moment().tz('Australia/Sydney').format('ha z');
Output:
- For
context.newyork: 5am PDT
- For
context.tokyo: 7pm JST
- For
context.sydney: 9pm AEDT
koreUtil.xml2js
koreUtil.xml2js is a Platform-offered JS library for parsing XML to JSON and vice versa.
Usage Example:
var obj = {name: "John", Surname: "Doe", age: 23};
var builder = new koreUtil.xml2js.Builder();
context.xml = builder.buildObject(obj);
koreUtil.hash
koreUtil.hash is a Platform offered JS library that supports SHA on JavaScript.
Usage Example:
context.hashString= koreUtil.hash('sha256').update('42').digest('hex');
Output:
For context.hashString:
73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
koreUtil._
koreUtil._ is a Platform-offered JS library that provides utility functions for common programming tasks.
Usage Example:
context.chunkArray = koreUtil._.chunk(['a', 'b', 'c', 'd'], 2);
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
context.filterActive =koreUtil._.pluck(koreUtil._.filter(users, { 'age': 36, 'active': true }), 'user');
Output:
- For
context.chunkArray: [["a","b"],["c","d"]]
- For
context.filterActive:
koreUtil.getCurrentOptions
koreUtil.getCurrentOptions is a platform-offered JS library that provides utility functions to get the current template for language selection standard response options. This can be used to customize the standard response. Post the release of v8.1, this function has been enhanced to return the authorization URL used in the standard response when end-user authorization is requested.
Following is the return format for each of the situations:
-
For the language selection standard response:
{"list_of_languages": [
{"title": "English","value": "English"},
{"title": "Deutsche","value": "Deutsche"},
{"title": "Español","value": "Español"}
]}
-
For the language ambiguity standard response:
{"current_language": "Español",
"list_of_new_languages": [
{"title": "English","value": "English"},
{"title": "Deutsche","value": "Deutsche"},
{"title": "Español","value": "Español"}
]}
-
For the language switch standard response:
{"new_language": "Español","current_language": "English"}
-
For User authorization request standard response:
{
"authorizationURL": "https:///r/xxxxxxxxxxxxx"
}
Usage Example 1: To get the standard response for “Ask if the user would like to select any of the languages”:
var info = koreUtil.getCurrentOptions();
var message = {
"type": "template",
"payload": {
"template_type": "button",
"text": "please select your language",
"subText": "Button Template Description",
"buttons": []
}
};
for (i = 0; i < info.list_of_languages.length; i++) {
var button = {
"type": "postback",
"title": info.list_of_languages[i].title,
"payload": info.list_of_languages[i].value
};
message.payload.buttons.push(button);
}
print(JSON.stringify(message));
Usage Example 4: To get the standard response for “User needs to authorize or re-authorize”:
var info = koreUtil.getCurrentOptions();
var message = {
type: 'template',
payload: {
template_type: 'button',
text: 'Please authorize before we continue...',
buttons: [
{
type: 'url',
title: 'Click here to authorize',
url: info.authorizationURL
}
]
}
};
print(JSON.stringify(message));
koreUtil.getAmbiguousIntents
The koreUtil.getAmbiguousIntents util function is used to obtain the ambiguous intents list when the Ambiguity Intents Identified event gets triggered. Along with the list of ambiguous intents, the retrieved context object contains additional details such as ambiguity reasons and the engine name that detects the intent (ML, FM, or KG).
Sample Response:
{
"intents": [
{
"intent": "Transfer Money",
"name": "Transfer Money",
"intentType": "dialog",
"mlScore": 100,
"rrScore": 7980,
"identifyingEngines": {
"ml": true
}
},
{
"intent": "Pay Bills",
"name": "Pay Bills",
"intentType": "dialog",
"rrScore": 0,
"identifyingEngines": {
"traits": true
}
}
],
"cause": "multipleChoices",
"userInput": "How do I make a payment or transfer"
}
koreUtil.getSessionId
koreUtil.getSessionId is a Platform offered JS library for obtaining the current conversation session ID.
Usage Example:
var sessionId = koreUtil.getSessionId();
koreUtil.closeConversationSession
koreUtil.closeConversationSession is a Platform function for force closing any active conversation. This function can be invoked from any component that supports JavaScript.
Usage Example:
koreUtil.closeConversationSession();
koreUtil.autoTranslate
koreUtil.autotranslate is a platform function that lets you automatically translate the AI Agent’s response from a language in which it is configured to the language in which the user is interacting or a language of your choice.
To use the koreUtil.autoTranslate function for language translation, you must have the Access Key provided by your translation service provider, such as Google Translator, Microsoft Translator, etc.
| Response Types | Components | Translation Behavior |
|---|
| Plain Text Responses | Refers to plain text responses defined anywhere in the application. For example, prompts, responses, etc. | Default: Auto-translated |
| Advanced / JavaScript based Responses | Refers to responses defined using JavaScript. | Default: No auto-translation |
| Auto-formatted Responses | Refers to the responses that are auto-formatted by the Platform as per the channel specifications. | Default: Auto-translated |
Usage Example:
koreUtil.autoTranslate();
Use Case-1: To translate part of a sentence in the user’s language:
{{koreUtil.autoTranslate('Welcome! I am a virtual assistant powered by ')}} General Insurance
Use Case-3: To translate the bot responses in a particular language by passing the language code:
var message = koreUtil.autoTranslate("This is a sample message", "de");
print(message);
The koreUtil.getFormDefinition allows you to get the form definition, which consists of the form meta, form components, and form messages.
Usage Example:
let formDef = koreUtil.getFormDefinition();