Teneo Developers

Add files to your solution

Sometimes it may come in handy to make use of a pre-existing script or a data set in your solution. In Teneo it is possible to upload a variety of different file formats, from Groovy scripts to compiled .jar files, as well as simple text based formats such as .txt or .csv.

On this page we will:

  • Upload a .csv file containing the opening hours for the Longberry Baristas coffee shops.
  • Create a global variable in which we can store the opening hours.
  • Read the contents of the .csv file using a Groovy script and store the results in a global variable.

Upload a .csv file

The .csv file with the opening hours for Longberry Baristas coffee shops can be downloaded here. It consists of two columns, one for the weekdays and one for the respective opening hours.

To add the file your solution, proceed as follows:

  1. Go to your Solution dashboard.
  2. Click on the Add icon on the File Resources tile.
  3. Drag and drop the .csv file you downloaded (or click to browse and select it).
  4. Set the 'Published Location' for this file to /script_lib. This ensures the file can be accessed using a Groovy script later.
  5. Hit 'Save'.

Access the file with Groovy

To access the content of this file, we will process the file content and store it in a global variable using a Groovy script. This script will be executed at the beginning of each new dialog. Before we can do that, however, we need to add the global variable in which we want to store the opening hours.

Create a global variable to store the data

Let's start with creating the global variable that will store the opening hours:

  1. From your Solution dashboard, click on the Add button next to Variables in the Globals tile.
  2. Give the new variable the name businessHours.
  3. Set the default value to an empty map [:].
  4. Hit 'Save'.

Read the file and store the data in the global variable

Now we will add the Groovy code to the global script 'Begin Dialog'. Scripts in 'Begin Dialog' are executed each time a conversation starts. Our script will read the .csv file, turn the data into a map and store it in the global variable we created earlier.

  1. From the Solution dashboard, click on the Add button next to Scripts in the Globals tile.
  2. Create a new Begin Dialog script and name it Load business hours.
  3. Paste the following code snippet below the already existing code:

groovy

1// get the csv file
2def file = groovy.io.FileType.class.getClassLoader().getResource('business_hours.csv')
3
4// parse the csv
5file.eachLine {
6  def (k,v) = it.split(",")
7  businessHours[k] = v
8}
9
  1. Hit 'Save'.