全部产品
阿里云办公

示例编写程序4 如何利用函数对全局继承变量进行赋值和使用

更新时间:2018-09-27 13:52:00

在函数编写的过程中可以通过全局变量来维护在整个对话流程中需要用到的数据,共分为变量定义,变量赋值,变量引用三个过程,在对话的任意节点可对变量进行引用变量定义:

image.png | left | 827x336

在函数中对全局变量进行修改:此示例说明了如何为全局变量赋值并在后续的节点中引用,其中eventObj.global[“transedOrderId”]中引用的变量为在对话中创建的全局变量,函数开始时,通过slots=eventObj.slotSummary,获取图中的所有槽位节点,在下一步是判断,如果“选择订单意图.order_id”有值,则将此值赋值组全局变量,并在后续需要用到的节点可以引用。

  1. module.exports.handler = function(event, context, callback) {
  2. var eventResult = "";
  3. try {
  4. var eventObj = JSON.parse(event.toString());
  5. // add your code here
  6. /**
  7. * code block
  8. **/
  9. //console.info(null, eventObj.toString());
  10. var slots = eventObj.slotSummary;
  11. var slotValue = slots["选择订单意图.order_id"]||'';
  12. console.info(null, "slotValue:" + slotValue);
  13. if (slotValue && slotValue !==''){
  14. eventObj.global["transedOrderId"] = slotValue; //此处对全局变量进行赋值
  15. } else {
  16. eventObj.global["transedOrderId"] = eventObj.environment["orderId"];
  17. }
  18. console.log(eventObj.global["transedOrderId"]);
  19. eventResult = JSON.stringify(eventObj);
  20. callback(null, eventResult);
  21. } catch (e) {
  22. console.error(null, e);
  23. callback(null, e);
  24. }
  25. };

Python

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import json
  4. def handler(event, context):
  5. logger = logging.getLogger()
  6. logger.info(event)
  7. eventObj = json.loads(event)
  8. slots = eventObj["slotSummary"]
  9. logger.info(slots)
  10. slotValue = slots["选择订单意图.orderId"]
  11. if slotValue is not None:
  12. eventObj["global"]["transedOrderId"] = slotValue;
  13. else:
  14. eventObj["global"]["transedOrderId"] = eventObj["environment"]["orderId"];
  15. return eventObj

JAVA

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.aliyun.fc.runtime.Context;
  5. import com.aliyun.fc.runtime.PojoRequestHandler;
  6. /**
  7. * Created by weili on 2018/8/2.
  8. *
  9. * @author weili
  10. * @date 2018/08/02
  11. */
  12. public class FunctionHandler implements PojoRequestHandler<JSONObject, JSONObject> {
  13. @Override
  14. public JSONObject handleRequest(JSONObject eventObj, Context context) {
  15. /**
  16. * eventObj structure definition
  17. *
  18. * read-only variables
  19. * "environment": "Object",
  20. * "lastOutputForFunction": "String",
  21. * "slotSummary": "Object",
  22. *
  23. * read/write variables
  24. * "global": "Object",
  25. * "overrideResponse": "Object",
  26. * "functionOutput": "String",
  27. * "routeVariable": "String"
  28. */
  29. JSONObject slots = eventObj.getJSONObject("slotSummary");
  30. String slotValue = slots.getString("选择订单意图.order_id");
  31. if (null != slotValue && !"".equals(slotValue)) {
  32. eventObj.getJSONObject("global").get("transedOrderId") = slotValue;
  33. } else {
  34. eventObj.getJSONObject("global").get("transedOrderId") = eventObj.getJSONObject("environment").getString("orderId");
  35. }
  36. return eventObj;
  37. }
  38. }

全局变量引用:

image.png | left | 827x569