【问题标题】:Handle Adaptive Card button inside LuisIntent method在 LuisIntent 方法中处理自适应卡片按钮
【发布时间】: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


    【解决方案1】:

    如果您想将按钮的处理与LUIS 意图分开,我认为更好的设计可能是让 RootDialog 做三件事:

    1. 检查传入的活动
    2. 如果activity.Value 为空,则将活动转发到 LuisDialog(使用context.Forward。假设您要检查所有消息是否为 1),那么您必须在执行完之后“结束”LUIS 对话框context.Done 的逻辑(而不是 context.Wait(...)
    3. 如果activity.Value不为null,则可以在RootDialog中处理消息并以context.Wait(...)结尾;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2021-03-24
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多