如何解决Dialogflow CX-使用Webhook实现来回复用户
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","Status": "TRANSITION_ROUTING"
}
}
}
}
}
],"Transition Targets Chain": [],"Webhook Latencies (ms)": [
95
],"Alternative Matched Intents": [
{
"Id": "00000000-0000-0000-0000-000000000000","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如何解析其响应?
解决方法
Dialogflow CX webhooks与Dialogflow ES webhooks类似,除了request和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','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 云函数中使用以下代码段作为网络钩子实现。
重要的是,为了让代理正确处理 webhook 响应,例如 fulfillmentResponse
到 fulfillment_response
,我必须从camelCase 切换到snake_case。
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。