C# 如何在ASP.Net Core中使用Google.Protobuf.WellknownTypes.Struct
我正在为我的Google Dialogflow代理实现一个webhookreceiver作为ASP.Net核心Webapp。为此,我使用了Google.Cloud.Dialogflow.V2(1.0.0-beta02)nuget包。当Dialogflow符合某个意图时,我希望我的ASP webapp调用TMDb API来检索特定电影的信息。我想把这些信息作为谷歌卡片发送回Dialogflow。 要将内容发送回Dialogflow,我使用WebhookResponse类(由Google.Cloud.Dialogflow.V2提供)。现在我的问题在于如何匹配Dialogflow期望的模式,如下所示:C# 如何在ASP.Net Core中使用Google.Protobuf.WellknownTypes.Struct,c#,asp.net-core,dialogflow-es,C#,Asp.net Core,Dialogflow Es,我正在为我的Google Dialogflow代理实现一个webhookreceiver作为ASP.Net核心Webapp。为此,我使用了Google.Cloud.Dialogflow.V2(1.0.0-beta02)nuget包。当Dialogflow符合某个意图时,我希望我的ASP webapp调用TMDb API来检索特定电影的信息。我想把这些信息作为谷歌卡片发送回Dialogflow。 要将内容发送回Dialogflow,我使用WebhookResponse类(由Google.Cloud
"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
}
]
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/""
}]
}
}]
}";
到目前为止,我发现我需要上面的Json作为Webhookresponse负载的一部分。()
但是使用Github注释中提供的方法很难匹配上述模式。
我知道Json对象(位于一对花括号之间的所有对象)相当于这行代码
Value.ForStruct(new Struct { Fields = { ["expectUser"] = Value.ForBool(true) } })
我似乎不知道什么是Json数组的等价物(在一对方括号之间)
我想我必须使用
Value.ForList()
但当我尝试它时,它不会编译(请参阅下面的代码)
这个片段应该是第一个代码块的Json结构的前8行。(我已经尽可能地将其格式化,不应该缺少任何括号)
VS2017中的错误是:
Argument 1: cannot convert from 'Google.Protobuf.WellKnownTypes.Struct' to 'Google.Protobuf.WellKnownTypes.Value'
有人知道如何使用Google.Protobuf吗?或者有没有其他方法让我的Dialogflow代理显示卡片
感谢您的帮助。结构本身不是
值
——您需要使用Struct.ForValue
来创建值
。这有点冗长,但确实有效。但是,按钮
看起来还是需要另一个列表。下面是为有效负载创建原始JSON的完整示例:
使用Google.Protobuf.WellKnownTypes;
使用制度;
班级计划
{
静态void Main(字符串[]参数)
{
var按钮=Value.ForStruct(新结构
{
田地=
{
[“回发”]=Value.ForString(“卡链接URL或文本”),
[“文本”]=Value.ForString(“卡片链接标题”)
}
});
var message=Value.ForStruct(新结构
{
田地=
{
[“按钮”]=Value.ForList(按钮),
[“imageUrl”]=Value.ForString(“http://urltoimage.com"),
[“平台”]=Value.ForString(“facebook”),
[“subtitle”]=Value.ForString(“卡片字幕”),
[“标题”]=Value.ForString(“购物车标题”),
[“类型”]=数值。用于数字(1)
}
});
var payload=new Struct{Fields={[“messages”]=Value.ForList(message)};
控制台写入线(有效载荷);
}
}
我怀疑一些助手方法可以使这更简单,但这至少可以解除您的阻碍。好的,多亏了Jon Skeets的回答,我能够获得
WebhookResponse的正确格式,但它没有像我想的那样工作。Dialogflow需要通过只读的FulfillmentMessages
属性获取显示卡的信息。我的解决方法是根本不使用WebhookResponse
类,只发送回我自己拼凑的JSON字符串。我发回的字符串如下所示:
"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
}
]
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/""
}]
}
}]
}";
这是一篇旧文章,但使用dialogflow 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>"
}
}
}
}
}
}
};
FulfillmentMessages=
{
新的Intent.Types.Message
{
新的Intent.Types.Message
{
平台=Platform.ActionsOnGoogle,
BasicCard=新BasicCard
{
Title=“”,
副标题=”,
FormattedText=“”,
图像=新图像
{ImageUri=”“,
AccessibilityText=“”
},
钮扣=
{
新BasicCard.Types.Button
{
Title=“”,
OpenUriAction=新建BasicCard.Types.Button.Types.OpenUriAction
{
Uri=“”
}
}
}
}
}
}
};
如果您仍然需要使用v2 api,我希望这可以帮助其他人或您。Dialogflow库为和提供消息类型。有没有一种方便的方法可以将这些类型的实例转换为结构值?@ChanceSnow:恐怕目前还没有。您可以编写自己的代码来使用protobuf反射来实现这一点,我可以想象它可能会在某个时候出现在主protobuf库中,但我不相信它已经存在。