Check if an input form is filled in a Adaptive Card bot framework c#

纵然是瞬间 提交于 2019-12-13 03:59:13

问题


Can we check whether the input form in an adaptive card is filled or not with a warning message. I am currently using an adaptive card to gather user input in bot application,I have already added isRequired for input validation but it doesnot give any warning message instead when I click on submit it doesnot go to the next method. As soon as the user presses submit I want to make sure that the form is not empty


回答1:


If you have an Adaptive Card like this (notice the ID given to the input):

var card = new AdaptiveCard
{
    Body =
    {
        new AdaptiveTextBlock("Adaptive Card"),
        new AdaptiveTextInput { Id = "text" },
    },
    Actions = {
        new AdaptiveSubmitAction { Title = "Submit" } },
    },
};

You can validate the value sent through the submit action like this:

if (string.IsNullOrEmpty(turnContext.Activity.Text))
{
    dynamic value = turnContext.Activity.Value;
    string text = value["text"];   // The property will be named after your input's ID
    var emailRegex = new Regex(@"^\S+@\S+$");   // This is VERY basic email Regex. You might want something different.

    if (emailRegex.IsMatch(text))
    {
        await turnContext.SendActivityAsync($"I think {text} is a valid email address");
    }
    else
    {
        await turnContext.SendActivityAsync($"I don't think {text} is a valid email address");
    }
}

Validating email with regex can get very complicated and I've taken a simple approach. You can read more about email Regex here: How to validate an email address using a regular expression?



来源:https://stackoverflow.com/questions/55415796/check-if-an-input-form-is-filled-in-a-adaptive-card-bot-framework-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!