如何将连接框架对话框与api.ai客户端连接

时间:2017-03-02 10:52:00

标签: c# bots botframework dialogflow

我在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);
  }

Faq with dialog我已经连接了一个对话框类。

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

2 个答案:

答案 0 :(得分:0)

我要做的是创建一个包含Faq值的枚举:

  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;
    }
} 

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

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

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

{{1}}

答案 1 :(得分:0)

[UPDATE]

C#

连接到对话框流(以前称为API.AI )

遵循这些步骤(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}");