我正在使用C#中的机器人框架创建一个机器人
我有一段代码:
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。你知道怎么做吗?
发布于 2017-03-08 00:39:51
我要做的是创建一个包含Faq值的枚举:
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");
}发布于 2019-10-18 17:27:12
更新
从连接到Dialogflow(以前称为API.AI)
遵循以下步骤(C#中的工作示例)
云平台,您可以在其中创建服务account
公共类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
https://stackoverflow.com/questions/42553371
复制相似问题