简体   繁体   中英

Google Dialogflow v2 using c#

I am trying to create a web hook, but there is little to no documentation on how to do this in c#. The goal is to response to a webhook call with a question that requires the user to select an answer (would be nice to use training phrases for this!). So far I have only managed to give a basic response, so I am trying to expand on that....

Now I have read that the response should look something like this :

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "richResponse": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": "We have more to do"
            }
          }
        ]
      }
    }
  }
}

The problem is, I don't know how to do this in c#. Currently, I have this method:

public async Task<string> ProcessAsync(WebhookRequest request)
{
    var response = new WebhookResponse
    {
        FulfillmentText = "Something went wrong"
    };

    var keyValuePair = request.QueryResult.Parameters.Fields.SingleOrDefault(m => m.Key.Equals("categories"));
    if (keyValuePair.Equals(default(KeyValuePair<string, Value>))) return response.ToString();

    var category = keyValuePair.Value;
    var questions = await ListScenariosAsync(category.StringValue);

    if (questions.Count != 1) return response.ToString();

    var question = questions.First();
    response = new WebhookResponse
    {
        Payload = new Struct()
    };
    return response.ToString();
}

This is my own method which is called when a POST is sent to my API:

/// <inheritdoc />
/// <summary>
/// Used for an google home api calls
/// </summary>
[RoutePrefix("google")]
public class GoogleController : ApiController
{
    private readonly IGoogleHomeProvider _provider;

    /// <inheritdoc />
    public GoogleController(IGoogleHomeProvider questionProvider) => _provider = questionProvider;

    /// <summary>
    /// Handles a webhook request
    /// </summary>
    /// <param name="model">The request model</param>
    /// <returns></returns>
    [HttpPost]
    [Route("")]
    public async Task<HttpResponseMessage> Post(WebhookRequest model)
    {
        var result = await _provider.ProcessAsync(model);
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(result, Encoding.UTF8, "application/json");
        return response;
    }
}

the line that states Payload = new Struct() is the confusing part. Apparently that is of type Google.Protobuf.WellKnownTypes.Struct . How can I create something that will be represent the json above?

This is all part of an application I am building for Actions on Google, but using the DialogFlow method. And this is supposed to be the fulfilment. As you may have guessed, I am new to this process and learning as I go. What I want to do is return a question (which has a number of answers) and I would like the user then to be able to say something that would relate to one of them answers. For example, in this case the question is:

How will you use this?

And the answers are something like:

  • For holidays and days out
  • Professionally
  • For fun
  • With the family

I mentioned training phrases because I don't expect the user to know the answers beforehand. I want them to talk freely and then I will try to match an answer (and come back to my API for another fulfilment) or go to a fallback which will then probably list out the answers.

Creating the response in ac# WebApi controller could be as simple as:

var dialogflowResponse = new WebhookResponse
{
     FulfillmentText = "Your user response"
};
var response = Request.CreateResponse(
    HttpStatusCode.OK, dialogflowResponse, "application/json"
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM