{{announcement.body}}
{{announcement.title}}

Adding Class Scripts to Your Chatbot in Teneo Studio

DZone 's Guide to

Adding Class Scripts to Your Chatbot in Teneo Studio

In this article, we will show you two ways to add a Groovy class to your chatbot solution.

· AI Zone ·
Free Resource

Scripts in Teneo are written in Groovy or Java code. Apache Groovy is similar to Java but with an easier to learn syntax. More details on Groovy can be found here: http://groovy-lang.org. An overview of the differences between Groovy and Java can be found here: Differences with Java.

Sometimes you may want to add a class to your solution, which you can then call in your integrations or scripts.

In this article, we will show you two ways to add a Groovy class to your solution:

  • Approach 1: upload the class as a .groovy file
  • Approach 2: add the class to the ‘Solution loaded’ script

Both ways have the same outcome: a class that you can use in the various scripts and integrations in your solution.

But first, let’s define a basic Groovy class that we can use as an example.

Example Groovy Class

We will use the following class as an example to add to Teneo:

Java
 




x


 
1
public class TeneoTextUtils {    /**    * Takes a String input, and returns it with the the first letter of     * each word capitalized and all other letters lower case    */    public static String capitalizeFirstLetters(String input) {        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')    } 
2
}



The class is called ‘TeneoTextUtils’ and its method ‘capitalizeFirstLetters’ will capitalize each word in a String. For example: TeneoTextUtils.capitalizeFirstLetters('new york') returns ‘New York’.

Approach 1: Add a Groovy Class via a .groovy File

One way of adding the class to the solution is to save it as a text file with the name of the class and the extension .groovy. This file is then uploaded in the Resource file manager in order to make it available in the solution.

First, save the following code in a text file called TeneoTextUtils.groovy

Java
 




xxxxxxxxxx
1


 
1
public class TeneoTextUtils {    /**    * Takes a String input, and returns it with the the first letter of     * each word capitalized and all other letters lower case    */    public static String capitalizeFirstLetters(String input) {        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')    } 
2
}



Next, add the file to your solution and set the path to /script_lib (If you need guidance on how to add the file to the solution, please check Add files to your solution).

Now the class ‘TeneoTextUtils’ can be used in your scripts and integrations.

Approach 2: Add a Class to the Global ‘Solution Loaded’ Script

The other way of adding a class is by adding it to the global script Solution loaded. This script is compiled and loaded each time a solution is loaded into memory, which happens when you publish your solution for example.

To add it, proceed as follows:

  1. Click on the ‘Solutions’ tab in the top left
  2. Select ‘Globals’ on the left.
  3. Select the ‘Scripts’ tab.
  4. Select ‘Solution Loaded’.
  5. Click on the ‘Edit’ button in the top right
  6. Paste the following groovy class into the editing window:public class TeneoTextUtils { /** * Takes a String input, and returns
     it with the the first letter of * each word capitalized and all other
     letters lower case */ public static String capitalizeFirstLetters(String
     input) { input.toLowerCase().tokenize().collect
     { it.capitalize() }.join(' ') } }
  7. Hit ‘Save’, then close the editing window.

The class ‘TeneoTextUtils’ can now be used in your scripts and integrations.

Topics:
ai chatbot, artificial intelligence, chatbot, chatbot development, conversational ai, conversational ai chatbot, groovy, tutorial

Published at DZone with permission of Gareth Walters . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}