我是Google API的新手。我想知道如何在C#中调用Google Dialogflow API,以获取输入文本的意图。但是我找不到使用C#调用Dialogflow的任何示例。
请提供一些示例以从C#调用Dialogflow。
如果我正确理解了您的问题,您想从C#应用程序中调用DialogFlow API(而不是编写从DialogFlow调用的实现端点。如果是这种情况,这里是进行该调用的示例:
using Google.Cloud.Dialogflow.V2;
...
...
var query = new QueryInput
{
Text = new TextInput
{
Text = "Something you want to ask a DF agent",
LanguageCode = "en-us"
}
};
var sessionId = "SomeUniqueId";
var agent = "MyAgentName";
var creds = GoogleCredential.FromJson("{ json google credentials file)");
var channel = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host,
creds.ToChannelCredentials());
var client = SessionsClient.Create(channel);
var dialogFlow = client.DetectIntent(
new SessionName(agent, sessionId),
query
);
channel.ShutdownAsync();
在DialogFlowAPI的早期版本中,当尝试重新部署channel.ShutDownAsync()
似乎可以解决的Web api项目时,我遇到了文件锁定问题。我认为此问题已在最新版本中修复。
这是我使用的DF请求的最简单版本。这篇文章在输入上下文中传递了一个更复杂的版本:Making DialogFlow v2 DetectIntent Calls w/ C# (including input context)
(Nitpicking:我想您知道DialogFlow将按在DialogFlow动作中指定/注册的方式调用您的代码吗?所以您的代码只能响应DialogFlow,而不能调用它。 简短回答/重定向:不要使用Google。ApisGoogleCloudDialogflowV2WebhookRequest
和GoogleCloudDialogflowV2WebhookResponse
),而要使用Google。Cloud
WebhookRequest
和WebhookResponse
)-请参见此eTag-error。我还将在下面提及其他替代方案。 使用Google.Cloud.Dialogflow.v2 NuGet(编辑
:FWIW:该代码是为Beta预览编写的:)[HttpPost] public dynamic PostWithCloudResponse([FromBody] WebhookRequest dialogflowRequest) { var intentName = dialogflowRequest.QueryResult.Intent.DisplayName; var actualQuestion = dialogflowRequest.QueryResult.QueryText; var testAnswer = $"Dialogflow Request for intent '{intentName}' and question '{actualQuestion}'"; var dialogflowResponse = new WebhookResponse { FulfillmentText = testAnswer, FulfillmentMessages = { new Intent.Types.Message { SimpleResponses = new Intent.Types.Message.Types.SimpleResponses { SimpleResponses_ = { new Intent.Types.Message.Types.SimpleResponse { DisplayText = testAnswer, TextToSpeech = testAnswer, //Ssml = $"<speak>{testAnswer}</speak>" } } } } } }; var jsonResponse = dialogflowResponse.ToString(); return new ContentResult { Content = jsonResponse, ContentType = "application/json" }; ; }
Edit
:事实证明,模型绑定可能无法正确绑定“ ProtoBuf-json”中的所有属性(例如WebhookRequest.outputContexts[N].parameters
),因此可能应该使用Google.Protobuf.JsonParser
(例如,参见this documentation)。
此解析器可能会越过未知字段,因此可能还想忽略它。因此,现在我使用此代码(有一天,我可以通过将HttpContext.Request.InputStream
设置为参数,使通用方法更通用,因此更有用):
public ActionResult PostWithCloudResponse() { var dialogflowRequest = ParseProtobufRequest<WebhookRequest>(); ... var jsonResponse = dialogflowResponse.ToString(); return new ContentResult { Content = jsonResponse, ContentType = "application/json" }; ; } private T ParseProtobufRequest<T>() where T : Google.Protobuf.IMessage, new() { // parse ProtoBuf (not 'normal' json) with unknown fields, else it may not bind ProtoBuf correctly // https://github.com/googleapis/google-cloud-dotnet/issues/2425 "ask the Protobuf code to parse the result" string requestBody; using (var reader = new StreamReader(HttpContext.Request.InputStream)) { requestBody = reader.ReadToEnd(); } var parser = new Google.Protobuf.JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true)); var typedRequest = parser.Parse<T>(requestBody); return typedRequest; }
BTW:此'ProtoBuf-json'也是使用
WebhookResponse.ToString()
的原因,而C0又使用了Google.Protobuf.JsonFormatter.ToDiagnosticString
。Microsoft的BotBuilder
Microsoft的BotBuilder软件包和Visual Studio template。我还没有使用过,但是期望大致相同的代码?
传入请求代码的简单示例(由Google称为NLU-Response)由Madoka Chiyoda(Chomado)在Github提供。来电简单地解析为她的DialogFlowResponseModel:
DialogFlow,请注意,Gactions的界面与DialogFlow的界面明显不同。json参数和返回值有一些重叠,但是没有任何东西可以让您获得任何编程时间(可能由于“ over”开始而浪费了一些时间)。但是,从DialogFlow开始可能会给您一些快速的对话经验(例如,问题与答案的设计/原型设计)。DialogFlow-API确实具有NuGet包,而Gactions接口没有NuGet包just yet。public static async Task<HttpResponseMessage> Run([...]HttpRequestMessage req, [...]CloudBlockBlob mp3Out, TraceWriter log) ... var data = await req.Content.ReadAsAsync<Models.DialogFlowResponseModel>();
Gactions
如果以后打算使用[[无]