在使用ms azure botFrameWork v3。 我想显示欢迎消息CardAction。 我想使用CardAction并不是一个简单的消息。 我不知道如何编码。
public class MessagesController : ApiController{
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity){
if (activity != null && activity.GetActivityType() == ActivityTypes.Message){
await Conversation.SendAsync(activity, () => new EchoDialog());
}else{
HandleSystemMessage(activity);
}
return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}
private Activity HandleSystemMessage(Activity message){
if (message.Type == ActivityTypes.DeleteUserData){
}
else if (message.Type == ActivityTypes.ConversationUpdate){
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id)){
////////////welcom
var reply = message.CreateReply("hello~");
ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
connector.Conversations.ReplyToActivityAsync(reply);
}
return null;
}
}
}
答案 0 :(得分:0)
有多种方法可以做到这一点。一种非常方便的方法是使用adaptive cards designer。
只需设计所需的消息,然后将结果json
存储为项目中的文件,例如在Cards/Welcome.json
下。
当然,您可以使用code来构建自适应卡,但是这样一来,就可以更轻松地学习和学习自适应卡的概念。
从那里,您可以在MessageController.cs
中执行以下操作:
private async Task<Activity> HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
if (message.MembersAdded.Any())
{
var reply = message.CreateReply();
foreach (var member in message.MembersAdded)
{
// the bot is always added as a user of the conversation, since we don't
// want to display the adaptive card twice ignore the conversation update triggered by the bot
if (member.Name.ToLower() != "bot")
{
// Read the welcome card from file system and send it as attachment to the user
string json = File.ReadAllText(HttpContext.Current.Request.MapPath("~\\Cards\\Welcome.json"));
AdaptiveCard card = JsonConvert.DeserializeObject<AdaptiveCard>(json);
reply.Attachments.Add(new Attachment
{
ContentType = AdaptiveCard.ContentType,
Content = card
});
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var connectorClient = scope.Resolve<IConnectorClient>();
await connectorClient.Conversations.ReplyToActivityAsync(reply);
}
}
}
}
}
return null;
}
始终注意频道的adaptive cards support。如果您的频道不支持自适应卡,则可以选择hero cards,然后再次回退到纯文本。
个人笔记:我发现有关各种渠道的自适应卡的状态和支持的文档大多已过时,有时甚至是错误的。如果您发现自己的自适应卡未在特定通道上呈现,则值得检查GitHub或StackOverflow。
Bot仿真器中的结果
欢迎使用自适应卡片示例
{
"type": "AdaptiveCard",
"body": [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"size": "Large",
"weight": "Bolder",
"text": "Welcome"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"size": "Small",
"weight": "Bolder",
"text": "This is a bot",
"wrap": true
}
],
"width": "stretch"
}
]
}
]
},
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "I'm helping you...",
"wrap": true
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Quick Quote",
"data": {
"action": "quickquote"
}
},
{
"type": "Action.Submit",
"title": "Some Action",
"data": {
"action": "someAction"
}
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}