首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将机器人框架对话框连接到api.ai客户端

如何将机器人框架对话框连接到api.ai客户端
EN

Stack Overflow用户
提问于 2017-03-02 18:52:00
回答 2查看 897关注 0票数 0

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

我有一段代码:

代码语言:javascript
复制
  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。你知道怎么做吗?

EN

回答 2

Stack Overflow用户

发布于 2017-03-08 00:39:51

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

代码语言:javascript
复制
Public enum Faq{
 Undefined,
 Menu,
 Dialog
}

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

代码语言:javascript
复制
  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;
    }
} 

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

代码语言:javascript
复制
var response = MatchAiIntent(faq);
if (response == Faq.Menu)
  {
      await context.PostAsync("Under construction");
  }
票数 0
EN

Stack Overflow用户

发布于 2019-10-18 17:27:12

更新

从连接到Dialogflow(以前称为API.AI)

遵循以下步骤(C#中的工作示例)

  1. 创建对话流代理后,请转到该代理的设置-->常规-->单击服务帐户链接,您将被发送到

云平台,您可以在其中创建服务account

  1. After您创建了一个服务帐户,将会出现一个创建KEY的选项。创建它并下载它的(JSON)格式这个密钥将用于从您的C#项目连接到您的项目中的对话流agent
  2. Install Google.Cloud.Dialogflow.V2
  3. 创建例如一个对话流管理器类(请查看下面的示例)

公共类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 =;=;();} private void SetEnvironmentVariable() { try { Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS",_contentRootPath + "\Keys\{THE_DOWNLOADED_JSON_FILE_HERE}.json");}SetEnvironmentVariable(_contentRootPath){ArgumentNullException;} catch (ArgumentException) { throw;} catch (SecurityException) { throw;}}私有异步任务CreateSession() { //创建客户端_sessionsClient =等待SessionsClient.CreateAsync();//初始化请求参数_sessionName =新的SessionName(_projectId,_userID);}公共异步任务< QueryResult > CheckIntent(string userInput,string LanguageCode = "en") {等待CreateSession();QueryInput queryInput =新的QueryInput();var queryText = _sessionsClient.DetectIntentAsync(_sessionName,TextInput();queryText.Text = userInput;queryText.LanguageCode = LanguageCode;queryInput.Text = queryText;//使请求响应DetectIntentResponse = await var queryInput);返回response.QueryResult;}}

  • ,然后可以像这样调用它来获取检测意图

等待对话流= DialogflowManager DialogflowManager("{INSERT_USER_ID}",_hostingEnvironment.WebRootPath,_hostingEnvironment.ContentRootPath,"{INSERT_AGENT_ID");var dialogflowQueryResult =等待DialogflowManager

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42553371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档