我已使用以下代码创建了自适应卡。但是,单击“提交”按钮后,机器人会引发错误。
var card = new AdaptiveCard();
card.Body.Add(new AdaptiveTextBlock() { Text = "May I know who I am chatting with?", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder, HorizontalAlignment = AdaptiveHorizontalAlignment.Left });
card.Body.Add(new AdaptiveImage() { UrlString = "https://media-exp1.licdn.com/dms/image/C510BAQHDcYycRMpKWQ/company-logo_200_200/0?e=2159024400&v=beta&t=MRb3ZSGGATtJvp6s6XQt3r_SqyhORkaleuB3bUWVe6g", Size = AdaptiveImageSize.Medium, HorizontalAlignment= AdaptiveHorizontalAlignment.Right }) ;
card.Body.Add(new AdaptiveTextBlock() { Text = "Name", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Text, Id = "Name", Placeholder = "First Last"});
card.Body.Add(new AdaptiveTextBlock() { Text = "Mobile number:", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Tel, Id = "Mobile number", Placeholder = "xxx.xxx.xxxx" });
card.Body.Add(new AdaptiveTextBlock() { Text = "Email:", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Email, Id = "Email", Placeholder = "[email protected]" });
card.Body.Add(new AdaptiveTextBlock() { Text = "Your Company Name:", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder});
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Text, Id = "Your Company Name" });
card.Body.Add(new AdaptiveTextBlock() { Text = "Message:", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder});
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Text, Id = "Message", IsMultiline = true });
card.Actions.Add(new AdaptiveSubmitAction() {Title = "Submit", Data = { } });
return new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
这是我创建自适应卡的方式-
public static Attachment PersonalInfo()
{
var card = new AdaptiveCard("1.0");
card.Body.Add(new AdaptiveTextBlock() { Text = "Sure. While I fetch that, may I know you a little?", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder, HorizontalAlignment = AdaptiveHorizontalAlignment.Left });
//card.Body.Add(new AdaptiveTextBlock() { Text = "If yes, please fill in the form below and click SUBMIT.", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder, HorizontalAlignment = AdaptiveHorizontalAlignment.Left });
//card.Body.Add(new AdaptiveTextBlock() { Text = "If no, please click CANCEL at the end", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder, HorizontalAlignment = AdaptiveHorizontalAlignment.Left });
//card.Body.Add(new AdaptiveChoiceSetInput() { Id = "Choose", Style = AdaptiveChoiceInputStyle.Expanded, Choices = new List<AdaptiveChoice>() { new AdaptiveChoice() { Title = "Yes", Value = "Yes" }, new AdaptiveChoice() { Title = "No", Value = "No" } } });
//card.Body.Add(new AdaptiveImage() { UrlString = "https://media-exp1.licdn.com/dms/image/C510BAQHDcYycRMpKWQ/company-logo_200_200/0?e=2159024400&v=beta&t=MRb3ZSGGATtJvp6s6XQt3r_SqyhORkaleuB3bUWVe6g", Size = AdaptiveImageSize.Medium, HorizontalAlignment= AdaptiveHorizontalAlignment.Right }) ;
//card.Body.Add(new AdaptiveTextBlock() { Text = "Name", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Text, Id = "Name", Placeholder = "Name", IsMultiline = true, IsRequired = true });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Tel, Id = "Mobile number", Placeholder = "Mobile number", IsMultiline = true });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Email, Id = "Email", Placeholder = "Email ID", IsMultiline = true, });
card.Body.Add(new AdaptiveTextInput() { Style = AdaptiveTextInputStyle.Text, Id = "Your Company Name", Placeholder = "Your Company Name", IsMultiline = true });
card.Actions.Add(new AdaptiveSubmitAction() { Title = "SUBMIT", Style = "positive", Id = "submit", Data = "submit", DataJson = "{\"submit\":\"submit\"}"});
card.Actions.Add(new AdaptiveSubmitAction() { Title = "CANCEL", Style = "negative", Id = "cancel", Data = "cancel", DataJson = "{\"cancel\":\"cancel\"}" });
return new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
}
然后在OnTurnAsyc活动的开头-
if (string.IsNullOrEmpty(turnContext.Activity.Text))
{
dynamic value = turnContext.Activity.Value;
if (value != null)
{
if (value["submit"] == "submit")
{
//bool flag = true;
isFormSubmit = true;
string text = value["submit"]; // The property will be named after your text input's ID
text = string.IsNullOrEmpty(text) ? "." : text; // In case the text input is empty
turnContext.Activity.Text = text;
}
else if (value["cancel"] == "cancel")
{
isFormCancel = true;
string text = value["cancel"]; // The property will be named after your text input's ID
text = string.IsNullOrEmpty(text) ? "." : text; // In case the text input is empty
turnContext.Activity.Text = text;
}
}
}