c# – 自适应卡片提交动作
作者:互联网
我已经生成了一个使用JSON格式的自适应卡,其中两个按钮提交和取消,它们分别返回“messageBack”消息作为提交和取消.
我使用C#来访问回复,但我无法弄清楚如何从自适应卡访问回复.
我的json是
{
"type": "AdaptiveCard",
"selectAction": {
"type": "Action.Submit"
},
"body": [
{
"type": "TextBlock",
"horizontalAlignment": "Center",
"size": "Large",
"weight": "Bolder",
"color": "Accent",
"text": "Meeting Composer Create"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"horizontalAlignment": "Left",
"spacing": "Medium",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"text": "Attendees:"
}
],
"width": "stretch"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"id": "attendeeVal",
"text": "a"
}
],
"width": "stretch"
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"text": "Subject:"
}
],
"width": "stretch"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"id": "subVal",
"text": "meeting"
}
],
"width": "stretch"
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"text": "Date:"
}
],
"width": "stretch"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"id": "dateVal",
"text": "17/11/2018 10.30 AM"
}
],
"width": "stretch"
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"text": "Document Name:"
}
],
"width": "stretch"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"id": "docVal",
"text": "Document1"
}
],
"width": "stretch"
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit",
"data": {
"msteams": {
"type": "messageBack",
"displayText": "I clicked this button",
"text": "text to bots",
"value": "Submit"
}
}
},
{
"type": "Action.Submit",
"title": "Cancel",
"data": {
"msteams": {
"type": "messageBack",
"displayText": "I clicked this button",
"text": "text to bots",
"value": "Cancel"
}
}
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
我的C#是
var response = getCard(stepContext, "Aditya Rao, Vishal Subramaniam" , "Scrum Meeting" , "17/11/1028, 10:30AM" , "Scrum Sprint.pptx");
await stepContext.Context.SendActivityAsync(response).ConfigureAwait(false);
我的附加功能是支持
private static Attachment CreateAdaptiveCardAttachment(string filePath, string names, string subj , string datee, string docs)
{
var adaptiveCardJson = File.ReadAllText(filePath);
dynamic obj = JsonConvert.DeserializeObject(adaptiveCardJson);
obj["body"][1]["columns"][1]["items"][0]["text"] = names;
obj["body"][2]["columns"][1]["items"][0]["text"] = subj;
obj["body"][3]["columns"][1]["items"][0]["text"] = datee;
obj["body"][4]["columns"][1]["items"][0]["text"] = docs;
var adaptiveCardAttachment = new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = obj,
};
return adaptiveCardAttachment;
}
// Create an attachment message response.
private Activity CreateResponse(Activity activity, Attachment attachment)
{
var response = activity.CreateReply();
response.Attachments = new List<Attachment>() { attachment };
return response;
}
private Activity getCard(WaterfallStepContext stepContext, string names, string subj , string datee, string docs)
{
var jsonFilePath = @".\Dialogs\CardTemplates\MeetingComposerCreate.json";
var activity = stepContext.Context.Activity;
var adCard = CreateAdaptiveCardAttachment(jsonFilePath,names,subj,datee,docs);
var response = CreateResponse(activity, adCard);
return response;
}
单击提交或取消后,如何访问值响应?
此外,如果有人可以帮我找到如何从input.choice获得回复,以获取所选卡的复选框数据.
解决方法:
How do I access the values response once submit or cancel is clicked?
我们可以通过Activity.Value属性获取用户从自适应卡提交的值.
if(turnContext.Activity.Value!= null)
{
reply.Text = $"submit data: {turnContext.Activity.Value}";
}
测试结果:
模拟器中的输出:
Also if someone can help me find how to get reply from input.choice to get the checkbox data of the selected cards.
要显示用户选择和获取所选选项的选项,可以参考以下代码段.
在json文件中:
{
"type": "Input.ChoiceSet",
"id": "optionSelection",
"isMultiSelect": true,
"style": "compact",
"choices": [
{
"title": "option1",
"value": "option1"
},
{
"title": "option2",
"value": "option2"
},
{
"title": "option3",
"value": "option3"
}
]
}
使用相同的代码来获取用户的选择:
reply.Text = $"submit data: {turnContext.Activity.Value}";
测试结果:
标签:c,adaptive-cards 来源: https://codeday.me/bug/20190710/1424678.html