Sample writing program 2 passes the parameters returned by the function into the dialog

Updated at:

Reference the returned result of a function in the dialog flow. You can use the \${ parameter name} method to reference the specific parameter description:

Parameter Name

Parameter type

Parameter description

Writable

environment

json

See the following environment description

Read-only

lastOutputForFunction

String

Output of the previous function

Read-only

slotSummary

json

Filling value of each slot

Read-only

global

String

Global variables, defined when designing the dialogue flow, can be directly referenced or assigned during function execution.

Yes

outputForResponse

json

The function returns the result

Yes

outputForFunction

String

The output value of the current function, which can be referenced by other functions through lastOutputForFunction

Yes

routeVariable

String

The branch logic routing variable of the function node. This parameter is required when the branch logic is determined.

Yes

Description of the environment parameter

Parameters

Type

Description

SENDER_ID

STRING

Sender ID.

SENDER_NICK

STRING

Sender alias.

IS_ADMIN

STRING

Specifies whether it is an administrator of the enterprise.

SENDER_STAFF_ID

STRING

If the sender is an employee of the enterprise corresponding to the enterprise robot, the job ID of the sender in the enterprise is returned.

MEMBER_TYPE

STRING

The type of a member.

TOKEN

STRING

The token of the session.

FROM

STRING

Session sources: im_h5,group_chat,single_chat,console_im_test

FROM_SITE

STRING

FROM=group_chat,FROM_SITE is the group number

Parameter reference example: Take global variables as an example. This example illustrates how to assign values to global variables and reference them in subsequent nodes, where the variables referenced in eventObj.global ["transedOrderId"] are global variables created in the dialog. The global variable creation method, "Example writer 4", at the beginning of the function, obtains all slot nodes in the graph through slots=eventObj.slotSummary. In the next step, if the "select order intent. order_id" has a value, this value is assigned to a group of global variables, which can be referenced by nodes that need to be used later. Sample code:

NodeJS

module.exports.handler = function(event, context, callback) { 
  var eventResult = "";
  try {

      var eventObj = JSON.parse(event.toString());

      /**
      * code block
      **/
      var slots = eventObj.slotSummary;
      var slotValue=slots ["Select Order Intent. order_id"]| |'';
      console.info(null, "slotValue:" + slotValue);
      if (slotValue && slotValue !==''){
        eventObj.global["transedOrderId"] = slotValue;
      } 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;
    }
}