{"id":23506,"date":"2020-04-20T10:53:40","date_gmt":"2020-04-20T09:53:40","guid":{"rendered":"https:\/\/developer.kore.ai\/v9-1\/?p=23506"},"modified":"2021-08-17T07:49:26","modified_gmt":"2021-08-17T06:49:26","slug":"how-to-use-bot-functions","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v9-1\/docs\/bots\/how-tos\/how-to-use-bot-functions\/","title":{"rendered":"How to use Bot Functions"},"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>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.<\/p>\n<p>For details on what Bot Functions are and how they are implemented in the Kore.ai Bots platform, <a href=\"\/docs\/bots\/bot-settings\/bot-functions\/reusing-bot-functions-custom-script-file\/#Updating_Custom_Script_File\" target=\"_blank\" rel=\"noopener noreferrer\">refer here<\/a>.<\/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=\"Problem_Statement\">Problem Statement<\/span><\/h2>\n<p>In our <a href=\"\/docs\/bots\/how-tos\/creating-a-banking-bot\/\">Banking Bot<\/a>, we have a reporting task:<\/p>\n<ol>\n<li>Monthtly Statement which gives the monthly balance amounts for a given Account number.<\/li>\n<li>Credit Card Statement gives an overview of the expenses for a given Credit Card.<\/li>\n<li>Both the reports are presented as pie chart and the input in both the cases is an array with month, amount values.<\/li>\n<\/ol>\n<p>In this document, we will see how Bot Function can be used to avoid duplication of coding effort and ensure uniform presentation.<\/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=\"Pre-requisites\">Pre-requisites<\/span><\/h2>\n<ul>\n<li>Bot building knowledge<\/li>\n<li>A working bot. We will be using the Banking Bot <a href=\"\/docs\/bots\/how-tos\/creating-a-banking-bot\/\" target=\"_blank\" rel=\"noopener noreferrer\">from here<\/a>.<\/li>\n<li>Following is the Script file with two functions using the message templates to display the data (see here for more on <a href=\"\/docs\/bots\/sdks\/message-templates\/\" target=\"_blank\" rel=\"noopener noreferrer\">message templates<\/a>):\n<ul>\n<li><em>tableTemplate &#8211;\u00a0<\/em>takes an two-dimensional array and displays the content in a tabular format<\/li>\n<li><em>pieTemplate <\/em><em>&#8211;\u00a0<\/em>takes an two-dimensional array and displays the content in a pie format<\/li>\n<\/ul>\n<p>Copy the following content and save it as a <em>.js<\/em> file (say, <em>functionEX.js<\/em>)<\/p>\n<pre>function tableTemplate(data){\r\n var message = {\r\n   \"type\": \"template\",\r\n   \"payload\": {\r\n   \"template_type\": \"mini_table\",\r\n   \"layout\": \"vertical\",\r\n   \"text\":\"Monthly Balance Statement\",\r\n   \"elements\": []\r\n  }\r\n };\r\n for (i=0; i &lt; data.length; i++) {\r\n   var j=0;\r\n   var element = {\r\n     \"primary\":[[data[i][j]],[data[i][j+1],\"right\"]],\r\n     \"additional\":[[data[i][j+2],data[i][j+3]],[data[i][j+4],data[i][j+5]]]\r\n   };\r\n message.payload.elements.push(element);\r\n }\r\n return JSON.stringify(message);\r\n};\r\n\r\nfunction pieTemplate(data){\r\n  var message = {\r\n   \"type\": \"template\",\r\n   \"payload\": {\r\n   \"template_type\": \"piechart\",\r\n   \"pie_type\": \"regular\",\r\n   \"text\": \"Monthly Expense Report\",\r\n   \"elements\": []\r\n   }\r\n  };\r\n for (i=0; i &lt; data.length; i++) {\r\n  var element = {\r\n   \"title\": data[i][0],\r\n   \"value\": data[i][1]\r\n   };\r\n  message.payload.elements.push(element);\r\n }\r\n return JSON.stringify(message);\r\n};<\/pre>\n<\/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<h2><span class=\"ez-toc-section\" id=\"Implementation\">Implementation<\/span><\/h2>\n<ol>\n<li>Open the Banking Bot<\/li>\n<li>Select the\u00a0<strong>Build<\/strong> tab from the top menu<\/li>\n<li>From <strong>Configurations <\/strong>select the <strong>Bot Functions<\/strong> section.<\/li>\n<li>Click\u00a0<strong>Import<\/strong> (or Import New) to open the Import Custom Script window<\/li>\n<li>Drag and drop or Browse the script file that you saved from the Pre-requisites section and click\u00a0<strong>Import<\/strong>.<\/li>\n<li>Once the success message is displayed click\u00a0<strong>Done<\/strong>.<\/li>\n<li>Your Script file is ready to use.<br \/>\n<a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812074757\/ht_functions1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-29076\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812074757\/ht_functions1.png\" alt=\"\" width=\"1366\" height=\"768\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812074757\/ht_functions1.png 1366w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812074757\/ht_functions1-300x169.png 300w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812074757\/ht_functions1-1024x576.png 1024w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/li>\n<li>Open the\u00a0<strong>Dialog Tasks<\/strong> page and create a New Dialog Task called <em>&#8220;Monthly Statement&#8221;.<\/em>\n<ol>\n<li>Add entities to capture the\u00a0<strong>Account Number<\/strong> and\u00a0<strong>Account Type<\/strong>.<\/li>\n<li>Ideally, there would be a service call to fetch the monthly balance details. Here we will use static values for demonstration purposes.<\/li>\n<li>Add a\u00a0<strong>Script<\/strong> node using the\u00a0<strong>Bot Action<\/strong> and enter the following &#8211; we are declaring an array carrying the balances for the months of January, February and March:<br \/>\n<code>context.monthlyBal = [[\"Jan\",100],[\"Feb\",200],[\"Mar\", 300]];<\/code><\/li>\n<li>Add a\u00a0<strong>Message<\/strong> node to send this data to the Bot Function declared earlier and get the report.\n<ul>\n<li>From\u00a0<strong>Bot Responses<\/strong> section, click\u00a0<strong>Manage Responses<\/strong><\/li>\n<li><strong>Add Bot Response<\/strong>, select <em>Web\/Mobile Client\u00a0<\/em>as the\u00a0<strong>Channel<\/strong>, switch to\u00a0<strong>JavaScript<\/strong> tab and enter\u00a0 the following code to display the values in a tabular format:<br \/>\n<code>var info = context.monthlyBal;<br \/>\nprint(tableTemplate(info));<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Add another <strong>Message<\/strong> node to send this data to the Bot Function declared earlier and get the report.\n<ul>\n<li>From\u00a0<strong>Bot Responses<\/strong> section, click\u00a0<strong>Manage Responses<\/strong><\/li>\n<li><strong>Add Bot Response<\/strong>, select <em>Web\/Mobile Client\u00a0<\/em>as the\u00a0<strong>Channel<\/strong>, switch to\u00a0<strong>JavaScript<\/strong> tab and enter\u00a0 the following code to display the values in a pie chart:<br \/>\n<code>var info = context.monthlyBal;<br \/>\nprint(pieTemplate(info));<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n<li>Close the dialog task and return to the <strong>Dialog Tasks<\/strong> page<\/li>\n<li>Create a New Dialog Task called <em>&#8220;Credit Card Statement&#8221;.<\/em>\n<ol>\n<li>Add entities to capture the\u00a0<strong>Account Number<\/strong>.<\/li>\n<li>Ideally, there would be a service call to fetch the monthly expenditure values. Here we will use static values for demonstration purposes.<\/li>\n<li>Add a\u00a0<strong>Script<\/strong> node within\u00a0<strong>Bot Action<\/strong> node and enter the following &#8211; we are declaring an array carrying the expenditure values for all the months:<br \/>\n<code>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]];<\/code><\/li>\n<li>Add a\u00a0<strong>Message<\/strong> node to send this data to the Bot Function declared earlier and get the report.\n<ul>\n<li>From\u00a0<strong>Bot Responses<\/strong> section, click\u00a0<strong>Manage Responses<\/strong><\/li>\n<\/ul>\n<ul>\n<li><strong>Add Bot Response<\/strong>, select <em>Web\/Mobile Client\u00a0<\/em>as the\u00a0<strong>Channel<\/strong>, switch to\u00a0<strong>JavaScript<\/strong> 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.<br \/>\n<code>var info = context.monthlyExpenses;<br \/>\nprint(pieTemplate(info));<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n<li>Close the Dialog Task<\/li>\n<li><strong>Talk to Bot<\/strong> and try both the dialogs. Thus we have used the same function to display the reports in multiple places.<\/li>\n<\/ol>\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 class=\"g-cols wpb_row  type_default valign_top vc_inner \"><div class=\"vc_col-sm-6 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><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075040\/ht_functions2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075040\/ht_functions2.png\" alt=\"\" width=\"1366\" height=\"764\" class=\"alignnone size-full wp-image-29079\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075040\/ht_functions2.png 1366w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075040\/ht_functions2-300x168.png 300w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075040\/ht_functions2-1024x573.png 1024w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/div><\/div><\/div><div class=\"vc_col-sm-6 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><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075157\/ht_functions3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075157\/ht_functions3.png\" alt=\"\" width=\"1366\" height=\"768\" class=\"alignnone size-full wp-image-29082\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075157\/ht_functions3.png 1366w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075157\/ht_functions3-300x169.png 300w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v9-1\/wp-content\/uploads\/sites\/22\/20210812075157\/ht_functions3-1024x576.png 1024w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/div><\/div><\/div><\/div><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":"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...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-23506","post","type-post","status-publish","format-standard","hentry","category-how-tos"],"_links":{"self":[{"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/posts\/23506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/comments?post=23506"}],"version-history":[{"count":5,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/posts\/23506\/revisions"}],"predecessor-version":[{"id":31512,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/posts\/23506\/revisions\/31512"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/media?parent=23506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/categories?post=23506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-1\/wp-json\/wp\/v2\/tags?post=23506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}