I have a webhook set up in ASP.NET Core 2.1. If I use:
public ContentResult Post([FromBody] Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2WebhookRequest webhookRequest)
I can get my parameter by using the following syntax:
queryResult.Parameters["MyParameter"]
However, if I use:
public ContentResult Post([FromBody] Google.Cloud.Dialogflow.v2.WebhookRequest webhookRequest)
There are no parameters:
queryResult.Parameters.Fields["MyParameter"]
QueryResult.Parameters is always empty.
Am I doing something wrong or is this a bug? Thanks.
Firstly, I'm sorry that we don't have documentation for implementing a Dialogflow web hook in C# yet. This has bitten a number of users, and it's on my list of things to do - with some progress this week. It's unlikely that we'll have very comprehensive documentation in the near future, but I'm hoping we can at least have something in the next couple of weeks.
This is effectively the same question as #2238 - I'd suggest reading through that for details. The upshot is that you need to ask the Protobuf code to parse the result, and then to format the result too.
I'd expect something like this:
// Ignore any fields that aren't known to this version of the library
private static JsonParser parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
public ContentResult Post()
{
WebhookRequest request;
using (StreamReader reader = new StreamReader(Request.Body))
{
request = parser.Parse<WebhookRequest>(reader);
}
// Implement your response here
WebhookResponse response = ...;
return new ContentResult
{
Content = response.ToString(),
ContentType = "application/json",
StatusCode = 200
}
}
I've now added an example of how to implement a web hook in ASP.NET Core in the documentation:
https://googlecloudplatform.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/index.html
I'll close this issue now, but do add more comments if it doesn't help.
I've now added an example of how to implement a web hook in ASP.NET Core in the documentation:
https://googlecloudplatform.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/index.html
I'll close this issue now, but do add more comments if it doesn't help.
Hi @jskeet is there any example on how to implement webhook in ASP.NET Framework 4.5.2? I'm having trouble in finding a way to parse the request body. The Request.Body syntax is native to ASP.NET Core only.
@jpocariz: It's been a long time since I've looked at ASP.NET non-core, and from what I remember the functionality was slightly different based on whether you were using Web API, WebForms or MVC (you'd probably want Web API to implement a webhook) but fundamentally you need to get the raw body of the request, and parse that. If you can specify exactly which API you're using (ideally which request type you have access to) I may be able to hunt down the body aspect, but I'd really just be following the same documentation as you.
I am also doing this in .net 4.6.2. The documentation you created is very sparse. It shows how to create an empty response. How do you add a payload to it? You can show me in .net core as I am certain I can decipher the rest :)
@r3plica: I'd suggest reading the Dialogflow Webhook response format documentation - the JSON there mostly corresponds to properties within the WebhookResponse protobuf message (see below). Be aware that repeated fields in the C# representation of protobuf is a read-only property, but you populate it by adding items to it, which you can do with a collection initializer.
For simple responses, you can just set the FulfillmentText message, e.g.
var response = new WebhookResponse
{
FulfillmentText = "This is the response"
};
If you want to populate the Payload property, that's trickier - because it's a Struct (not a C# struct, but a protobuf Struct.) That represents an arbitrary JSON-like message, which is currently a little more awkward to populate. As an example, suppose you wanted to create a response like this:
{
"payload": {
"google": {
"expectUserResponse": true
}
}
You'd use C# like this:
var response = new WebhookResponse
{
Payload = new Struct
{
Fields =
{
["google"] = Value.ForStruct(new Struct { Fields = { ["expectUser"] = Value.ForBool(true) } })
}
}
};
That's clearly not as simple as we'd like it to be, but we don't have the resources to tidy up that part at the moment, I'm afraid. (I recently gave a demo of Dialogflow and didn't use Payload at all - you may want to ask on a Dialogflow-specific forum for more info about the response data. The contributors reading issues in this repo tend to know about .NET-specific aspects, but not about Dialogflow details.
@jskeet Hi, I'm developing a Chatbot for film recommendations using Dialogflow and an ASP.Net Core Webapp as the Webhook. I want to send the response from my ASP.Net app so that Dialogflow will display a Card. But I'm having trouble using the Protobuf Struct, as the structure of a card message is more complex than the above example.
What I'm trying to build as a Protobuf struct is this:
"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
}
]
It's where the Square Brackets are that I'm not sure how to 'write' them in a Protobuf Struct manner. I assume i need Value.ForList but I can't seem to get it to work.
If you could help me with that I'd be very thankful:)
Also, If had multiple Fields in a Protobuf Struct would I separate them like this?:
Fields = { ["expectUser"] = Value.ForBool(true), ["expectUser"] = Value.ForBool(true)}
Any help is appreciated:)
Cheers
@RasmusKauffeld: Could you either file a new issue for this, or possibly ask on Stack Overflow? That would be better than adding to an existing but slightly different question.
It would be good if you could show what you've tried so far and how close it gets. For list values, it's simplest to use Value.ForList(value1, value2, ...) - hopefully that'll help you get started.
Thanks for your quick reply I've made a post on Stack Overflow: https://stackoverflow.com/questions/56078753/how-to-use-google-protobuf-wellknowntypes-struct-in-asp-net-core