【发布时间】:2018-01-20 02:45:26
【问题描述】:
我正在使用 MS Bot Framework 和 LUIS 开发一个机器人,我的对话流程中有很多带有按钮的自适应卡片。我需要一种使用按钮来处理此流程的方法(准确地说是 Action.Submit 按钮)。
我第一次尝试:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
if (activity.Text != null & activity.Value == null)
{
await Conversation.SendAsync(activity, () => new RootLuisDialog());
}
else if (activity.Text == null & activity.Value != null)
{
await Conversation.SendAsync(activity, () => new ButtonHandler());
}
}
else
{
this.HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
因为当单击自适应卡片上的按钮时activity.Value 包含 Action.Submit 按钮的属性。
这个想法是能够将按钮控制器和意图控制器分开,即RootLuisDialog()。但它没有用,我不知道为什么:使用此代码,总是采用 RootLuisDialog() 退出。换句话说,对话框停留在RootLuisDialog()
第二个想法是像这样使用None Luis Intent:
[LuisIntent("")]
[LuisIntent("None")]
public async Task None(IDialogContext context, LuisResult result)
{
var act = context.Activity as IMessageActivity;
string message = String.Empty;
if (act.Text != null & act.Value == null)
{
message = $"Sorry, I did not understand '{act.Text}'. Type 'help' if you need assistance.";
}
else if (act.Text == null & act.Value != null)
{
Button btn = JsonConvert.DeserializeObject<Button>(act.Value.ToString());
message = $"You clicked on {btn.Type}";
}
await context.PostAsync(message);
context.Wait(this.MessageReceived);
}
虽然这可行,但在 None 意图下按下按钮代码看起来并不正确。
我还尝试了context.Wait(buttonHandler),其中buttonHandler 是处理按钮按下的异步函数,但随后我收到了以下错误消息:
"exceptionMessage": "Object reference not set to an instance of an object.",
"exceptionType": "System.NullReferenceException",
我确信我想要实现的目标已经有了一个很好的答案,但这是我的第一个 C# 项目/任务,我需要帮助来解决这个问题。 非常感谢提前!!!
【问题讨论】:
标签: c# botframework chatbot azure-language-understanding adaptive-cards