Example 6: Use external environment variables in dialogue logic
Updated at:
In a chat session, the client can pass an environment context to the system. This allows the robot to provide personalized replies. For example, the robot can identify the current user and respond differently to each person. If Peter starts a chat, the robot replies, "Hello, handsome Peter." If Mary starts a chat, the robot replies, "Wow, Mary, you're so pretty." For any other user, such as Jack, the robot replies, "Hi Jack."
Example code
You can pass the environment context through the ext field in the session API. In this example, SENDER_NICK is a field in the ext environment variables. In the code for the user-defined function node, you can retrieve the required environment variable from the environment object using its key.
Node.js
module.exports.handler = function(event, context, callback) {
/** event structure definition
{
// read-only variables
"environment": "Object",
"lastOutputForFunction": "String",
"slotSummary": "Object",
// read/write variables
"global": "Object",
"overrideResponse": "Object",
"functionOutput": "String",
"routeVariable": "String"
}
**/
var arr = "";
var eventResult = "";
try {
var eventObj = JSON.parse(event);
// add your code here
var userNick = eventObj.environment["SENDER_NICK"];
if (userNick === 'Peter') {
eventObj.overrideResponse.htmlText = ["Hello, handsome Peter."];
} else if (userNick === 'Mary') {
eventObj.overrideResponse.htmlText = ["Wow, Mary, you're so pretty."];
} else {
eventObj.overrideResponse.htmlText = ["Hi " + userNick];
}
eventResult = JSON.stringify(eventObj);
callback(null, eventResult);
} catch (e) {
eventResult = JSON.stringify(eventObj);
callback(null, eventResult);
}
};Python
# -*- coding: utf-8 -*-
import logging
import json
def handler(event, context):
logger = logging.getLogger()
logger.info(event)
eventObj = json.loads(event)
environment = eventObj['environment']
userNick = environment['SENDER_NICK']
if userNick == 'Peter':
responseText = 'Hello, handsome Peter.'
elif userNick == 'Mary':
responseText = 'Wow, Mary, you\'re so pretty.'
else:
responseText = 'Hi ' + userNick
eventObj['overrideResponse'] = {'htmlText':[responseText]}
return eventObjJava
package com.aliyun.openservices.tcp.example.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.PojoRequestHandler;
public class FunctionHandler implements PojoRequestHandler<JSONObject, JSONObject> {
@Override
public JSONObject handleRequest(JSONObject eventObj, Context context) {
/**
* eventObj structure definition
*
* read-only variables
* "environment": "Object",
* "lastOutputForFunction": "String",
* "slotSummary": "Object",
*
* read/write variables
* "global": "Object",
* "overrideResponse": "Object",
* "functionOutput": "String",
* "routeVariable": "String"
*/
JSONObject environment = eventObj.getJSONObject("environment");
String userNick = environment.getString("SENDER_NICK");
String responseText;
if (userNick.equals("Peter")) {
responseText = "Hello, handsome Peter.";
} else if (userNick.equals("Mary")) {
responseText = "Wow, Mary, you're so pretty.";
} else {
responseText = "Hi " + userNick;
}
JSONArray htmlText = new JSONArray();
htmlText.add(responseText);
JSONObject overrideResponse = new JSONObject();
overrideResponse.put("htmlText", htmlText);
eventObj.put("overrideResponse", overrideResponse);
return eventObj;
}
}Is this page helpful?