Dialogflow CX - 使用 webhook fulfillment 回复用户

Dialogflow CX - using webhook fulfilment to reply user

https://i.stack.imgur.com/oqT5V.png https://i.stack.imgur.com/r3thU.png

{
  "currentPage": {
    "displayName": "Start Page",
    "name": "projects/agent3-293107/locations/global/agents/5ca68c8a-abfc-4b35-a06a-f50cd4b697bd/flows/00000000-0000-0000-0000-000000000000/pages/START_PAGE"
  },
  "diagnosticInfo": {
    "Triggered Transition Names": [
      "e03439ef-fc0c-49f1-943e-2b5d46d68474"
    ],
    "Execution Sequence": [
      {
        "Step 1": {
          "InitialState": {
            "FlowState": {
              "Name": "Default Start Flow",
              "PageState": {
                "Name": "Start Page",
                "Status": "TRANSITION_ROUTING"
              }
            },
            "MatchedIntent": {
              "Type": "NLU",
              "DisplayName": "Default Welcome Intent",
              "Active": true,
              "Id": "00000000-0000-0000-0000-000000000000",
              "Score": 1
            }
          },
          "Type": "INITIAL_STATE"
        }
      },
      {
        "Step 2": {
          "Type": "STATE_MACHINE",
          "StateMachine": {
            "TriggeredIntent": "Default Welcome Intent",
            "FlowState": {
              "PageState": {
                "Name": "Start Page",
                "Status": "TRANSITION_ROUTING"
              },
              "Version": 0,
              "Name": "Default Start Flow"
            },
            "TransitionId": "e03439ef-fc0c-49f1-943e-2b5d46d68474"
          }
        }
      },
      {
        "Step 3": {
          "Type": "FUNCTION_EXECUTION",
          "FunctionExecution": {
            "Responses": [],
            "Webhook": {
              "Status": "OK",
              "Latency": "95 ms"
            }
          }
        }
      },
      {
        "Step 4": {
          "Type": "STATE_MACHINE",
          "StateMachine": {
            "FlowState": {
              "Name": "Default Start Flow",
              "Version": 0,
              "PageState": {
                "Name": "Start Page",
                "Status": "TRANSITION_ROUTING"
              }
            }
          }
        }
      }
    ],
    "Transition Targets Chain": [],
    "Webhook Latencies (ms)": [
      95
    ],
    "Alternative Matched Intents": [
      {
        "Id": "00000000-0000-0000-0000-000000000000",
        "DisplayName": "Default Welcome Intent",
        "Type": "NLU",
        "Score": 1,
        "Active": true
      }
    ]
  },
  "intent": {
    "displayName": "Default Welcome Intent",
    "name": "projects/agent3-293107/locations/global/agents/5ca68c8a-abfc-4b35-a06a-f50cd4b697bd/intents/00000000-0000-0000-0000-000000000000"
  },
  "intentDetectionConfidence": 1,
  "languageCode": "en",
  "match": {
    "confidence": 1,
    "intent": {
      "displayName": "Default Welcome Intent",
      "name": "projects/agent3-293107/locations/global/agents/5ca68c8a-abfc-4b35-a06a-f50cd4b697bd/intents/00000000-0000-0000-0000-000000000000"
    },
    "matchType": "INTENT",
    "modelType": "MODEL_TYPE_STANDARD",
    "resolvedInput": "hi"
  },
  "sentimentAnalysisResult": {
    "magnitude": 0.3,
    "score": 0.3
  },
  "text": "hi",
  "webhookPayloads": [
    {}
  ],
  "webhookStatuses": [
    {}
  ]
}

我正在学习如何在 dialogflow CX fulfilment 中使用 webhook。 上面的代码是 dialogflow CX 的测试代理模拟器内部的“原始响应”。 第 3 步说我的 webhook 状态正常但没有响应。 返回的 JSON 内容是否正确? dialogflow CX 如何以不同于 ES 的方式解析其响应?

Dialogflow CX webhook 类似于 Dialogflow ES webhooks, except that request and response 字段已更改为支持 Dialogflow CX 功能。这就是 Dialogflow CX 以不同于 Dialogflow ES 的方式解析其响应的原因。

如您所附的屏幕截图所示,您在对象中仅使用了 text 字段参数,而不是 fulfillment_response.messages[] 表示可以由您的会话代理返回的响应消息。要为您的用例获得所需的 webhook 响应,请确保您遵循 Dialogflow CX 的 webhook response format。如果你使用 Flask,可以考虑使用 jsonify 方法,如下所示:

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    tag = request.json['fulfillmentInfo']['tag']
    fulfillmentResponse = {
        'fulfillmentResponse': {
            'messages': [{
                'text': {
                    'text': 'Hello World!'
                }
            }]
            },
    'sessionInfo':request.json['sessionInfo']
    }
    return jsonify(fulfillmentResponse)

@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
    tag = request.json['fulfillmentInfo']['tag']
    fulfillmentResponse = {
        'fulfillmentResponse': {
            'messages': [{
                'text': {
                    'text': 'Hi there'
                }
            }]
        },
    'sessionInfo':request.json['sessionInfo']
    }
    return jsonify(fulfillmentResponse)

    app.run(host='0.0.0.0', port=8080)

结果如下:

Webhook 响应JSON结果:

{
   "fulfillmentResponse":{
      "messages":[
         {
            "text":{
               "text":[
                  "Hello World"
               ]
            }
         }
      ]
   },
   "sessionInfo":{
      "session":"projects/project-id/locations/location-id/agents/agent-id/sessions/sessions-id"
   }
}

另外,您在Dialogflow CX中得到的原始响应是webhook处理后返回的响应内容。

截至 2021 年 3 月 5 日,我能够在 GCP Cloud Functions 中使用下面的代码片段作为 webhook fulfillment。

重要的是,我必须从驼峰命名法切换到 snake_case 以便代理正确处理 webhook 响应,例如 fulfillmentResponsefulfillment_response

from flask import jsonify

def process_call(request):


  request_json = request.get_json()
  print(request_json)
  response_dict = {
       "fulfillment_response":{
            "messages":[
              {
                  "text":{
                    "text":[
                        "This is a text example where we explore the importance of webhooks and session variables. The amount due is $session.params.amount_due."
                    ]
                  }
              }
            ]
        } ,
        "session_info":{
          "parameters": {
            "amount_due": 500
          }
        } 
  }

  print(response_dict)

  return jsonify(response_dict)