Modify Output Texts and Parameters in Post-Processing

Manipulating the output texts and parameters is a common-used technique among experienced Teneo developers. It can help you standardize spelling rules in all output nodes, for example change all American spellings to British spellings, or help you attach an additional output text or output parameters to the normal output set up in the flows, for example to request feedback from the user.

According to the processing order, the global Post-Processing script is the last place to modify the output texts and parameters before they get set as response by the Teneo Engine. You can get the raw output text using this expression: _.outputText or this method: _.getOutputText(). The methods starting with an underscore are from a pre-defined class EngineAccess . You can click here for more details about this class, and click here for more information about the Teneo Engine API.

The difference between these two ways to get the output text is that the former is a string object, and you can modify it directly by common string methods like _.outputText = _.outputText.replace("A", "B") or _.outputText = _.outputText + "additional texts". While the latter is a method which returns the current output text, but please note that you cannot modify it like this: _.getOutputText() = _.getOutputText().replace("A", "B"). Be also aware that _.outputText is a non-callable object so this expression is wrong as well: _.outputText().

Here is an example for you. Suppose that you have an output node containing this output text:
example output node

Then you have this code in the Post-Processing script:
_.outputText = _.outputText.replace("color","colour")

When this output node is activated, you will see the output text like this:
output in advanced tryout

If you want to replace the whole output text, you can use this method: _.setOutputText(). For example, in a project we want to translate the original output defined in flows to different languages by calling a machine translation API. In this case we use _.setOutputText() method to replace the original output text in English with the output returned from the machine translation API, such as Google Cloud Translation or Microsoft Translator.

Similar to the output text, you can either use _.outputParameters or _.getOutputParameters() to retrieve all output parameters. The former is a map object and you can modify the value of an output parameter directly like this: _.outputParameters.key = "modified_value". The latter is a method which returns a map containing all output parameters. If you want to get the value of just one output parameter, you can use this method: _.getOutputParamter(key).

If you want to add an output parameter, you can either modify the output parameters object directly: _.outputParameters.new_key = "new_value", or use this method: _.putOutputParameter("new_key", "new_value"). Please be aware that the arguments this method requires are two strings.

Here is an example of how to attach an additional output asking for user feedback with two buttons in the format of Teneo Web Chat.

// define button for positive feedback
Map mPositive = new HashMap()
mPositive.style = "success"
mPositive.title = "Yes"
mPositive.postback = "Yes"
mPositive.parameters = ["command":"userFeedback","feedbackRating":"positive"]

// define button for negative feedback
Map mNegative = new HashMap()
mNegative.style = "danger"
mNegative.title = "No"
mNegative.postback = "No"
mNegative.parameters = ["command":"userFeedback","feedbackRating":"negative"]

// generate json for buttons
Map mFeedback = new HashMap()
mFeedback.type = "quickreply"
mFeedback.quick_replies = [mPositive,mNegative]
def feedbackButtonJson = new groovy.json.JsonBuilder(mFeedback)

// attach the feedback question; '||' is the text bubble separator for Teneo Web Chat
_.outputText = _.outputText + '||' + 'Was my answer helpful?'

// attach the json for buttons
_.putOutputParameter('teneowebclient',feedbackButtonJson.toString());

The code above in the Post-Processing script can add a new text bubble (“Was my answer helpful?”) and two buttons (Yes/No) to the original answer. It will look like this in the Teneo Web Chat frontend:
exmaple dialog in TWC

In this article, you have learned how to modify output texts and output parameters in Teneo’s Post-Processing script. If you have more questions about it or want to share your use case to help other Teneo developers, please don’t hesitate to share your thoughts in Teneo Developers Community!

3 Likes