我在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);
}
Faq with dialog我已经连接了一个对话框类。
我想在Api.ai
中将Faq与我的客户端的菜单相关联。你知道怎么做吗?
答案 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#中的工作示例)
创建例如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;
}
}
然后可以像这样调用它,例如获取检测意图
DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}",
_hostingEnvironment.WebRootPath,
_hostingEnvironment.ContentRootPath,
"{INSERT_AGENT_ID");
var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");