Skip to main content

Sending Activities

BotBuilder's pattern for sending activities via its TurnContext is similar to that in Teams SDK, but one key difference is that sending adaptive cards doesn't require constructing the entire activity yourself.

-   using Microsoft.Bot.Builder;
- using Microsoft.Bot.Schema;
+ using Microsoft.Teams.Apps;
+ using Microsoft.Teams.Plugins.AspNetCore.Extensions;
+ using Microsoft.Teams.Api.Activities;

- public class MyActivityHandler : ActivityHandler
- {
- protected override async Task OnMessageActivityAsync(
- ITurnContext<IMessageActivity> turnContext,
- CancellationToken cancellationToken)
- {
- await turnContext.SendActivityAsync(
- Activity.CreateTypingActivity(),
- cancellationToken: cancellationToken);
- }
- }
+ var teams = app.UseTeams();
+ teams.OnMessage(async (context) =>
+ {
+ await context.Send(new Activity(type:"typing"));
+ });

Strings​

-   using Microsoft.Bot.Builder;
- using Microsoft.Bot.Schema;
+ using Microsoft.Teams.Apps;
+ using Microsoft.Teams.Plugins.AspNetCore.Extensions;

- public class MyActivityHandler : ActivityHandler
- {
- protected override async Task OnMessageActivityAsync(
- ITurnContext<IMessageActivity> turnContext,
- CancellationToken cancellationToken)
- {
- await turnContext.SendActivityAsync("hello world", cancellationToken: cancellationToken);
- }
- }
+ var teams = app.UseTeams();
+ teams.OnMessage(async (context) =>
+ {
+ await context.Send("hello world");
+ });

Adaptive Cards​

-   using Microsoft.Bot.Builder;
- using Microsoft.Bot.Schema;
+ using Microsoft.Teams.Apps;
+ using Microsoft.Teams.Cards;
+ using Microsoft.Teams.Plugins.AspNetCore.Extensions;

- public class MyActivityHandler : ActivityHandler
- {
- protected override async Task OnMessageActivityAsync(
- ITurnContext<IMessageActivity> turnContext,
- CancellationToken cancellationToken)
- {
- var card = new
- {
- type = "AdaptiveCard",
- version = "1.0",
- body = new[]
- {
- new { type = "TextBlock", text = "hello world" }
- }
- };
- var attachment = new Attachment
- {
- ContentType = "application/vnd.microsoft.card.adaptive",
- Content = card
- };
- var activity = MessageFactory.Attachment(attachment);
- await turnContext.SendActivityAsync(activity, cancellationToken: cancellationToken);
- }
- }
+ var teams = app.UseTeams();
+ teams.OnMessage(async (context) =>
+ {
+ await context.Send(new AdaptiveCard(new TextBlock("hello world")));
+ });

Attachments​

-   using Microsoft.Bot.Builder;
- using Microsoft.Bot.Schema;
+ using Microsoft.Teams.Apps;
+ using Microsoft.Teams.Api;
+ using Microsoft.Teams.Plugins.AspNetCore.Extensions;

- public class MyActivityHandler : ActivityHandler
- {
- protected override async Task OnMessageActivityAsync(
- ITurnContext<IMessageActivity> turnContext,
- CancellationToken cancellationToken)
- {
- var activity = MessageFactory.Attachment(new Attachment { /* ... */ });
- await turnContext.SendActivityAsync(activity, cancellationToken: cancellationToken);
- }
- }
+ var teams = app.UseTeams();
+ teams.OnMessage(async (context) =>
+ {
+ var activity = new MessageActivity();
+ activity.AddAttachment(new Attachment { /* ... */ });
+ await context.SendAsync(activity);
+ });