如何在 ASP.Net 核心中使用 Google.Protobuf.WellknownTypes.Struct
How to use Google.Protobuf.WellknownTypes.Struct in ASP.Net Core
我正在为我的 Google Dialogflow Agent 实现一个 webhookreceiver 作为 ASP.Net 核心 Webapp。为此,我使用 Google.Cloud.Dialogflow.V2(1.0.0-beta02) nuget 包。当 Dialogflow 匹配某个意图时,我希望我的 ASP webapp 调用 TMDb API 来检索有关特定电影的信息。我想将此信息作为 google 卡片发送回 Dialogflow。
为了将内容发送回 Dialogflow,我使用 WebhookResponse class(由 Google.Cloud.Dialogflow.V2 提供)。现在我的问题在于如何匹配 Dialogflow 期望的模式,它看起来像这样:
"messages": [
{
"buttons": [
{
"postback": "Card Link URL or text",
"text": "Card Link Title"
}
],
"imageUrl": "http://urltoimage.com",
"platform": "facebook",
"subtitle": "Card Subtitle",
"title": "Card Title",
"type": 1
}
]
到目前为止我发现我需要上述 Json 作为 Webhook 响应的有效负载的一部分。 (https://github.com/googleapis/google-cloud-dotnet/issues/2425#issuecomment-459885762)
但是使用Github评论中提供的方法很难匹配到上面的模式。
我知道 Json 对象(一对大括号之间的所有内容)等同于这行代码
Value.ForStruct(new Struct { Fields = { ["expectUser"] = Value.ForBool(true) } })
我似乎无法找出什么是 Json 数组的等效项(一对方括号之间的所有内容)
我假设我必须使用
Value.ForList()
但是当我尝试它时它不会编译(请参阅下面的代码我是如何尝试的)。
Payload = new Struct {
Fields ={["messages"] = Value.ForList(new Struct {
Fields = {["buttons"] = Value.ForStruct(new Struct {
Fields = {["postback"] = Value.ForString("Card link url or test"),
["text"] = Value.ForString("card link title")}
})
} })
}
}
此片段应该是第一个代码块中 Json 结构的前 8 行。 (我已经尽力格式化了,应该不会漏掉任何括号)
VS2017中的错误是:
Argument 1: cannot convert from 'Google.Protobuf.WellKnownTypes.Struct' to 'Google.Protobuf.WellKnownTypes.Value'
有谁知道如何使用 Google.Protobuf 或者是否有其他方法可以让我的 Dialogflow 代理显示卡片?
感谢任何帮助。
Struct
本身不是 Value
- 您需要使用 Struct.ForValue
来创建 Value
。这一切都有些冗长,但确实有效。但是,看起来 buttons
无论如何都需要是另一个列表。这是一个完整的示例,它为有效载荷创建了原始 JSON:
using Google.Protobuf.WellKnownTypes;
using System;
class Program
{
static void Main(string[] args)
{
var button = Value.ForStruct(new Struct
{
Fields =
{
["postback"] = Value.ForString("Card Link URL or text"),
["text"] = Value.ForString("Card Link Title")
}
});
var message = Value.ForStruct(new Struct
{
Fields =
{
["buttons"] = Value.ForList(button),
["imageUrl"] = Value.ForString("http://urltoimage.com"),
["platform"] = Value.ForString("facebook"),
["subtitle"] = Value.ForString("Card Subtitle"),
["title"] = Value.ForString("Cart Title"),
["type"] = Value.ForNumber(1)
}
});
var payload = new Struct { Fields = { ["messages"] = Value.ForList(message) } };
Console.WriteLine(payload);
}
}
我怀疑一些辅助方法可以使这更简单,但这至少应该解除你的阻碍。
好的,感谢 Jon Skeets 的回答,我能够为 WebhookResponse
获得正确的格式,但它并没有像我想的那样工作。 Dialogflow 需要通过 FulfillmentMessages
属性(即 read-only)获取信息以显示卡片。我的解决方法是根本不使用 WebhookResponse
class 并只发回我自己组合的 JSON 字符串。我发回的字符串如下所示:
string testResponse = @"{
""fulfillmentText"": ""This is a text response"",
""fulfillmentMessages"": [
{
""card"":
{
""title"": ""card title"",
""subtitle"": ""card text"",
""imageUri"": ""https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png"",
""buttons"": [
{
""text"": ""button text"",
""postback"": ""https://assistant.google.com/""
}]
}
}]
}";
这是一个旧的 post 但使用对话流 Api 的正确方法是:
FulfillmentMessages =
{
new Intent.Types.Message
{
new Intent.Types.Message
{
Platform = Platform.ActionsOnGoogle,
BasicCard = new BasicCard
{
Title = "<Your Title>",
Subtitle="<Your SubTitle>",
FormattedText = "<Your Text>",
Image = new Image
{ ImageUri = "<your Image Url>",
AccessibilityText = "<your text here>"
},
Buttons =
{
new BasicCard.Types.Button
{
Title = "<Your Button Text>",
OpenUriAction = new BasicCard.Types.Button.Types.OpenUriAction
{
Uri = "<your visit url>"
}
}
}
}
}
}
};
如果您仍然需要使用 v2,我希望这可以帮助其他人或您 api。
我正在为我的 Google Dialogflow Agent 实现一个 webhookreceiver 作为 ASP.Net 核心 Webapp。为此,我使用 Google.Cloud.Dialogflow.V2(1.0.0-beta02) nuget 包。当 Dialogflow 匹配某个意图时,我希望我的 ASP webapp 调用 TMDb API 来检索有关特定电影的信息。我想将此信息作为 google 卡片发送回 Dialogflow。 为了将内容发送回 Dialogflow,我使用 WebhookResponse class(由 Google.Cloud.Dialogflow.V2 提供)。现在我的问题在于如何匹配 Dialogflow 期望的模式,它看起来像这样:
"messages": [
{
"buttons": [
{
"postback": "Card Link URL or text",
"text": "Card Link Title"
}
],
"imageUrl": "http://urltoimage.com",
"platform": "facebook",
"subtitle": "Card Subtitle",
"title": "Card Title",
"type": 1
}
]
到目前为止我发现我需要上述 Json 作为 Webhook 响应的有效负载的一部分。 (https://github.com/googleapis/google-cloud-dotnet/issues/2425#issuecomment-459885762)
但是使用Github评论中提供的方法很难匹配到上面的模式。 我知道 Json 对象(一对大括号之间的所有内容)等同于这行代码
Value.ForStruct(new Struct { Fields = { ["expectUser"] = Value.ForBool(true) } })
我似乎无法找出什么是 Json 数组的等效项(一对方括号之间的所有内容) 我假设我必须使用
Value.ForList()
但是当我尝试它时它不会编译(请参阅下面的代码我是如何尝试的)。
Payload = new Struct {
Fields ={["messages"] = Value.ForList(new Struct {
Fields = {["buttons"] = Value.ForStruct(new Struct {
Fields = {["postback"] = Value.ForString("Card link url or test"),
["text"] = Value.ForString("card link title")}
})
} })
}
}
此片段应该是第一个代码块中 Json 结构的前 8 行。 (我已经尽力格式化了,应该不会漏掉任何括号)
VS2017中的错误是:
Argument 1: cannot convert from 'Google.Protobuf.WellKnownTypes.Struct' to 'Google.Protobuf.WellKnownTypes.Value'
有谁知道如何使用 Google.Protobuf 或者是否有其他方法可以让我的 Dialogflow 代理显示卡片?
感谢任何帮助。
Struct
本身不是 Value
- 您需要使用 Struct.ForValue
来创建 Value
。这一切都有些冗长,但确实有效。但是,看起来 buttons
无论如何都需要是另一个列表。这是一个完整的示例,它为有效载荷创建了原始 JSON:
using Google.Protobuf.WellKnownTypes;
using System;
class Program
{
static void Main(string[] args)
{
var button = Value.ForStruct(new Struct
{
Fields =
{
["postback"] = Value.ForString("Card Link URL or text"),
["text"] = Value.ForString("Card Link Title")
}
});
var message = Value.ForStruct(new Struct
{
Fields =
{
["buttons"] = Value.ForList(button),
["imageUrl"] = Value.ForString("http://urltoimage.com"),
["platform"] = Value.ForString("facebook"),
["subtitle"] = Value.ForString("Card Subtitle"),
["title"] = Value.ForString("Cart Title"),
["type"] = Value.ForNumber(1)
}
});
var payload = new Struct { Fields = { ["messages"] = Value.ForList(message) } };
Console.WriteLine(payload);
}
}
我怀疑一些辅助方法可以使这更简单,但这至少应该解除你的阻碍。
好的,感谢 Jon Skeets 的回答,我能够为 WebhookResponse
获得正确的格式,但它并没有像我想的那样工作。 Dialogflow 需要通过 FulfillmentMessages
属性(即 read-only)获取信息以显示卡片。我的解决方法是根本不使用 WebhookResponse
class 并只发回我自己组合的 JSON 字符串。我发回的字符串如下所示:
string testResponse = @"{
""fulfillmentText"": ""This is a text response"",
""fulfillmentMessages"": [
{
""card"":
{
""title"": ""card title"",
""subtitle"": ""card text"",
""imageUri"": ""https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png"",
""buttons"": [
{
""text"": ""button text"",
""postback"": ""https://assistant.google.com/""
}]
}
}]
}";
这是一个旧的 post 但使用对话流 Api 的正确方法是:
FulfillmentMessages =
{
new Intent.Types.Message
{
new Intent.Types.Message
{
Platform = Platform.ActionsOnGoogle,
BasicCard = new BasicCard
{
Title = "<Your Title>",
Subtitle="<Your SubTitle>",
FormattedText = "<Your Text>",
Image = new Image
{ ImageUri = "<your Image Url>",
AccessibilityText = "<your text here>"
},
Buttons =
{
new BasicCard.Types.Button
{
Title = "<Your Button Text>",
OpenUriAction = new BasicCard.Types.Button.Types.OpenUriAction
{
Uri = "<your visit url>"
}
}
}
}
}
}
};
如果您仍然需要使用 v2,我希望这可以帮助其他人或您 api。