Go dialogflow sdk WebhookResponse return 不正确 json
Go dialogflow sdk WebhookResponse doesn't return the correct json
我正在尝试在 Go 中为 Dialogflow 编写一个 webhook,我使用的是官方 SDK 的 apiv2
google.golang.org/genproto/googleapis/cloud/dialogflow/v2
但是我无法使用官方sdk生成正确的响应。
我的意思是,按照文档和 WebhookResponse 结构,我无法生成预期的 json 响应。
这是我正在使用的代码片段:
response = dialogflow.WebhookResponse{
FulfillmentMessages: []*dialogflow.Intent_Message{
{
Message: &dialogflow.Intent_Message_Card_{
Card: &dialogflow.Intent_Message_Card{
Title: "Title",
Subtitle: "Subtitle",
ImageUri: "https://example.com/images/example.png",
Buttons: []*dialogflow.Intent_Message_Card_Button{
{
Text: "Button",
Postback: "https://example.com/path/for/end-user/to/follow",
},
},
},
},
},
},
}
这是它生成的 json:
{
"fulfillment_messages": [
{
"Message": {
"Card": {
"title": "Title",
"subtitle": "Subtitle",
"image_uri": "https://example.com/images/example.png",
"buttons": [
{
"text": "Button",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
}
]
}
但这是我应该发回的 json(根据 official documentation)
"fulfillmentMessages": [
{
"card": {
"title": "card title",
"subtitle": "card text",
"imageUri": "https://example.com/images/example.png",
"buttons": [
{
"text": "button text",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
]
}
所以我的 json 不起作用,因为它有不应该出现的 Message 和首字母大写的 Card。我已尝试发送文档的 json 并且它有效,Dialog Flow 响应正确。
我不明白如何使用官方 SDK 生成正确的 json。请考虑我是使用 Go Lang 的新手。这是我的第一个项目。
这是我目前正在使用的文档:
https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/dialogflow/v2?tab=doc#WebhookResponse
如您所见,FulfillmentMessages 是 Intent_Message
的数组
FulfillmentMessages []*Intent_Message
并且 Intent_Message 必须包含消息(here 文档)
在此先感谢您的帮助和建议。
H2K
更新:
如果我使用 log.Println(response) 我可以在日志中看到正确的响应
fulfillment_messages:{card:{title:"Title" subtitle:"Subtitle" image_uri:"https://example.com/images/example.png" buttons:{text:"Button" postback:"https://example.com/path/for/end-user/to/follow"}}}
它不是 JSON 但结构正确,没有消息,没有卡片...
所以问题是当我用 Gin 和命令 return 时:
c.JSON(200, response)
我找到了解决办法!
我需要使用 jsonpb 封送拆收器 return 它作为 Gin 的字符串
举个例子:
m := jsonpb.Marshaler{}
result, _ := m.MarshalToString(response)
c.String(200, result)
我完全为它着迷,希望它能对其他人有所帮助。
我正在尝试在 Go 中为 Dialogflow 编写一个 webhook,我使用的是官方 SDK 的 apiv2
google.golang.org/genproto/googleapis/cloud/dialogflow/v2
但是我无法使用官方sdk生成正确的响应。
我的意思是,按照文档和 WebhookResponse 结构,我无法生成预期的 json 响应。
这是我正在使用的代码片段:
response = dialogflow.WebhookResponse{
FulfillmentMessages: []*dialogflow.Intent_Message{
{
Message: &dialogflow.Intent_Message_Card_{
Card: &dialogflow.Intent_Message_Card{
Title: "Title",
Subtitle: "Subtitle",
ImageUri: "https://example.com/images/example.png",
Buttons: []*dialogflow.Intent_Message_Card_Button{
{
Text: "Button",
Postback: "https://example.com/path/for/end-user/to/follow",
},
},
},
},
},
},
}
这是它生成的 json:
{
"fulfillment_messages": [
{
"Message": {
"Card": {
"title": "Title",
"subtitle": "Subtitle",
"image_uri": "https://example.com/images/example.png",
"buttons": [
{
"text": "Button",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
}
]
}
但这是我应该发回的 json(根据 official documentation)
"fulfillmentMessages": [
{
"card": {
"title": "card title",
"subtitle": "card text",
"imageUri": "https://example.com/images/example.png",
"buttons": [
{
"text": "button text",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
]
}
所以我的 json 不起作用,因为它有不应该出现的 Message 和首字母大写的 Card。我已尝试发送文档的 json 并且它有效,Dialog Flow 响应正确。
我不明白如何使用官方 SDK 生成正确的 json。请考虑我是使用 Go Lang 的新手。这是我的第一个项目。
这是我目前正在使用的文档: https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/dialogflow/v2?tab=doc#WebhookResponse 如您所见,FulfillmentMessages 是 Intent_Message
的数组FulfillmentMessages []*Intent_Message
并且 Intent_Message 必须包含消息(here 文档)
在此先感谢您的帮助和建议。
H2K
更新: 如果我使用 log.Println(response) 我可以在日志中看到正确的响应
fulfillment_messages:{card:{title:"Title" subtitle:"Subtitle" image_uri:"https://example.com/images/example.png" buttons:{text:"Button" postback:"https://example.com/path/for/end-user/to/follow"}}}
它不是 JSON 但结构正确,没有消息,没有卡片...
所以问题是当我用 Gin 和命令 return 时:
c.JSON(200, response)
我找到了解决办法!
我需要使用 jsonpb 封送拆收器 return 它作为 Gin 的字符串
举个例子:
m := jsonpb.Marshaler{}
result, _ := m.MarshalToString(response)
c.String(200, result)
我完全为它着迷,希望它能对其他人有所帮助。