Skip to main content

Code Basics

After following the guidance in the quickstart to create your first Teams application, let's review its structure and key components. This knowledge can help you build more complex applications as you progress.

Project Structure​

When you create a new Teams application, it generates a directory with this basic structure:

QuoteAgent/
├── Program.cs # Main application startup code
├── appsettings.json # Configuration (logging, auth, etc.)
  • Program.cs: The entry point. A standard ASP.NET Core app that registers the Teams application into DI and wires it into the request pipeline.
  • appsettings.json: Standard ASP.NET Core configuration — logging levels, bot credentials.

Core Components​

Let's break down the simple application from the quickstart into its core components.

The App Class​

The heart of an application is the App class. This class handles all incoming activities and manages the application's lifecycle. It also acts as a way to host your application service.

Program.cs
using Microsoft.Teams.Apps;

WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
builder.Services.AddTeamsBotApplication();
WebApplication app = builder.Build();

TeamsBotApplication teams = app.UseTeamsBotApplication();

teams.OnMessage(async (context, cancellationToken) =>
{
await context.TypingAsync(cancellationToken);
await context.SendAsync($"you said '{context.Activity.Text}'", cancellationToken);
});

app.Run();

AddTeamsBotApplication() registers everything the SDK needs into dependency injection. UseTeamsBotApplication() wires it into the ASP.NET Core pipeline and maps the POST /api/messages endpoint. From there, you register handlers for the activity types you care about.

Plugins​

Removed in SDK 2.1

Plugins have been removed in SDK 2.1. Because your app is a standard ASP.NET Core app, extend it with regular middleware and dependency injection instead — for per-turn logic, implement ITurnMiddleware and register it with teams.UseMiddleware(...).

Message Handling​

Teams applications respond to various types of activities. The most basic is handling messages:

Program.cs
teams.OnMessage(async (context, cancellationToken) =>
{
await context.TypingAsync(cancellationToken);
await context.SendAsync($"you said \"{context.Activity.Text}\"", cancellationToken);
});

You can also match specific messages with a regular expression — teams.OnMessage("(?i)^help$", ...) only fires for a case-insensitive help.

This code:

  1. Listens for all incoming messages using the OnMessage handler.
  2. Sends a typing indicator, which renders as an animated ellipsis (…) in the chat.
  3. Responds by echoing back the received message.
info

Each activity type has a dedicated handler method (OnMessage, OnMessageReaction, OnMembersAdded, and so on) for type-safe, readable routing.

Application Lifecycle​

Your application starts when you run:

WebApplication app = builder.Build();
app.UseTeamsBotApplication();
app.Run();

This code initializes your application server and, when configured for Teams, also authenticates it to be ready for sending and receiving messages.

Next Steps​

Now that you understand the basic structure of your Teams application, you're ready to run it in Teams. You'll use the Teams Developer CLI to register your bot and sideload it into Teams.

After that, you can:

Continue on to the next page to learn about these advanced features.

Other Resources​