【问题标题】:Sending an Adaptive Card on bot startup as the welcome message在机器人启动时发送自适应卡片作为欢迎消息
【发布时间】:2017-11-19 13:26:30
【问题描述】:

我有一些代码让机器人在启动时发送消息(字符串)。

但是,不是像您在下面的代码中看到的那样发送文本。我试图弄清楚在这种情况下您将如何发送自适应卡。我之前从 RootDialog 发送了一张卡片,但不是从 MessageController.cs 发送的。任何方向在这里都会很棒!

else 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

                IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
                if (iConversationUpdated != null)
                {
                    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

                    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                    {
                        // if the bot is added, then
                        if (member.Id == iConversationUpdated.Recipient.Id)
                        {
                            var reply = ((Activity)iConversationUpdated).CreateReply($"WELCOME MESSAGE HERE");
                            await connector.Conversations.ReplyToActivityAsync(reply);
                        }
                    }
                }
            }

谢谢

【问题讨论】:

    标签: c# botframework adaptive-cards


    【解决方案1】:

    使用您提供的代码 sn-p 您应该能够复制并粘贴它来替换它。关于bot框架中卡片的更多信息可以找到in this blog

           else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
                if (iConversationUpdated != null)
                {
                    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
    
                    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                    {
                        // if the bot is added, then
                        if (member.Id == iConversationUpdated.Recipient.Id)
                        {
    
                            Activity replyToConversation = message.CreateReply("Should go to conversation");
                            replyToConversation.Attachments = new List<Attachment>();
    
                            AdaptiveCard card = new AdaptiveCard();
    
                            // Specify speech for the card.
                            card.Speak = "<s>Your  meeting about \"Adaptive Card design session\"<break strength='weak'/> is starting at 12:30pm</s><s>Do you want to snooze <break strength='weak'/> or do you want to send a late notification to the attendees?</s>";
    
                            // Add text to the card.
                            card.Body.Add(new TextBlock()
                            {
                                Text = "Adaptive Card design session",
                                Size = TextSize.Large,
                                Weight = TextWeight.Bolder
                            });
                            // Add text to the card.
                            card.Body.Add(new TextBlock()
                            {
                                Text = "Conf Room 112/3377 (10)"
                            });
                            // Add text to the card.
                            card.Body.Add(new TextBlock()
                            {
                                Text = "12:30 PM - 1:30 PM"
                            });
                            // Add list of choices to the card.
                            card.Body.Add(new ChoiceSet()
                            {
                                Id = "snooze",
                                Style = ChoiceInputStyle.Compact,
                                Choices = new List<Choice>()
                                {
                                    new Choice() { Title = "5 minutes", Value = "5", IsSelected = true },
                                    new Choice() { Title = "15 minutes", Value = "15" },
                                    new Choice() { Title = "30 minutes", Value = "30" }
                                }
                            });
                            // Add buttons to the card.
                            card.Actions.Add(new HttpAction()
                            {
                                Url = "http://foo.com",
                                Title = "Snooze"
                            });
                            card.Actions.Add(new HttpAction()
                            {
                                Url = "http://foo.com",
                                Title = "I'll be late"
                            });
                            card.Actions.Add(new HttpAction()
                            {
                                Url = "http://foo.com",
                                Title = "Dismiss"
                            });
                            // Create the attachment.
                            Attachment attachment = new Attachment()
                            {
                                ContentType = AdaptiveCard.ContentType,
                                Content = card
                            };
                            replyToConversation.Attachments.Add(attachment);
    
                            var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
                        }
                    }
                }
            }
    

    【讨论】:

    • 在机器人模拟器中测试过,效果很好,谢谢
    • 仅供参考,如果有人想知道,您需要安装 Microsoft.AdaptiveCards nuget 包。
    猜你喜欢
    • 2021-02-22
    • 2022-01-23
    • 2020-05-18
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多