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:
- Go to your Solution dashboard.
- Click on the Add icon on the File Resources tile.
- Drag and drop the .csv file you downloaded (or click to browse and select it).
- Set the 'Published Location' for this file to /
script_lib
. This ensures the file can be accessed using a Groovy script later. - 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:
- From your Solution dashboard, click on the Add button next to Variables in the Globals tile.
- Give the new variable the name
businessHours
. - Set the default value to an empty map
[:]
. - 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.
- From the Solution dashboard, click on the Add button next to Scripts in the Globals tile.
- Create a new Begin Dialog script and name it
Load business hours
. - 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
- Hit 'Save'.