c# - 如何在机器人框架 v4 中从 ActivityHandler.OnMessageActivityAsync 启动瀑布对话框
问题描述
我正在尝试编写一个简单的机器人,当用户输入某些内容时,它将启动我的瀑布对话框。用例非常简单,但它似乎不起作用,有什么问题?
主机器人是这样设置的,我尝试在 OnMessageActivityAsync 函数中调用我的对话框:
namespace EmptyBot1.Dialogs
{
public class MainChatbot : ActivityHandler
{
private readonly IOptions<Models.Configurations> _mySettings;
protected readonly IRecognizer _recognizer;
protected readonly BotState _conversationState;
public MainChatbot(ConversationState conversationState, IOptions<Models.Configurations> mySettings, ChatbotRecognizer recognizer)
{
_mySettings = mySettings ?? throw new ArgumentNullException(nameof(mySettings));
_recognizer = recognizer;
_conversationState = conversationState;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
string LuisAppId = _mySettings.Value.LuisAppId;
string LuisAPIKey = _mySettings.Value.LuisAPIKey;
string LuisAPIHostName = _mySettings.Value.LuisAPIHostName;
await turnContext.SendActivityAsync(MessageFactory.Text($"You Said: {turnContext.Activity.Text}"), cancellationToken);
var luisResult = await _recognizer.RecognizeAsync<Models.ChatbotIntent>(turnContext, cancellationToken);
Models.ChatbotIntent.Intent TopIntent = luisResult.TopIntent().intent;
await turnContext.SendActivityAsync(MessageFactory.Text($"Your Intention Is: {TopIntent.ToString()}"), cancellationToken);
switch (TopIntent)
{
case Models.ChatbotIntent.Intent.RunBot:
var RunBotOptions = new Models.RunBotOptions();
Dialog d = new MyCustomDialog();
// Trying to start my dialog here.
await d.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
break;
default:
break;
}
return;
}
}
}
然后我像这样设置我的对话框,也很简单:
namespace EmptyBot1.Dialogs
{
public class MyCustomDialog : InteruptsDialog
{
public MyCustomDialog()
: base(nameof(MyCustomDialog))
{
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
AskName,
AskUseDefault,
FinalStep
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
// ...
}
}
一切都注入到 startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<Models.Configurations>(Configuration);
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, Dialogs.MainChatbot>();
// Create the Conversation state. (Used by the Dialog system itself.)
var storage = new MemoryStorage();
var conversationState = new ConversationState(storage);
services.AddSingleton(conversationState);
// Register LUIS recognizer
services.AddSingleton<ChatbotRecognizer>();
services.AddSingleton<Dialogs.MyCustomDialog>();
}
但是当我运行它时,我得到 500 错误,我做错了什么?
编辑: 澄清一下,我的目标是能够直接从ActivityHandler.OnMessageActivityAsync
.
来自在线和来自 Microsoft 的示例项目的一般解决方案都说将对话框作为类型 T 传递给我的机器人。
但是,我已经确切知道要启动哪个对话框,因此需要将其作为类型传递,我可以直接在机器人内部对其进行硬编码,我该如何启动它?
解决方案
结果我的代码似乎工作正常,不知道为什么它昨天不工作。我会把它留给未来的人检查答案。您可以完全按照问题中的方式使用它。
推荐阅读
- ssh - 使用代理挂载远程服务器
- node.js - 使用exceljs将excel文件导出到客户端
- python - 如何从 cmd 获取 python.exe 路径?
- java - Java中的变量初始化
- css - TailwindCSS Flexbox 导航栏链接未内联显示
- vue.js - 如何将 json 文件导入和读取到 Vue / Gridsome main.js
- seal - 旋转密文向量时出现错误,
- docker - kubernetes kubectl 是否与镜像一起运行创建部署 yaml 文件
- jquery - 在 laravel 6 中使用 jquery
- amazon-web-services - 如何使用应用程序和通过 3G/LTE 连接的物联网设备设置云