How to set the text value of AdaptiveCard Submit button action on BotFramework

时间:2018-02-22 04:28:51

标签: c# botframework luis adaptive-cards

Im trying to integrate Luis with botframework. From what i see (p/s. still new on this), Luis handle the response based on the text input of the user. So when im trying to use Adaptive Card Submit Button action, i can set the value but not the text value. Even if i use dataJson on submit button, it still produce null error. Im still confuse on how to approach this. Code is as follows:

LuisIntent("Greet.Welcome")]
    public async Task QueryGPN(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult luisResult)
    {
        AdaptiveCard gpnCard = new AdaptiveCard();

        gpnCard.Body.Add(new TextBlock()
        {
            Text = "GPN Lookup Form",
            Size = TextSize.Large,
            Weight = TextWeight.Bolder
        });

        TextInput gpnInput = new TextInput()
        {
            Id = "GPN",
            IsMultiline = false
        };

        gpnCard.Body.Add(gpnInput);

        gpnCard.Actions.Add(new SubmitAction()
        {
            Title = "Submit"
        });

        Attachment gpnCardAttachment = new Attachment()
        {
            ContentType = AdaptiveCard.ContentType,
            Content = gpnCard
        };

        IMessageActivity gpnFormMessage = context.MakeMessage();
        gpnFormMessage.Attachments = new List<Attachment>();
        gpnFormMessage.Attachments.Add(gpnCardAttachment);

        await context.PostAsync(gpnFormMessage);
        context.Wait(this.MessageReceived);
    }


[LuisIntent("Curse")]
    public async Task Cursing(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult luisResult)
    {
        Console.WriteLine("Curse");
        await context.PostAsync($"Curse");
        context.Wait(this.MessageReceived);
    }

The situation is i will input curse on the text input and im expecting the bot will redirect to the "Curse" LuisIntent.

TQVM in advanced.

1 个答案:

答案 0 :(得分:4)

我认为问题是因为您使用的是LuisDialog,并且您期望AdaptiveCards的提交操作发送的值被对话框用作LUIS的输入

这个问题的主要问题是提交操作的值不在(新)活动的Text属性中,而是来自Value属性。我怀疑是因为你得到NullReference异常,因为LuisDialog使用该属性来提取要发送给LUIS的值。

好消息是解决这个问题应该非常简单。在幕后,LuisDialog调用GetLuisQueryTextAsync方法从IMessageActivity中提取将发送到LUIS的文本。这种情况发生在MessageReceivedAsync方法上。

因此,我相信通过覆盖GetLuisQueryTextAsync方法,您应该能够更新逻辑并从Value属性而不是Text属性中检索文本。类似的东西:

protected override Task<string> GetLuisQueryTextAsync(IDialogContext context, IMessageActivity message)
{
    if (message.Value != null) 
    {
         dynamic value = message.Value;
         // assuming your DataJson has a type property like :
         // DataJson = "{ \"Type\": \"Curse\" }" 
         string submitType = value.Type.ToString();

         return Task.FromResult(submitType);
    }
    else 
    {
       // no Adaptive Card value, let's call the base
       return base.GetLuisQueryTextAsync(context, message);
    }
}

上述代码假设您的SubmitAction具有DataJson属性,其值为"{ \"Type\": \"Curse\" }",但当然,您可以更新该属性。

更多资源