我正在尝试编写一个简单的机器人,当用户输入内容时,它将启动我的瀑布对话框。用例非常简单,但似乎不起作用,怎么了?
主机器人是这样设置的,我尝试在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型传递给我的机器人。但是,我已经确切知道要启动哪个对话框,因此需要将其作为类型传递,我可以直接在bot内对其进行硬编码,如何启动它?
据我所知,在启动时添加机器人本身并不是在添加机器人。你有
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, Dialogs.MainChatbot>();
尝试:
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, MainChatbot<MyCustomDialog>>();
为了做到这一点,您将不得不更改MainChatBot。在班级任务中,您具有:
public class MainChatbot : ActivityHandler
将其更改为:
public class MainChatbot<T> : ActivityHandler
where T : Dialog
您在其中拥有主要的“机器人”,但是直到得到LUIS意向后您才调用对话框。但是,在启动对话框之前,您不能调用LUIS意图。而是使用对话框初始化您的机器人,因此您的机器人实际上知道从哪里“开始”。
结果是我的代码似乎运行良好,不确定为什么昨天无法运行。我将其留给将来的人检查答案。您可以完全按照问题中的说明使用它。