[英]bot framework welcome message. how to CardAction?
in use ms azure botFrameWork v3. 在使用ms azure botFrameWork v3。 I want to display a welcome message CardAction. 我想显示欢迎信息CardAction。 Not a simple message I want to use CardAction. 不是一个简单的消息我想使用CardAction。 I do not know how to code it. 我不知道如何编码。
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;
}
}
}
There are multiple ways of doing this. 有多种方法可以做到这一点。 One way that is very convenient is by using adaptive cards designer . 一种非常方便的方法是使用自适应卡设计器 。
Simply design the message you want and store the resulting json
as a file in your project, eg under Cards/Welcome.json
. 只需设计您想要的消息,并将生成的json
存储为项目中的文件,例如在Cards/Welcome.json
。
Of course, you can construct the adaptive card using code , but this way it's easier to play around and learning the concepts of adaptive cards along the way. 当然,您可以使用代码构建自适应卡,但这样一来,您可以更轻松地学习自适应卡的概念。
From there you can do something like this in your MessageController.cs
: 从那里你可以在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;
}
Always note the adaptive cards support for your channel. 请务必注意自适应卡支持您的频道。 In case your channel is not supporting adaptive cards, you can go for hero cards and then fallback to plain text again. 如果您的频道不支持自适应卡,您可以使用英雄卡 ,然后再次回退到纯文本。
Personal note: I found documentation about the status and the support of adaptive cards for the various channels being mostly outdated and sometimes wrong. 个人注意事项:我发现有关各种渠道的自适应卡的状态和支持的文档大部分已过时,有时也是错误的。 It's worth checking GitHub or StackOverflow if you find your adaptive card not rendering as you want it on that particular channel. 如果您发现自适应卡不能在特定通道上进行渲染,则值得检查GitHub或StackOverflow。
Result in Bot emulator Bot模拟器中的结果
Welcome adaptive card sample 欢迎自适应卡样品
{
"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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.