Engine scripting API
Many of your scripts will depend on internal Teneo data. You may for example want to use the user's input in a script, or find out what the name is of the flow that is currently processed or you need to manipulate and answer text using a script. For that you can use the Engine scripting API. This API gives you access to detail provided in the requests as well as the current state of the dialog.
The two classes that you will use mostly in your scripts are:
- _engineAccess (alias _) - For accessing details about the conversation and bot's internal state.
- engineEnvironment - For access to request/input parameters
The Engine scripting API Reference provides details on all classes and methods as well as the scopes in which they can be used. Below you will find some examples of Engine scripting API calls that are commonly used in Teneo solutions.
Common scripts
Retrieve an input parameter
groovy
1engineEnvironment.getParameter('userTimeZone')
2
Usually called in the either global Begin dialog or the global Pre-pocessing script. A more detailed guide can be found here: How to store input parameters.
Set the lifespan of a global variable
groovy
1_.setSessVarLifespan('coffeeTypeInFocus',2)
2
Get the user's input
groovy
1_.getUserInputText()
2
or, slightly shorter:
groovy
1_.userInputText
2
Get the output text
groovy
1_.getOutputText()
2
Get the output url for an output
groovy
1_.getOutputURL()
2
Manipulate an output text
groovy
1if (channel == 'slack' && _.getOutputURL()) {
2 _.setOutputText(_.getOutputText() + '\nMore details: ' + _.getOutputURL())
3}
4
Note: outputs can only be manipulated in the global Post-processing script.
Clear all output parameters
groovy
1if (something == true) {
2 _.outputParameters.clear()
3}
4
Note: outputs can only be manipulated in the global Post-processing script.
Add an output parameter
groovy
1_.putOutputParameter('openMic', 'true')
2
Note: the value of outputs parameters should always be Strings. Outputs can only be manipulated in the global Post-processing script.
Get the length of the dialog
groovy
1_.getDialogHistoryLength()
2
Clear the flow stack
You would typically add this script to the On-drop script of the flow that should clear the stack.
groovy
1if( _.activeFlows.size()>1) {
2 for ( item in _.activeFlows[0..-2]){
3 for (flow in item) {
4 flow.abort()
5 }
6 }
7}
8
Get the contents of a file stored in your solution
You can get the contents of a file stored in the /script_lib folder in the Resource file manager like this:
groovy
1def fileContents = groovy.io.FileType.class.getClassLoader().getResource(resourceFilename).text
2
End session
You can programmatically end or kill an engine session like this:
groovy
1engineEnvironment.setSessionTimeout(0)
2
Change session timeout
The bot session timeout period can be set in a script.
- From the main solution window, click on the 'Solution' tab in the top left corner.
- In the purple section on the left, choose 'Globals' and select the 'Scripts' tab at the top.
- Open the section, Begin Dialog and add a line as below where 'n' is the amount of seconds you wish for the timeout:
groovy
1engineEnvironment.setSessionTimeout(n)
2
- Don´t forget to hit save!