简体   繁体   English

机器人框架欢迎消息。 如何CardAction?

[英]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模拟器中的结果

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.

相关问题 机器人框架删除卡片动作 - bot framework remove cardaction 当用户向我的机器人发送消息时,他会收到欢迎消息。 但是,当用户对此做出响应时,机器人会再次发送欢迎消息。 我怎样才能解决这个问题? - When user sends message to my bot, he receives Welcome message. But when user respond to that, bot sends Welcome message again. How can I fix this? 如何在 bot 框架 v4 中将对话框设置为欢迎消息 - how to set a dialog as the welcome message in bot framework v4 如何使用Microsoft Bot Framework从我的Bot显示欢迎消息 - How to display a welcome message from my Bot using Microsoft Bot Framework 您如何在团队的 Bot Framework .NET 中处理 CardAction 按钮点击? - How do you handle CardAction Button Clicks in Bot Framework .NET for Teams? Bot Framework-为什么Choice持有CardAction,每个角色的作用是什么? - Bot Framework- Why Choice hold CardAction, and what is the role of each? 带有 type=invoke 的 CardAction 按钮在 Microsoft bot 框架中不起作用 - CardAction button with type=invoke doesn't work in microsoft bot framework 在 v4 Bot Framework Bot(C# + .Net Core Web 应用程序)中显示欢迎消息 - Display Welcome Message in v4 Bot Framework Bot (C# + .Net Core Web Application) 聊天机器人在本地机器人框架模拟器中可以很好地处理欢迎消息,但在 azure 聊天机器人中不起作用 - chatbot is working for welcome message fine in local bot framework Emulator, but not work in azure chatbot Azure Bot服务欢迎消息问题 - Azure Bot Service Welcome Message Issues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM