Make use of Javascript (JS) functions when you want to perform a set of actions with the Freddy Self-service bot when the actions aren't available by default. Let's take the example of a simple JS function:



print: function (a, b, c) {        
console.log("print called: a:" + a + " b: " + b + " c: " +c);
}

Configuration in the widget script


You'll first have to configure the JS code into the widget script. To get the bot widget script

  • Login to you Freddy Self-service account as an Admin or Owner

  • Navigate to the bot of your choice > Generate script on the sidebar > Generate Script button > you will see the code on the right.


Copy-paste this code onto a script editor so that you can include your script inside this code. You are allowed to write any function as a logic into this script. 


  • Include your custom JS function after the getClientParams function is defined. In our case, let's define the print function here.



Once you define this function in the bot script, you can call it from anywhere in the bot flow.


Configuration inside Admin Portal

  • Go back to your bot account and navigate to API Libary on the sidebar (of the bot in whose widget script you entered the function).

  • Provide a (1) Name for the JS Function. This is just for your reference. 

  • In the (2) URL / JS function name field, enter the function's name. This should be the same name that you defined in the bot widget script. In our example, it is "print". Remember that the function name is case sensitive.

  • Set the method type to (3) JS Function and the (4) Payload type as JSON.

  • Enter the argument values that you need to pass in the (4) Payload content as JSON single line values. These argument values can also be dynamic. Use the + button to include dynamic values.
     
  • In the (6) Add the required response parameters field, you can pass your success and failure response parameters. The bot will pass the input to the script after making the function call. When the function executes, it will expect a response parameter. These response parameters can be passed from the script to the bot based on the result of the action.


The response value from here can be further called in other parts of the bot flow. For example, if you're setting up the customer input as a message or a question, you can insert content from APIs, or in this case, insert the result of the JS function (success or failure response parameter).


Configuring the dialogs


To call the function, you'll need to set up an action in the dialog for the bot to trigger the function.


  • Open the dialog where you want to run the function > open the Actions tab.
  • Choose Client-side actions from the (1) Action type dropdown.

  • Choose Javascript dynamic content request from the (2) Client-side action type dropdown.

  • Pick the Javascript function that you defined earlier in the (3) Select an API dropdown.

  • Choose an appropriate (4) Processing time and Time unit. The action will be delayed by the amount of time you enter here.



Keep in mind that if you're using a client-side action to call the JS function, you'll have to get the user input as a text field. This is to denote that the function is returning a response. 


Some use cases

  • If you have a phone number that your customer needs to call, for example, a toll-free phone number, you can have a button that says "call us" which your customer can tap or click. This can trigger the JS function, which in turn will populate the default phone dialler with your toll-free number. In this case, for Android devices, the WebView should have permission to access the dialler. The phone dialler permission should be allowed at the manifest file level. 

  • If you've deployed the bot on your mobile app, and you want to take your customer to a specific page on your app, you can do so by using a deep link in the app.

  • If you don't have an API, but you want to communicate with your app, which has a connection with the server, even after the bot flow initializes, you can make use of custom JS functions to interact with the app and fetch information.

  • Any standard functions, like math functions, that can be written as a JS function can be carried out with the custom JS functions.


Please write to freshbots-support@freshworks.com if you have any more questions. We'll be happy to help you.