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:
Quote.Agent/
|ββ appPackage/ # Teams app package files
βββ Program.cs # Main application startup code
βββ MainController.cs # Main activity handling code
- appPackage/: Contains the Teams app package files, including the
manifest.json
file and icons. This is required for sideloading the app into Teams for testing. The app manifest defines the app's metadata, capabilities, and permissions.
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.
using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions;
using Microsoft.Teams.Plugins.AspNetCore.Extensions;
using Quote.Agent;
var builder = WebApplication.CreateBuilder(args);
builder.AddTeams();
builder.AddTeamsDevTools();
builder.Services.AddTransient<MainController>();
var app = builder.Build();
app.UseTeams();
app.Run();
The app configuration includes a variety of options that allow you to customize its behavior, including controlling the underlying server, authentication, and other settings.
Pluginsβ
Plugins are a core part of the Teams AI v2 SDK. They allow you to hook into various lifecycles of the application. The lifecycles include server events (start, stop, initialize, etc.), and also Teams Activity events (onActivity, onActivitySent, etc.). In fact, the DevTools application you already have running is a plugin too. It allows you to inspect and debug your application in real-time.
DevTools is a plugin that should only be used in development mode. It should not be used in production applications since it offers no authentication and allows your application to be accessed by anyone.
Be sure to remove the DevTools plugin from your production code.
Message Handlingβ
Teams applications respond to various types of activities. The most basic is handling messages:
- Controller
- Minimal
[TeamsController("main")]
public class MainController
{
[Message]
public async Task OnMessage([Context] MessageActivity activity, [Context] IContext.Client client)
{
await client.Typing();
await client.Send($"you said \"{activity.Text}\"");
}
}
app.OnMessage(async context =>
{
await context.Typing();
await context.Send($"you said \"{context.activity.Text}\"");
});
This code:
- Listens for all incoming messages using
[Message]
attribute. - Sends a typing indicator, which renders as an animated ellipsis (β¦) in the chat.
- Responds by echoing back the received message.
Each activity type has both an attribute and a functional method for type safety/simplicity of routing logic!
Application Lifecycleβ
Your application starts when you run:
var app = builder.Build();
app.UseTeams();
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 will learn about Microsoft 365 Agents Toolkit and other important tools that help you with deployment and testing your application.
After that, you can:
- Add more activity handlers for different types of interactions. See Listening to Activities for more details.
- Integrate with external services using the API Client.
- Add interactive cards and dialogs.
- Implement AI.
Continue on to the next page to learn about these advanced features.