You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#的Message Controller的Conversation Update中添加Hero Card

How to Send a Hero Card on Conversation Start in C# Bot Framework

Got it, let's walk through exactly how to make your bot pop up a Hero Card the second a user opens the chat. We'll implement this in the Conversation Update handler using C#—here's everything you need:

Step 1: Use the ActivityHandler's Conversation Update Method

If you're using the modern Bot Framework v4+, your bot class should inherit from ActivityHandler. This class has a built-in method OnConversationUpdateActivityAsync that triggers when the conversation state changes (like a user joining). We'll override this method to send our Hero Card.

Step 2: Filter for User Join Events

The ConversationUpdate event fires for multiple scenarios (like the bot itself joining the conversation), so we need to check if a user is the one who just joined. We do this by comparing the new member's ID to the bot's ID.

Step 3: Build and Send the Hero Card

Once we confirm a user has joined, we'll construct the Hero Card, convert it to an attachment, and send it as a message.

Full Code Example

Here's how your bot class will look (this replaces the default EchoBot-style class):

using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class WelcomeBot : ActivityHandler
{
    protected override async Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
    {
        // Iterate through all new members added to the conversation
        foreach (var member in turnContext.Activity.MembersAdded)
        {
            // Skip the bot itself to avoid sending a card to ourselves
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                // Create your custom Hero Card
                var welcomeHeroCard = new HeroCard
                {
                    Title = "Welcome to My Awesome Bot!",
                    Subtitle = "Choose an option to get started:",
                    // Add an optional image (replace with your own URL)
                    Images = new List<CardImage>
                    {
                        new CardImage("https://your-image-url-here.com/welcome-banner.png")
                    },
                    // Add interactive buttons
                    Buttons = new List<CardAction>
                    {
                        new CardAction(ActionTypes.MessageBack, "Learn More", value: "learn_more"),
                        new CardAction(ActionTypes.OpenUrl, "Visit Our Site", value: "https://your-website.com"),
                        new CardAction(ActionTypes.PostBack, "Contact Support", value: "contact_support")
                    }
                };

                // Convert the Hero Card to an attachment the bot can send
                var cardAttachment = welcomeHeroCard.ToAttachment();

                // Create a message activity containing the card
                var welcomeMessage = MessageFactory.Attachment(cardAttachment);

                // Send the message to the user
                await turnContext.SendActivityAsync(welcomeMessage, cancellationToken);
            }
        }

        // Call the base method to maintain default behavior
        await base.OnConversationUpdateActivityAsync(turnContext, cancellationToken);
    }
}

Key Details to Note

  • NuGet Packages: Make sure you have these installed in your project:
    • Microsoft.Bot.Builder
    • Microsoft.Bot.Builder.Integration.AspNet.Core
    • Microsoft.Bot.Schema
  • Customization: Tweak the Hero Card's Title, Subtitle, Images, and Buttons to match your bot's purpose. The ActionTypes enum has options like MessageBack, OpenUrl, and PostBack for different button behaviors.
  • Legacy Bot Framework v3: If you're still using v3, the core logic is similar—you'll handle the ConversationUpdate activity in your MessagesController, check for new users, and construct the Hero Card using HeroCard and Attachment classes from Microsoft.Bot.Connector.

内容的提问来源于stack exchange,提问作者Shivain Mattoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:12:04