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 will:

  • Upload a .csv file containing the opening hours for 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. Click on 'Solution' in the upper left corner.
  2. Select 'Resources' on the left.
  3. Select 'File' at the top.
  4. Use 'Add' on the upper right to add the file you downloaded. Alternatively, you may 'drag and drop' it.
  5. Set the Path for this file from / to /script_lib. This ensures the file can be accessed using a Groovy script later.
  6. Hit 'Save'. In the confirmation popup that appears, click 'Continue', then close the popup.

Access the file with Groovy

In order 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 in which will store the opening hours:

  1. Select 'Globals' on the left.
  2. Select 'Variables' at the top.
  3. Add a new variable using the 'Add' button on the upper right and give it the name businessHours.
  4. Set the default value to an empty map [:].
  5. 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. Change to the 'Script' tab at the top while still in 'Globals'.
  2. Select 'Begin Dialog' on the left hand side in the 'Sessions' column.
  3. Click 'Edit' at the top to the very right.
  4. You can now edit the 'Begin Dialog' and paste the following code snippet below the already existing code:

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

The existing code in the 'Begin Dialog' script came with the Teneo Dialogue Resource template solution that you used when you created the solution.

Try it out!

To find out whether the opening hours from the .csv file have been correctly loaded into the global variable, we can look at the value of this variable after having started a new dialog:

  1. Reload Engine (if not yet done).
  2. Enter any input to Try out (this will start the 'begin dialog' script and populate the global variable businessHours).
  3. Open the 'Response Info' panel on the right.
  4. Look for the global variable businessHours and check whether it contains the opening hours.

Was this page helpful?