{"id":18057,"date":"2018-07-23T10:01:47","date_gmt":"2018-07-23T10:01:47","guid":{"rendered":"https:\/\/developer.kore.ai\/v10-1\/?post_type=docs&#038;p=15099"},"modified":"2022-10-03T09:23:50","modified_gmt":"2022-10-03T08:23:50","slug":"reusing-bot-functions-custom-script-file","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-settings\/bot-functions\/reusing-bot-functions-custom-script-file\/","title":{"rendered":"Bot Functions"},"content":{"rendered":"<p><span style=\"font-weight: 400\">Bot Functions is a feature that uses a custom Script file that enables you to collate and manage reusable utility JavaScript functions that can be used by different tasks, nodes, and other elements. Once you upload a custom script file to the assistant, it reduces the redundant development efforts by making all the functions in the file accessible anywhere in the assistant.<\/span><\/p>\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>\n<h1><span class=\"ez-toc-section\" id=\"Using_the_Custom_Script_File\"><span style=\"font-weight: 400\">Using the Custom Script File<\/span><\/span><\/h1>\n<p><span style=\"font-weight: 400\">Let\u2019s consider one use case from a Travel Planning Assistant, to understand how you can leverage the custom script file to reduce development overhead considerably. Take a look at the following conversation flow of this assistant, that frequently presents customers with options to make a choice. In the below chat, the assistant first prompts the user to select what they want to book, by clicking Flight or Hotel, and then also prompts them to select a Fare, such as Economy, Premium Economy or First Class.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\"><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091601\/use-case.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-47187\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091601\/use-case.png\" alt=\"\" width=\"370\" height=\"626\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091601\/use-case.png 370w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091601\/use-case-177x300.png 177w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/a><\/span><\/p>\n<p><span style=\"font-weight: 400\">You can observe that both the booking and fare options in the above chat are identical in functionality, but only the choices that they show differ. Instead of hard-coding the functionality of the button template into each node, you can write a function in the custom script file that can be invoked by these nodes by passing options. Below is the sample function script to accomplish the functionality.<\/span><\/p>\n<pre><p><span style=\"font-weight: 400\">function custTemplatel(info){<br \/>var message = {<br \/>\"type\": \"template\",<br \/>\"payload\": {<br \/>\"template_type\": \"button\",<br \/>\"text\": \"Select an option\",<br \/>\"subText\": \"3utton Template<br \/>\"buttons\": []}<br \/>};<br \/>for (i = 0; i &lt; info.length; i++)<br \/>var button = {<br \/>\"type\": \"postback\", <br \/>\"title\": info[i], <br \/>\"payload\": \"payloadl\"<br \/>):<br \/>message.payload.buttons.push(button);<br \/>}<br \/>return JSON.stringify(message);<br \/>}<br \/><\/span><\/p>\r\n<\/pre>\n<p><span style=\"font-weight: 400\">This is how the function can be called from the nodes:<\/span><\/p>\n<pre><p><span style=\"font-weight: 400\">var info =[\"Flight\",\"Hotel\"]; <br \/>print(custTemplatel(info));|<br \/><\/span><\/p><\/pre>\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>\n<h1><span class=\"ez-toc-section\" id=\"Uploading_a_Custom_Script_File\"><span style=\"font-weight: 400\">Uploading a Custom Script File<\/span><\/span><\/h1>\n<p><span style=\"font-weight: 400\">Follow these steps to upload the script file to the assistant:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Select the <\/span><strong>Build<\/strong><span style=\"font-weight: 400\"> tab from the top menu<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">From the left menu, select <\/span><strong>Configurations &gt; Bot Functions<\/strong><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Under <\/span><strong>Custom Script<\/strong><span style=\"font-weight: 400\">, click <\/span><strong>Import<\/strong><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">On the <\/span><em><span style=\"font-weight: 400\">Import Custom Script<\/span><\/em><span style=\"font-weight: 400\"> dialog, click <\/span><strong>Browse <\/strong><span style=\"font-weight: 400\">and navigate to the script file in your local machine. Click <\/span><strong>Import<\/strong><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">After the import is successful, click <\/span><strong>Done<\/strong><span style=\"font-weight: 400\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\"><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091556\/import-script.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-47184\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20221003091556\/import-script.gif\" alt=\"\" width=\"719\" height=\"320\" \/><\/a><\/span><\/p>\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>\n<h1><span class=\"ez-toc-section\" id=\"Updating_the_Custom_Script_File\"><span style=\"font-weight: 400\">Updating the Custom Script File<\/span><\/span><\/h1>\n<p><span style=\"font-weight: 400\">You can add or modify functions in the custom script file by downloading the file, making changes, and then importing it back to the assistant. Importing a new custom script file <\/span><span style=\"font-weight: 400\">replaces <\/span><span style=\"font-weight: 400\">the existing file, which cannot be retrieved later. Therefore, we recommend you exercise caution and keep a copy of the downloaded file for backup.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Select the <\/span><strong>Build<\/strong><span style=\"font-weight: 400\"> tab from the top menu.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">From the left menu, select <\/span><strong>Configurations &gt; Bot Functions.<\/strong><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Under the <\/span><strong>Custom Script<\/strong><span style=\"font-weight: 400\">, click <\/span><strong>Download<\/strong><span style=\"font-weight: 400\"> to save a copy of the file on your local computer.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">After you make changes to the file, click <\/span><strong>Import New<\/strong><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Click <\/span><strong>Browse <\/strong><span style=\"font-weight: 400\">to the location of the custom script file on your local computer and then click <\/span><strong>Import<\/strong><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Click <\/span><strong>Done <\/strong><span style=\"font-weight: 400\">when the import is complete.<\/span><\/li>\n<\/ol>\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>\n","protected":false},"excerpt":{"rendered":"<p>Bot Functions is a feature that uses a custom Script file that enables you to collate and manage reusable utility JavaScript functions that can be used by different tasks, nodes, and other elements. Once you upload a custom script file to the assistant, it reduces the redundant development efforts by making all the functions in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-18057","post","type-post","status-publish","format-standard","hentry","category-bot-functions"],"_links":{"self":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/18057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/comments?post=18057"}],"version-history":[{"count":7,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/18057\/revisions"}],"predecessor-version":[{"id":47191,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/18057\/revisions\/47191"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/media?parent=18057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/categories?post=18057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/tags?post=18057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}