Sample program 4: Assign and use global variables in functions
Updated at:
Use global variables in functions to maintain data throughout a conversation flow. This process involves defining the variable, assigning it a value, and referencing it from any node in the conversation.
This example shows how to modify a global variable within a function. You can then reference the variable in later nodes. The `eventObj.global["transedOrderId"]` variable is a global variable created in the conversation flow. When the function starts, it retrieves all slot nodes from the graph using `slots=eventObj.slotSummary`. The function then checks if `Select Order Intent.order_id` has a value. If a value exists, the function assigns it to the global variable. You can then reference this variable in any node that requires it.
module.exports.handler = function(event, context, callback) {
var eventResult = "";
try {
var eventObj = JSON.parse(event.toString());
// add your code here
/**
* code block
**/
//console.info(null, eventObj.toString());
var slots = eventObj.slotSummary;
var slotValue = slots["Select Order Intent.order_id"]||'';
console.info(null, "slotValue:" + slotValue);
if (slotValue && slotValue !==''){
eventObj.global["transedOrderId"] = slotValue; // Assign a value to the global variable here.
} else {
eventObj.global["transedOrderId"] = eventObj.environment["orderId"];
}
console.log(eventObj.global["transedOrderId"]);
eventResult = JSON.stringify(eventObj);
callback(null, eventResult);
} catch (e) {
console.error(null, e);
callback(null, e);
}
};
Python
# -*- coding: utf-8 -*-
import logging
import json
def handler(event, context):
logger = logging.getLogger()
logger.info(event)
eventObj = json.loads(event)
slots = eventObj["slotSummary"]
logger.info(slots)
slotValue = slots["Select Order Intent.orderId"]
if slotValue is not None:
eventObj["global"]["transedOrderId"] = slotValue;
else:
eventObj["global"]["transedOrderId"] = eventObj["environment"]["orderId"];
return eventObj
JAVA
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;
/**
* Created by weili on 2018/8/2.
*
* @author weili
* @date 2018/08/02
*/
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 slots = eventObj.getJSONObject("slotSummary");
String slotValue = slots.getString("Select Order Intent.order_id");
if (null != slotValue && !"".equals(slotValue)) {
eventObj.getJSONObject("global").get("transedOrderId") = slotValue;
} else {
eventObj.getJSONObject("global").get("transedOrderId") = eventObj.getJSONObject("environment").getString("orderId");
}
return eventObj;
}
}
Reference the global variable

Is this page helpful?