[英]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#. 我正在尝试创建一个Web挂钩,但是几乎没有关于如何在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!). 目标是响应一个带有用户要求选择答案的问题的webhook呼叫(为此很高兴使用训练短语!)。 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#. 问题是,我不知道如何在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: 这是我自己的方法,当POST发送到我的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. 包含Payload = new Struct()
是令人困惑的部分。 Apparently that is of type Google.Protobuf.WellKnownTypes.Struct
. 显然,该类型为Google.Protobuf.WellKnownTypes.Struct
。 How can I create something that will be represent the json above? 我如何创建将代表上述json的内容?
This is all part of an application I am building for Actions on Google, but using the DialogFlow method. 这是我在Google上为Actions构建的应用程序的全部部分,但是使用DialogFlow方法。 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: 答案是这样的:
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. 我希望他们自由交谈,然后我将尝试匹配答案(并返回我的API以获得另一个成就)或进行回退,然后可能会列出答案。
Creating the response in ac# WebApi controller could be as simple as: 在ac#WebApi控制器中创建响应可以很简单:
var dialogflowResponse = new WebhookResponse
{
FulfillmentText = "Your user response"
};
var response = Request.CreateResponse(
HttpStatusCode.OK, dialogflowResponse, "application/json"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.