Dialogflow V2 API的无效示例

时间:2018-10-22 17:03:37

标签: c# dialogflow

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_

没有对方法ToChannelCredentials()的引用。 即使空白项目,我们也无法将SDK连接到dialogflow。此方法仍然存在还是不建议使用?

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();

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用服务帐户私钥进行连接? ( Json文件

遵循这些步骤(C#中的工作示例)

  1. 创建Dialogflow代理后,转到代理的设置->常规->单击服务帐户链接
  2. 您将被发送到Google Cloud Platform,您可以在其中创建服务帐户
  3. 创建服务帐户后,将提供一个选项,用于创建密钥,创建并下载其(JSON)格式
  4. 此键将用于从C#项目连接到Dialogflow代理
  5. 在项目中安装 Google.Cloud.Dialogflow.V2 程序包
  6. 创建例如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;
        }
    }
    
  7. 然后可以像这样调用它,例如获取检测意图

         DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}",
        _hostingEnvironment.WebRootPath,
        _hostingEnvironment.ContentRootPath,
        "{INSERT_AGENT_ID");
    
    var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");