Not working example for Dialogflow V2 api

前端 未结 1 724
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 23:52

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.Dialogf

相关标签:
1条回答
  • 2021-01-28 00:26

    Have you tried connecting using the service account private key ? (Json file)

    Follow these steps (working example in C#)

    1. After you create a Dialogflow agent go to the agent's settings --> General --> click on the Service Account link
    2. You will be sent to to google cloud platform where you can create a service account
    3. After you create a service account, there will be an option to create a KEY, create it and download the (JSON) format of it
    4. This key will be used to connect from your C# project to the Dialogflow agent
    5. Install Google.Cloud.Dialogflow.V2 package in your project
    6. Create for example a Dialogflow manager class (check below for an example)

          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. 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}");
      
    0 讨论(0)
提交回复
热议问题