[英]Not working example for Dialogflow V2 api
Experienced problems with C# SDK documentation which can be found here: http://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/api/Google.Cloud.Dialogflow.V2.SessionsClient.html#Google_Cloud_Dialogflow_V2_SessionsClient_Create_Google_Api_Gax_Grpc_ServiceEndpoint_Google_Cloud_Dialogflow_V2_SessionsSettings_可在此处找到 C# SDK 文档遇到的问题: http : //googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/api/Google.Cloud.Dialogflow.V2.SessionsClient。 html#Google_Cloud_Dialogflow_V2_SessionsClient_Create_Google_Api_Gax_Grpc_ServiceEndpoint_Google_Cloud_Dialogflow_V2_SessionsSettings_
No reference for method ToChannelCredentials(). ToChannelCredentials() 方法没有参考。 We cannot connect the SDK to dialogflow, even with blank project.
我们无法将 SDK 连接到 dialogflow,即使是空白项目。 Is this method still existing or deprecated?
此方法是否仍然存在或已弃用?
using Google.Cloud.Dialogflow.V2;
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
using Grpc.Core;
...
GoogleCredential cred = GoogleCredential.FromFile("/path/to/credentials.json");
Channel channel = new Channel(
SessionsClient.DefaultEndpoint.Host, SessionsClient.DefaultEndpoint.Port, cred.ToChannelCredentials());
SessionsClient client = SessionsClient.Create(channel);
...
// Shutdown the channel when it is no longer required.
channel.ShutdownAsync().Wait();
Have you tried connecting using the service account private key ?您是否尝试过使用服务帐户私钥进行连接? ( Json file )
( Json 文件)
Follow these steps (working example in C#)按照以下步骤操作(C# 中的工作示例)
Create for example a Dialogflow manager class (check below for an example)例如创建一个 Dialogflow 管理器类(查看下面的示例)
public class DialogflowManager { private string _userID; private string _webRootPath; private string _contentRootPath; private string _projectId; private SessionsClient _sessionsClient; private SessionName _sessionName; public DialogflowManager(string userID, string webRootPath, string contentRootPath, string projectId) { _userID = userID; _webRootPath = webRootPath; _contentRootPath = contentRootPath; _projectId = projectId; SetEnvironmentVariable(); } private void SetEnvironmentVariable() { try { Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", _contentRootPath + "\\\\Keys\\\\{THE_DOWNLOADED_JSON_FILE_HERE}.json"); } catch (ArgumentNullException) { throw; } catch (ArgumentException) { throw; } catch (SecurityException) { throw; } } private async Task CreateSession() { // Create client _sessionsClient = await SessionsClient.CreateAsync(); // Initialize request argument(s) _sessionName = new SessionName(_projectId, _userID); } public async Task < QueryResult > CheckIntent(string userInput, string LanguageCode = "en") { await CreateSession(); QueryInput queryInput = new QueryInput(); var queryText = new TextInput(); queryText.Text = userInput; queryText.LanguageCode = LanguageCode; queryInput.Text = queryText; // Make the request DetectIntentResponse response = await _sessionsClient.DetectIntentAsync(_sessionName, queryInput); return response.QueryResult; } }
And then this can be called like this for example to get detect Intents然后可以像这样调用它,例如获取检测意图
DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}", _hostingEnvironment.WebRootPath, _hostingEnvironment.ContentRootPath, "{INSERT_AGENT_ID"); var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.