文档

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

更新时间:

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

示例代码

在会话接口中,可以将环境信息通过ext字段传递进来,在这个例子中,SENDER_NICK就是ext环境变量中的一个字段。在自定义函数节点的代码中,开发者可以通过获取environment对象的某个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 = ["你好,Peter帅哥"];
        } else if (userNick === 'Mary') {
          eventObj.overrideResponse.htmlText = ["哇,Mary,好漂亮"];
        } 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 = '你好,Peter帅哥'
  elif userNick == 'Mary':
    responseText = '哇,Mary,好漂亮'
  else:
    responseText = 'Hi ' + userNick

  eventObj['overrideResponse'] = {'htmlText':[responseText]}

  return eventObj

Java

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 = "你好,Peter帅哥";
        } else if (userNick.equals("Mary")) {
            responseText = "哇,Mary,好漂亮";
        } 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;
    }
}