[英]bot framework welcome message. how to CardAction?
在使用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;
}
}
}
有多种方法可以做到这一点。 一种非常方便的方法是使用自适应卡设计器 。
只需设计您想要的消息,并将生成的json
存储为项目中的文件,例如在Cards/Welcome.json
。
当然,您可以使用代码构建自适应卡,但这样一来,您可以更轻松地学习自适应卡的概念。
从那里你可以在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;
}
请务必注意自适应卡支持您的频道。 如果您的频道不支持自适应卡,您可以使用英雄卡 ,然后再次回退到纯文本。
个人注意事项:我发现有关各种渠道的自适应卡的状态和支持的文档大部分已过时,有时也是错误的。 如果您发现自适应卡不能在特定通道上进行渲染,则值得检查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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.