【问题标题】:How to connect Bot Framework dialog with api.ai client如何将 Bot Framework 对话框与 api.ai 客户端连接
【发布时间】:2019-10-18 17:27:11
【问题描述】:

我正在使用 C# 中的 Bot Framework 创建一个机器人

我有这段代码:

  var faq = await result;

  if (faq == "Faq with menu")
  {
      await context.PostAsync("Under construction");
  }
  else if (faq == "Faq with dialog")
  {
      context.Call(new FaqDialog(), this.ResumeAfterOptionDialog);
  }

带有对话框的常见问题解答 我已经连接了一个对话框类。

我想在Api.ai 中将 Faq 与菜单与我的客户联系起来。你知道怎么做吗?

【问题讨论】:

    标签: c# bots botframework dialogflow-es


    【解决方案1】:

    我要做的是使用常见问题值创建一个枚举:

    Public enum Faq{
     Undefined,
     Menu,
     Dialog
    }
    

    然后创建一个方法,该方法将使用用户消息调用 Api.ai 并将意图响应映射到枚举:

      public T MatchAiIntent<T>(string message) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enum type!");
            }
    
        T result = default(T);
        try
        {
            var response = apiAi.TextRequest(message);
            var intentName = response?.Result?.Metadata?.IntentName;
    
            if (intentName == null)
            {
                return result;
            }
    
            Enum.TryParse<T>(intentName, true, out result);
    
            return result;
        }
        catch (Exception exception)
        {
            //logit
            throw;
        }
    } 
    

    然后你可以在你的代码中使用它:

    var response = MatchAiIntent(faq);
    if (response == Faq.Menu)
      {
          await context.PostAsync("Under construction");
      }
    

    【讨论】:

      【解决方案2】:

      [更新]

      C#

      连接到 Dialogflow以前称为 API.AI

      按照这些步骤(C# 中的工作示例)

      1. 创建 Dialogflow 代理后,转到代理设置 --> 常规 --> 点击服务帐户链接
      2. 您将被转到谷歌云平台,您可以在其中创建服务帐户
      3. 创建服务帐号后,会出现创建KEY的选项,创建并下载它的(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. 然后可以像这样调用它来检测 Intents

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

      【讨论】:

        猜你喜欢
        • 2017-12-02
        • 2022-11-10
        • 1970-01-01
        • 2017-12-04
        • 2017-04-27
        • 2016-10-30
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多