1

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 实现中使用 webhook。上面的代码是 dialogflow CX 的测试代理模拟器中的“原始响应”。第 3 步说我的 webhook 状态正常,但没有响应。返回的 JSON 内容是否正确?dialogflow CX 如何解析其响应与 ES 不同?

4

2 回答 2

2

Dialogflow CX webhook 类似于Dialogflow ES webhook,不同之处在于请求响应字段已更改为支持 Dialogflow CX 功能。这就是 Dialogflow CX 解析其响应的方式与 Dialogflow ES 不同的原因。

正如您在附加的屏幕截图中所观察到的,您在对象中仅使用了一个文本字段参数,而不是fulfillment_response.messages[]来表示可以由您的会话代理返回的响应消息。要为您的用例获取所需的 webhook 响应,请确保您遵循 Dialogflow CX 的webhook 响应格式。如果你使用 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 处理后返回的响应内容。

于 2020-10-29T04:03:13.607 回答
0

截至 2021 年 3 月 5 日,我能够在 GCP 云函数中使用以下代码段作为 webhook 实现。

重要的是,我必须从 camelCase 切换到 snake_case,代理才能正确处理 webhook 响应fulfillmentResponse,例如fulfillment_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)

于 2021-03-06T06:53:50.103 回答