BotApplication Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a bot application that receives and processes activities from a messaging channel.
public class BotApplication
type BotApplication = class
Public Class BotApplication
- Inheritance
-
BotApplication
- Derived
Remarks
BotApplication is the central entry point for handling incoming bot activities. Register it with the host using AddBotApplication(IServiceCollection, String) and map it to an endpoint with UseBotApplication(IEndpointRouteBuilder, String).
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddBotApplication();
var app = builder.Build();
var bot = app.UseBotApplication();
bot.OnActivity = async (activity, ct) =>
{
await bot.SendActivityAsync(
activity.Conversation.Id!,
CoreActivityInput.CreateBuilder()
.WithType(ActivityType.Message)
.WithProperty("text", "Hello!")
.Build(),
activity.ServiceUrl!,
ct);
};
app.Run();
public class MyBot : BotApplication
{
public MyBot(ConversationClient conversationClient, UserTokenClient userTokenClient, ILogger<MyBot> logger)
: base(conversationClient, userTokenClient, logger)
{
OnActivity = HandleActivityAsync;
}
private async Task HandleActivityAsync(CoreActivity activity, CancellationToken ct)
{
if (activity.Type == ActivityType.Message)
{
// Echo the user's message back
await SendActivityAsync(
activity.Conversation.Id!,
CoreActivityInput.CreateBuilder()
.WithType(ActivityType.Message)
.WithProperty("text", $"You said: {activity.Properties["text"]}")
.Build(),
activity.ServiceUrl!,
ct);
}
}
}
Constructors
| Name | Description |
|---|---|
| BotApplication() |
Creates a default instance, primarily for testing purposes. The ConversationClient and UserTokenClient properties will not be initialized; accessing them will throw InvalidOperationException. |
| BotApplication(ConversationClient, UserTokenClient, ILogger<BotApplication>, BotApplicationOptions) |
Initializes a new instance of the BotApplication class with the specified conversation client, user token client, logger, and optional application options. |
Properties
| Name | Description |
|---|---|
| AppId |
Gets the application (client) ID configured for this bot (for example, the Azure AD app registration client ID). |
| ConversationClient |
Gets the ConversationClient used to send, update, and delete activities in conversations. |
| OnActivity |
Gets or sets the delegate that is invoked to handle each incoming activity. |
| UserTokenClient |
Gets the UserTokenClient used to manage OAuth user tokens (sign-in, sign-out, token exchange). |
| Version |
Gets the version of the Microsoft.Teams.Core SDK (for example, |
Methods
| Name | Description |
|---|---|
| ProcessAsync(HttpContext, CancellationToken) |
Processes an incoming HTTP request containing a bot activity. |
| SendActivityAsync(String, CoreActivityInput, Uri, AgenticIdentity, Boolean, CancellationToken) |
Sends the specified activity to the conversation asynchronously. |
| UseMiddleware(ITurnMiddleware) |
Adds the specified turn middleware to the middleware pipeline. |