Gow*_*ham 8 c# google-api dialogflow-es
我是Google API的新手。我想知道如何在C#中调用Google Dialogflow API,以获取输入文本的意图。但是我找不到使用C#调用Dialogflow的任何示例。
请提供一些示例以从C#调用Dialogflow。
小智 6
如果我正确理解了您的问题,那么您想从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();
Run Code Online (Sandbox Code Playgroud)
在DialogFlowAPI的早期版本中,尝试重新部署channel.ShutDownAsync()似乎可以解决的Web api项目时遇到了文件锁定问题。我认为这已在最近的版本中修复。
这是我使用的DF请求的最简单版本。这篇文章中有一个更复杂的版本在输入上下文中传递:使用 C#进行DialogFlow v2 DetectIntent调用(包括输入上下文)
(吹毛求疵:我假设您知道 DialogFlow 会按照 DialogFlow 操作中指定/注册的方式调用您的代码?因此您的代码只能响应 DialogFlow,而不能调用它。)
简短回答/重定向:
不要使用谷歌。Apis .Dialogflow.v2(使用GoogleCloudDialogflowV2WebhookRequest和GoogleCloudDialogflowV2WebhookResponse)但使用 Google。Cloud .Dialogflow.v2(带有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" }; ;
}
Run Code Online (Sandbox Code Playgroud)
编辑:事实证明,模型绑定可能无法正确绑定“ProtoBuf-json”中的所有属性(例如WebhookRequest.outputContexts[N].parameters),因此人们可能应该使用Google.Protobuf.JsonParser(例如,请参阅此文档)。
该解析器可能会遇到未知字段,因此人们可能也想忽略它。所以现在我使用这段代码(有一天我可以通过创建一个参数使通用方法更加通用,从而变得有用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;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句:这个“ProtoBuf-json”也是使用它的原因,而WebhookResponse.ToString()它又使用Google.Protobuf.JsonFormatter.ToDiagnosticString.
Microsoft 的 BotBuilder 包和Visual Studio 模板. 我还没有使用过它,但期望大致相同的代码?
传入请求代码(Google 称为 NLU-Response)的简单示例由 Madoka Chiyoda ( Chomado ) 在Github上提供。传入的呼叫被简单地解析为她的DialogFlowResponseModel:
public static async Task<HttpResponseMessage> Run([...]HttpRequestMessage req, [...]CloudBlockBlob mp3Out, TraceWriter log)
...
var data = await req.Content.ReadAsAsync<Models.DialogFlowResponseModel>();
Run Code Online (Sandbox Code Playgroud)
如果您打算稍后不使用DialogFlow,请注意,Gactions 的界面与 DialogFlow 的界面有很大不同。json 参数和返回值有一些重叠,但不会为您赢得任何编程时间(可能会因开始“over”而损失一些时间)。
但是,从 DialogFlow 开始可能会为您带来一些快速的对话体验(例如问答设计/原型设计)。DialogFlow-API 确实有一个 NuGet 包,而 Gactions 界面还没有 NuGet 包。
| 归档时间: |
|
| 查看次数: |
5830 次 |
| 最近记录: |