全部产品
阿里云办公

示例编写程序6 如何将外部环境变量应用在对话逻辑中

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

场景描述

在小蜜的会话场景中,会话端可以将一些环境信息传递给小蜜,以达到让机器人有更加个性化回复的效果。比如在这个例子当中,我们希望机器人能够识别出当前的聊天对象是谁,并且根据不同的聊天对象给出不同的回复。如果是Peter在和机器人打招呼,机器人就回复说“你好,Peter帅哥”。如果是Mary在和机器人打招呼,机器人就回复“哇,Mary,好漂亮”。如果是其他人的话,比如Jack,机器人就回复“Hi Jack”。

示例代码

在小蜜的会话接口中,可以将环境信息通过ext字段传递进来,在这个例子中,SENDER_NICK就是ext环境变量重的一个字段。在自定义函数节点的代码中,开发者可以通过获取environment对象的某个Key的方式获取自己想要的环境变量

Node.js

  1. module.exports.handler = function(event, context, callback) {
  2. /** event structure definition
  3. {
  4. // read-only variables
  5. "environment": "Object",
  6. "lastOutputForFunction": "String",
  7. "slotSummary": "Object",
  8. // read/write variables
  9. "global": "Object",
  10. "overrideResponse": "Object",
  11. "functionOutput": "String",
  12. "routeVariable": "String"
  13. }
  14. **/
  15. var arr = "";
  16. var eventResult = "";
  17. try {
  18. var eventObj = JSON.parse(event);
  19. // add your code here
  20. var userNick = eventObj.environment["SENDER_NICK"];
  21. if (userNick === 'Peter') {
  22. eventObj.overrideResponse.htmlText = ["你好,Peter帅哥"];
  23. } else if (userNick === 'Mary') {
  24. eventObj.overrideResponse.htmlText = ["哇,Mary,好漂亮"];
  25. } else {
  26. eventObj.overrideResponse.htmlText = ["Hi " + userNick];
  27. }
  28. eventResult = JSON.stringify(eventObj);
  29. callback(null, eventResult);
  30. } catch (e) {
  31. eventResult = JSON.stringify(eventObj);
  32. callback(null, eventResult);
  33. }
  34. };

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. environment = eventObj['environment']
  9. userNick = environment['SENDER_NICK']
  10. if userNick == 'Peter':
  11. responseText = '你好,Peter帅哥'
  12. elif userNick == 'Mary':
  13. responseText = '哇,Mary,好漂亮'
  14. else:
  15. responseText = 'Hi ' + userNick
  16. eventObj['overrideResponse'] = {'htmlText':[responseText]}
  17. return eventObj

Java

  1. package com.aliyun.openservices.tcp.example.handler;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.aliyun.fc.runtime.Context;
  6. import com.aliyun.fc.runtime.PojoRequestHandler;
  7. public class FunctionHandler implements PojoRequestHandler<JSONObject, JSONObject> {
  8. @Override
  9. public JSONObject handleRequest(JSONObject eventObj, Context context) {
  10. /**
  11. * eventObj structure definition
  12. *
  13. * read-only variables
  14. * "environment": "Object",
  15. * "lastOutputForFunction": "String",
  16. * "slotSummary": "Object",
  17. *
  18. * read/write variables
  19. * "global": "Object",
  20. * "overrideResponse": "Object",
  21. * "functionOutput": "String",
  22. * "routeVariable": "String"
  23. */
  24. JSONObject environment = eventObj.getJSONObject("environment");
  25. String userNick = environment.getString("SENDER_NICK");
  26. String responseText;
  27. if (userNick.equals("Peter")) {
  28. responseText = "你好,Peter帅哥";
  29. } else if (userNick.equals("Mary")) {
  30. responseText = "哇,Mary,好漂亮";
  31. } else {
  32. responseText = "Hi " + userNick;
  33. }
  34. JSONArray htmlText = new JSONArray();
  35. htmlText.add(responseText);
  36. JSONObject overrideResponse = new JSONObject();
  37. overrideResponse.put("htmlText", htmlText);
  38. eventObj.put("overrideResponse", overrideResponse);
  39. return eventObj;
  40. }
  41. }