Commonly used global contexts

In a conversational interface it is essential to control the type and format of data that is transmitted in a message exchange under certain circumstances. In Teneo Studio, thanks to the Context Restrictions you can control the output contents according to the publish environment, channel or other factors. You can create Global Contexts ( Solution tab > Globals > Contexts ) to define Context Restrictions which can be used in any flows in your solution. Creating Global Contexts requires knowledge of Groovy, but don’t worry if you are not a professional programmer, as Teneo Conversational Modules (or called Teneo Dialogue Resources) provide you template solutions with 7 Predefined Global Contexts which are widely used in different projects:

Class Confidence High - Restricts class triggers to only fire when the confidence score is 0.80 or higher.
Class Confidence Medium - Restricts class triggers to only fire when the confidence score is 0.60 or higher.
Disable Match - Assign this context to disable a trigger or a transition from matching.
Follow up of Flow - Checks if the previous input triggered the same flow as the trigger that is currently evaluated. This allows you to determine if an input is a follow up of the same flow.
Follow up of Folder - Checks if the previous input triggered a flow in the same folder as the current one. This allows you to determine if an input is a follow up of a flow in the same folder.
Input Sentences - The Context if fulfilled if the number of sentences in the user input matches selected state.
Input Words - The Context if fulfilled if the number of words in the user input matches selected state(s).

Besides these predefined ones, in this article we are going to provide you with other three useful Global Contexts.

Not allowed in production

This Context restriction is usually attached to a trigger or a transition leading to contents in building which should not be included in the Production environment. Thanks to this Context restriction, you can safely publish your solution from Dev to Prod with these contents in building without concerning that the end users might see something not ready to be released.

This is the code you need to fill in the Script field:

// Use to limit flows or transitions to lower environments
if (engineAccess.getProperty('servletContextParameters.release_environment').toLowerCase() == "production") return "prod" // Check if environment is PROD
else return "other" // If environment is not PROD, then set to "other"

If you have already collected the information of release environment in the Begin Dialog script and saved it in a global variable sReleaseEnvironment following this post: Get Release Environment, you can simply change the code above in this way:

// Use to limit flows or transitions to lower environments
if (sReleaseEnvironment.toLowerCase() == "production") return "prod" // Check if environment is PROD
else return "other" // If environment is not PROD, then set to "other"

Then you need one Expected State which can be named as Not allowed on Prod , with the Evaluation Script as “other”. See a screenshot of this scripted context:

Release in Channel

It is common to design the functionalities of your bot depending on the channel, for example Alexa, Microsoft Teams, Facebook Messenger, etc. As some functionalities are designed to be only available in some of them, you can use this Context restriction to control it.

This is the code you need to fill in the Script field:

// Use to limit flows or transitions to certain channels
if (engineEnvironment.getParameter("channel")) {
    return engineEnvironment.getParameter("channel").toLowerCase()
} else {
    return null
}

Then you need several Expected States corresponding to the channels your chat bot connects to, with the Evaluation Script as the value of the Engine Input Parameter ‘channel’. Open the Channels section in Teneo Documentation and choose the channel to check the value of this input parameter. Here you have an example of the full context:

Day or night

This Context restriction is useful when you need to control certain content to be available only during a certain period. This is the code you need to fill in the Script field:

// get the hour of the day in 24h format
def hour = java.time.LocalDateTime.now().getHour()
// is it day or night
if (hour >= 7 && hour =< 19) {
    return "day"
} else {
    return "night"
}

Then add corresponding Expected States:


You can use this context to control the day of week or the day and month as well by changing the .getHour() method to .getDayOfWeek(), .getDayOfMonth() and .getMonth(), and change the Expected States according to your needs.

This article provides you with three examples of useful Global Contexts . We encourage you to share your use case of contexts in the forum to help other Teneo developers. We hope you found this article useful, and feel free to ask here any questions you might have on the Context restriction.

3 Likes

Finest ways to use Context!

3 Likes