登录
首页 >  Golang >  Go问答

如何在 Dialogflow v2 WebhookResponse 中包含 FulfillmentMessages?

来源:stackoverflow

时间:2024-02-28 19:03:25 177浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《如何在 Dialogflow v2 WebhookResponse 中包含 FulfillmentMessages?》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

问题内容

我正在尝试将履行消息作为 dialogflow 的 v2 api webhookresponse 的一部分发回。

这有效:

仅发送 fulfillmenttext 作为我的回复的一部分效果很好(在 google simulator 上的操作中测试应用会回复正确的 fulfillmenttext 值):

func random(c *gin.context, wr dialogflow.webhookrequest)  {
    log.println("random action detected")

    fullfillment := dialogflow.webhookresponse{
        fulfillmenttext: "foobar",
    }

    c.json(http.statusok, fullfillment)
}

发送回的 json:

{"fulfillment_text":"foobar"}

模拟器中的响应:

{
    "conversationtoken": "[]",
    "finalresponse": {
    "richresponse": {
        "items": [
            {
                "simpleresponse": {
                    "texttospeech": "foobar"
                }
            }
        ]
    }
},
    "responsemetadata": {
    "status": {
        "message": "success (200)"
    },
    "querymatchinfo": {
        "querymatched": true,
            "intent": "24db2044-f2fb-4607-9897-1de757990622"
    }
}
}

这不会:

但是,一旦我尝试将任何实际消息(短信、基本卡、简单响应等)作为 fulfillmentmessages 的一部分发回,测试就会失败:

func random(c *gin.context, wr dialogflow.webhookrequest)  {
    log.println("random action detected")

    textmessage := dialogflow.intent_message_text{
        text: []string{"foo", "bar"},
    }

    fullfillment := dialogflow.webhookresponse{
        fulfillmenttext: "foobar",
        fulfillmentmessages: []*dialogflow.intent_message{
            {
                message: &dialogflow.intent_message_text_{
                    text: &textmessage,
                },
            },
        },
    }

    c.json(http.statusok, fullfillment)
}

发送回的 json:

{
    "fulfillment_text":"foobar",
    "fulfillment_messages":[
        {
            "message":{
                "text":{
                    "text":[
                        "foo",
                        "bar"
                    ]
                }
            }
        }
    ]
}

模拟器中的响应:

{
    "responseMetadata": {
    "status": {
        "code": 10,
            "message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
            "details": [
            {
                "@type": "type.googleapis.com/google.protobuf.Value",
                "value": "{\"id\":\"917d8ac3-3f0f-4953-b556-4dec27b8bbb8\",\"timestamp\":\"2018-10-22T09:00:45.488Z\",\"lang\":\"en-us\",\"result\":{},\"alternateResult\":{},\"status\":{\"code\":206,\"errorType\":\"partial_content\",\"errorDetails\":\"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: Message in message google.cloud.dialogflow.v2.Intent.Message.\"},\"sessionId\":\"ABwppHHSbrqOCPRp_DAPDLepL6YjSNpbzQ61CIBDTMl99rtRqfaWq-y0HtExb1--k6bcaL4CACQMeiVF3p-x5qk\"}"
            }
        ]
    }
}
}

我假设我的 web 服务发回的 json 是错误的,因为它返回 ... 找不到字段:消息 ... 作为其响应的一部分。 我正在为 dialogflow 使用正确的 golang sdk (https://godoc.org/google.golang.org/genproto/googleapis/cloud/dialogflow/v2#webhookresponse)

这已在 google 模拟器上的 actions 上进行了测试,并在 pixel 2 上运行实际的 google assistant。

有人能指出我做错的事情的正确方向吗?


解决方案


正如您所说,问题出在响应的 json 结构上。下面是一个有效的 webhook 响应

{
"fulfillmentMessages": [
    {
        "text": {
            "text": [
                "foo",
                "bar"
            ]
        }
    }
],
"fulfillmentText": "foobar",
"source": "Test"
}
  1. 实际结构中不存在消息
  2. fullfillment_text 应为fulfillmenttext
  3. 外部文本应为文本
  4. fullfillment_messages应为fulfillmentmessages

今天关于《如何在 Dialogflow v2 WebhookResponse 中包含 FulfillmentMessages?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>