microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/MigrationBot/NewSdkBot.cs
53lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps; |
| 5 | using Microsoft.Teams.Bot.Apps.Handlers; |
| 6 | using Microsoft.Teams.Bot.Core; |
| 7 | using Microsoft.Teams.Bot.Core.Hosting; |
| 8 | |
| 9 | namespace MigrationBot; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Teams SDK–style echo bot — the migration target. |
| 13 | /// |
| 14 | /// This is a dedicated <see cref="TeamsBotApplication"/> subclass registered |
| 15 | /// independently from the CompatAdapter's own <see cref="TeamsBotApplication"/>. |
| 16 | /// Each instance owns its own isolated router and OnActivity delegate, so the two |
| 17 | /// instances never interfere with each other's routing. |
| 18 | /// |
| 19 | /// As handlers are moved here from <see cref="LegacyEchoBot"/>, remove them from |
| 20 | /// that class. When <see cref="LegacyEchoBot"/> is empty, drop the CompatAdapter |
| 21 | /// and the <c>Migration:UseTeamsSdk</c> flag and register this class directly as a |
| 22 | /// plain <see cref="TeamsBotApplication"/>. |
| 23 | /// </summary> |
| 24 | public class NewSdkBot : TeamsBotApplication |
| 25 | { |
| 26 | public NewSdkBot( |
| 27 | ConversationClient conversationClient, |
| 28 | UserTokenClient userTokenClient, |
| 29 | TeamsApiClient teamsApiClient, |
| 30 | IHttpContextAccessor httpContextAccessor, |
| 31 | ILogger<TeamsBotApplication> logger, |
| 32 | BotApplicationOptions? options = null) |
| 33 | : base(conversationClient, userTokenClient, teamsApiClient, httpContextAccessor, logger, options) |
| 34 | { |
| 35 | // ── Handler: message activity ───────────────────────────────────────── |
| 36 | // Equivalent in LegacyEchoBot: |
| 37 | // OnMessageActivityAsync → MessageFactory.Text($"Echo (Bot Framework): {text}") |
| 38 | this.OnMessage(async (context, cancellationToken) => |
| 39 | { |
| 40 | await context.SendActivityAsync( |
| 41 | $"Echo (Teams SDK): {context.Activity.Text}", cancellationToken); |
| 42 | }); |
| 43 | |
| 44 | // ── Handler: members added ──────────────────────────────────────────── |
| 45 | // Equivalent in LegacyEchoBot: |
| 46 | // OnMembersAddedAsync → MessageFactory.Text("Welcome! ... CompatAdapter.") |
| 47 | this.OnMembersAdded(async (context, cancellationToken) => |
| 48 | { |
| 49 | await context.SendActivityAsync( |
| 50 | "Welcome! This bot is powered by the Teams SDK.", cancellationToken); |
| 51 | }); |
| 52 | } |
| 53 | } |
| 54 | |