如何在C#的Message Controller的Conversation Update中添加Hero Card
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.BuilderMicrosoft.Bot.Builder.Integration.AspNet.CoreMicrosoft.Bot.Schema
- Customization: Tweak the Hero Card's
Title,Subtitle,Images, andButtonsto match your bot's purpose. TheActionTypesenum has options likeMessageBack,OpenUrl, andPostBackfor different button behaviors. - Legacy Bot Framework v3: If you're still using v3, the core logic is similar—you'll handle the
ConversationUpdateactivity in yourMessagesController, check for new users, and construct the Hero Card usingHeroCardandAttachmentclasses fromMicrosoft.Bot.Connector.
内容的提问来源于stack exchange,提问作者Shivain Mattoo

