Example 3: Branch a conversation flow based on function return values

更新时间:
复制 MD 格式

This example continues the order query scenario from Example 1. It shows how to create branches in a conversation flow to apply different logic and replies based on the order number type. For example, for a Class A order, the flow queries the Class A order API before replying, while for other order types, it replies with static content.

Example code

In Dialog Studio, you can enable the branch (switch) feature in a function node to create branches in the conversation flow. The branching logic executes based on the value of the built-in system variable routeVariable.

In the "Branch Condition" function node, you can enable the branch setting and connect two downstream nodes. If the order number is for a Class A order (starts with "A_"), the flow provides a specific response for Class A orders. For other order types, it provides a different response. The branch condition depends on the value of routeVariable. If routeVariable is 1, the flow follows the path marked "A". If routeVariable is 0, it follows the path marked "B". The following example code shows how to set the value of routeVariable. The code checks the orderId parameter of the order query intent. If the order number starts with "A_", the code sets routeVariable to 1. Otherwise, it sets routeVariable to 0. This method uses custom code to control the branch logic.

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",
      "outputForResponse": "Object",
      "outputForFunction": "String",
      "routeVariable": "String"
    }
    **/
    var arr = "";
    var eventResult = "";
    try {
        var eventObj = JSON.parse(event);
        // add your code here
        var slots = eventObj["slotSummary"];
        var orderId = slots["query_order.orderId"];
        if (orderId.startsWith("A_")) {
            eventObj.routeVariable = '1';
        } else {
            eventObj.routeVariable = '0';
        }
        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)

  slots = eventObj["slotSummary"]
  logger.info(slots)
  orderId = slots[u"query_order.orderId"]

  if orderId.startswith("A_"):
    eventObj["routeVariable"] = "1"
  else:
    eventObj["routeVariable"] = "0"

  return eventObj

Java

package com.aliyun.openservices.tcp.example.handler;

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",
         *  "outputForResponse": "Object",
         *  "outputForFunction": "String",
         *  "routeVariable": "String"
         */

        JSONObject slots = eventObj.getJSONObject("slotSummary");
        String orderId = slots.getString("query_order.orderId");

        if (orderId.startsWith("A_")) {
            eventObj.put("routeVariable", "1");
        } else {
            eventObj.put("routeVariable", "0");
        }

        return eventObj;
    }
}