DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Build a Simple Chat Server With gRPC in .Net Core
  • Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
  • The SPACE Framework for Developer Productivity
  • Front-End: Cache Strategies You Should Know
  1. DZone
  2. Coding
  3. Languages
  4. Adding Class Scripts to Your Chatbot in Teneo Studio

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.

Gareth Walters user avatar by
Gareth Walters
·
Jun. 17, 20 · Tutorial
Like (2)
Save
Tweet
Share
3.59K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Groovy (programming language) Chatbot

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

Opinions expressed by DZone contributors are their own.

Trending

  • Build a Simple Chat Server With gRPC in .Net Core
  • Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
  • The SPACE Framework for Developer Productivity
  • Front-End: Cache Strategies You Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: