{"id":22924,"date":"2019-11-26T07:12:38","date_gmt":"2019-11-26T07:12:38","guid":{"rendered":"https:\/\/developer.kore.ai\/v9-0\/?p=22924"},"modified":"2023-06-28T08:53:06","modified_gmt":"2023-06-28T07:53:06","slug":"koreutil-libraries","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v9-0\/docs\/bots\/advanced-topics\/koreutil-libraries\/","title":{"rendered":"koreUtil Libraries"},"content":{"rendered":"<section class=\"l-section wpb_row height_auto\"><div class=\"l-section-h i-cf\"><div class=\"g-cols vc_row type_default valign_top\"><div class=\"vc_col-sm-12 wpb_column vc_column_container\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<p><strong>koreUtil libraries<\/strong> provide pre-written JavaScript functions which makes common or complex tasks easy to implement. These libraries often target specific tasks such as setting up recurring rules for calendar, parsing and manipulation of dates, conversion of dates between timezone, tools to simplify programming with strings, numbers, arrays, functions and objects etc.<\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h2><span class=\"ez-toc-section\" id=\"How_to_Use\">How to Use<\/span><\/h2>\n<p>koreUtil libraries can be used anywhere in the bot where there is a 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.<\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h2><span class=\"ez-toc-section\" id=\"List_of_koreUtil_libraries\">List of koreUtil libraries<\/span><\/h2>\n<p>Below is the list of koreUtil libraries provided by the platform:<\/p>\n<ul>\n<li><a href=\"#koreUtilrrule\">koreUtil.rrule<\/a><\/li>\n<li><a href=\"#koreUtilmoment\">koreUtil.moment<\/a><\/li>\n<li><a href=\"#koreUtilintl\">koreUtil.intl<\/a><\/li>\n<li><a href=\"#koreUtilmomenttz\">koreUtil.momenttz<\/a><\/li>\n<li><a href=\"#koreUtilxml2js\">koreUtil.xml2js<\/a><\/li>\n<li><a href=\"#koreUtilhash\">koreUtil.hash<\/a><\/li>\n<li><a href=\"#koreUtil\">koreUtil._<\/a><\/li>\n<li><a href=\"#koreUtilgetCurrentOptions\">koreUtil.getCurrentOptions<\/a><\/li>\n<li><a href=\"#koreUtilgetSessionId\">koreUtil.getSessionId<\/a><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilrrule\">koreUtil.rrule<\/span><\/h3>\n<p>koreUtil.rrule is platform offered JS library for creating new recurrence rules, interpreting and reading them in human readable format.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>context.rule = new koreUtil.rrule({<br \/>freq: koreUtil.rrule.WEEKLY,<br \/>interval: 5,<br \/>byweekday: [koreUtil.rrule.MO, koreUtil.rrule.FR],<br \/>dtstart: new Date(2012, 1, 1, 10, 30),<br \/>until: new Date(2012, 12, 31)<br \/>});<\/code><br \/><code>context.between=context.rule.between(new Date(2012, 7, 1), new Date(2012, 8, 1));<\/code><br \/><code>context.readableFormat=context.rule.toText();<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.between<br \/><code>[<br \/>\"2012-08-27T10:30:00.000Z\",<br \/>\"2012-08-31T10:30:00.000Z\"<br \/>]<\/code><\/li>\n<li>For context.readableFormat<br \/><code>every 5 weeks on Monday, Friday until January 31, 2013<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilmoment\">koreUtil.moment<\/span><\/h3>\n<p>koreUtil.moment is platform offered JS library for validating, manipulating, and formatting dates.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>context.german=koreUtil.moment().locale('de').format('LLLL');<\/code><\/p>\n<p><b>Output:<\/b><br \/>For context.german:<br \/><code>Montag, 18. November 2019 01:43<\/code><\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilintl\">koreUtil.intl<\/span><\/h3>\n<p>koreUtil.intl is platform offered JS library for language specific string comparison, number formatting, and date and time formatting.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>context.formattedNumberUK = koreUtil.intl.NumberFormat('en-GB').format(123456.789);<br \/>var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));<br \/>context.USdate=koreUtil.intl.DateTimeFormat('en-US').format(date);<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.formattedNumberUK:<br \/><code>123,456.789<\/code><\/li>\n<li>For context.USdate<br \/><code>12\/20\/2012<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilmomenttz\">koreUtil.momenttz<\/span><\/h3>\n<p>koreUtil.momenttz is platform offered JS library for formatting of dates in any timezone and convert dates between timezones. <\/p>\n<p><b>Usage Example:<\/b><br \/><code>var jun = koreUtil.moment(\"2014-06-01T12:00:00Z\");<br \/>context.newyork= koreUtil.momenttz(jun,'America\/New_York').format('ha z');<br \/>context.tokyo = koreUtil.moment().tz('Asia\/Tokyo').format('ha z');<br \/>context.sydney = koreUtil.moment().tz('Australia\/Sydney').format('ha z');<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.newyork:<br \/><code>5am PDT<\/code><\/li>\n<li>For context.tokyo:<br \/><code>7pm JST<\/code><\/li>\n<li>For context.sydney:<br \/><code>9pm AEDT<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilxml2js\">koreUtil.xml2js<\/span><\/h3>\n<p>koreUtil.xml2js is platform offered JS library for parsing xml to json and vice versa.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>var obj = {name: \"John\", Surname: \"Doe\", age: 23};<br \/>var builder = new koreUtil.xml2js.Builder();<br \/>context.xml = builder.buildObject(obj);<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.xml:<br \/><code><!--?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?-->  John Doe 23 <\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilhash\">koreUtil.hash<\/span><\/h3>\n<p>koreUtil.hash is platform offered JS library that supports SHA on JavaScript.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>context.hashString= koreUtil.hash('sha256').update('42').digest('hex');<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.hashString:<br \/><code>73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtil\">koreUtil._<\/span><\/h3>\n<p>koreUtil._ is a platform offered JS library which provides utility functions for common programming tasks.<\/p>\n<p><b>Usage Example:<\/b><br \/><code>context.chunkArray = koreUtil._.chunk(['a', 'b', 'c', 'd'], 2);<br \/>var users = [<br \/>{ 'user': 'barney', 'age': 36, 'active': true },<br \/>{ 'user': 'fred',   'age': 40, 'active': false }<br \/>];<br \/>context.filterActive =koreUtil._.pluck(koreUtil._.filter(users, { 'age': 36, 'active': true }), 'user');<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For context.chunkArray:<br \/><code>[[\"a\",\"b\"],[\"c\",\"d\"]]<\/code><\/li>\n<li>For context.filterActive:<br \/><code>[<br \/>\"barney\"<br \/>]<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilgetCurrentOptions\">koreUtil.getCurrentOptions<\/span><\/h3>\n<p>koreUtil.getCurrentOptions is a platform offered JS library which provides utility functions to get the current template for language selection standard response options. This can be used to customize the standard response.<br \/>\nPost 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.<\/p>\n<p>Following is the return format for each of the situations:<\/p>\n<ul>\n<li>For the language selection standard response:\n<pre>{\"list_of_languages\": [\r\n   {\"title\": \"English\",\"value\": \"English\"},\r\n   {\"title\": \"Deutsche\",\"value\": \"Deutsche\"},\r\n   {\"title\": \"Espa\u00f1ol\",\"value\": \"Espa\u00f1ol\"}\r\n]}<\/pre>\n<\/li>\n<li>For the language ambiguity standard response:\n<pre>{\"current_language\": \"Espa\u00f1ol\",\r\n      \"list_of_new_languages\": [\r\n         {\"title\": \"English\",\"value\": \"English\"},\r\n         {\"title\": \"Deutsche\",\"value\": \"Deutsche\"},\r\n         {\"title\": \"Espa\u00f1ol\",\"value\": \"Espa\u00f1ol\"}\r\n  ]}<\/pre>\n<\/li>\n<li>For the language switch standard response:\n<pre>{\"new_language\": \"Espa\u00f1ol\",\"current_language\": \"English\"}<\/pre>\n<\/li>\n<li>For User authorization request standard response:\n<pre>{\r\n    \"authorizationURL\": \"https:\/\/\/r\/xxxxxxxxxxxxx\"\r\n}<\/pre>\n<\/li>\n<\/ul>\n<p>Following are the usage examples of how the above utility can be used to render the message in a button template, you can use any template as per your requirements.\u00a0 <a href=\"\/v9-0\/v9-0\/docs\/bots\/sdks\/message-templates\/\" target=\"_blank\" rel=\"noopener noreferrer\">See here for more on message templates<\/a>.<\/p>\n<p><b>Usage Example 1:<\/b> To get the standard response for &#8220;<em>Ask if the user would like to select any of the languages<\/em>&#8220;<\/p>\n<pre>var info = koreUtil.getCurrentOptions();\r\nvar message = {\r\n  \"type\": \"template\",\r\n  \"payload\": {\r\n    \"template_type\": \"button\",\r\n    \"text\": \"please select your laguage\",\r\n    \"subText\": \"Button Template Description\",\r\n    \"buttons\": []\r\n    }\r\n  };\r\nfor (i = 0; i &lt; info.list_of_languages.length; i++) {\r\n  var button = {\r\n    \"type\": \"postback\",\r\n    \"title\": info.list_of_languages[i].title,\r\n    \"payload\": info.list_of_languages[i].value\r\n    };\r\n\r\n  message.payload.buttons.push(button);\r\n }\r\nprint(JSON.stringify(message));\r\n<\/pre>\n<p><b>Usage Example 2:<\/b> To get the standard response for &#8220;<em>Ask if the user would like to switch to any of the new languages detected during a conversation<\/em>&#8220;<\/p>\n<pre>var info = koreUtil.getCurrentOptions();\r\nvar message = {\r\n    \"type\": \"template\",\r\n    \"payload\": {\r\n        \"template_type\": \"button\",\r\n        \"text\": \"language abiguity response for the current language\" + info.current_language,\r\n        \"subText\": \"Button Template Description\",\r\n        \"buttons\": []\r\n    }\r\n};\r\nfor (i = 0; i &lt; info.list_of_new_languages.length; i++) {\r\n    var button = {\r\n        \"type\": \"postback\",\r\n        \"title\": info.list_of_new_languages[i].title,\r\n        \"payload\": info.list_of_new_languages[i].value\r\n    };\r\n\r\n    message.payload.buttons.push(button);\r\n}\r\nprint(JSON.stringify(message));\r\n<\/pre>\n<p><b>Usage Example 3:<\/b> To get the standard response for &#8220;<em>Ask if the user would like to switch to any of the new languages detected during a conversation<\/em>&#8220;<\/p>\n<pre>var info = koreUtil.getCurrentOptions();\r\nvar message = {\r\n    \"type\": \"template\",\r\n    \"payload\": {\r\n        \"template_type\": \"button\",\r\n        \"text\": \"would you like to switch to new language detected: current language \" + info.current_language + \"new language : \" + info.new_language,\r\n        \"subText\": \"Button Template Description\",\r\n        \"buttons\": []\r\n    }\r\n};\r\nvar options = [\"yes\", \"no\"]\r\nfor (i = 0; i &lt; options.length; i++) {\r\n    var button = {\r\n        \"type\": \"postback\",\r\n        \"title\": options[i],\r\n        \"payload\": options[i]\r\n    };\r\n    message.payload.buttons.push(button);\r\n}\r\nprint(JSON.stringify(message));\r\n<\/pre>\n<p><b>Usage Example 4:<\/b> To get the standard response for &#8220;<em>User needs to authorize or re-authorize<\/em>&#8220;<\/p>\n<pre>var info = koreUtil.getCurrentOptions();\r\n\r\nvar message = {\r\n    type: 'template',\r\n    payload: {\r\n        template_type: 'button',\r\n        text: 'Please authorize before we continue...',\r\n        buttons: [\r\n            {\r\n                type: 'url',\r\n                title: 'Click here to authorize',\r\n                url: info.authorizationURL\r\n            }\r\n        ]\r\n    }\r\n};\r\n\r\nprint(JSON.stringify(message));\r\n<\/pre>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h3><span class=\"ez-toc-section\" id=\"koreUtilgetSessionId\">koreUtil.getSessionId<\/span><\/h3>\n<p>koreUtil.getSessionId is a platform offered JS library for obtaining the current conversation session Id.<\/p>\n<p><b>Usage Example:<\/b><br \/>\n<code>var sessionId = koreUtil.getSessionId();<\/code><\/p>\n<p><b>Output:<\/b><\/p>\n<ul>\n<li>For sessionId:<br \/>\n<code>5x54321x11xx87654321xx12<\/code><\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"koreUtil libraries provide pre-written JavaScript functions which makes common or complex tasks easy to implement. These libraries often target specific tasks such as setting up recurring rules for calendar, parsing and manipulation of dates, conversion of dates between timezone, tools to simplify programming with strings, numbers, arrays, functions and objects etc. How to Use koreUtil...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-22924","post","type-post","status-publish","format-standard","hentry","category-advanced-topics"],"_links":{"self":[{"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/posts\/22924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/comments?post=22924"}],"version-history":[{"count":10,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/posts\/22924\/revisions"}],"predecessor-version":[{"id":31913,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/posts\/22924\/revisions\/31913"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/media?parent=22924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/categories?post=22924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-0\/wp-json\/wp\/v2\/tags?post=22924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}