microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/samples/PABot/Program.cs
62lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Bot.Builder; |
| 5 | using Microsoft.Bot.Builder.Integration.AspNet.Core; |
| 6 | using Microsoft.Teams.Bot.Apps; |
| 7 | using PABot; |
| 8 | using PABot.Bots; |
| 9 | using PABot.Dialogs; |
| 10 | |
| 11 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 12 | |
| 13 | // Register all the keyed services (ConversationClient, UserTokenClient, TeamsApiClient, TeamsBotApplication) |
| 14 | builder.Services.AddTeamsBotApplications(); |
| 15 | |
| 16 | // Register keyed adapters using the keyed TeamsBotApplication |
| 17 | builder.Services.AddKeyedSingleton<IBotFrameworkHttpAdapter>("AdapterOne", (sp, keyName) => |
| 18 | { |
| 19 | return new AdapterWithErrorHandler( |
| 20 | sp.GetRequiredKeyedService<TeamsBotApplication>("AdapterOne"), |
| 21 | sp.GetRequiredService<IHttpContextAccessor>(), |
| 22 | sp.GetRequiredService<IConfiguration>(), |
| 23 | sp.GetRequiredService<ILogger<IBotFrameworkHttpAdapter>>(), |
| 24 | sp.GetRequiredService<IStorage>(), |
| 25 | sp.GetRequiredService<ConversationState>()); |
| 26 | }); |
| 27 | |
| 28 | builder.Services.AddKeyedSingleton<IBotFrameworkHttpAdapter>("AdapterTwo", (sp, keyName) => |
| 29 | { |
| 30 | return new AdapterWithErrorHandler( |
| 31 | sp.GetRequiredKeyedService<TeamsBotApplication>("AdapterTwo"), |
| 32 | sp.GetRequiredService<IHttpContextAccessor>(), |
| 33 | sp.GetRequiredService<IConfiguration>(), |
| 34 | sp.GetRequiredService<ILogger<IBotFrameworkHttpAdapter>>(), |
| 35 | sp.GetRequiredService<IStorage>(), |
| 36 | sp.GetRequiredService<ConversationState>()); |
| 37 | }); |
| 38 | |
| 39 | // Register bot state and dialog |
| 40 | builder.Services.AddSingleton<IStorage, MemoryStorage>(); |
| 41 | builder.Services.AddSingleton<UserState>(); |
| 42 | builder.Services.AddSingleton<ConversationState>(); |
| 43 | builder.Services.AddSingleton<MainDialog>(); |
| 44 | |
| 45 | // Register bots |
| 46 | builder.Services.AddKeyedTransient<IBot, TeamsBot<MainDialog>>("TeamsBot"); |
| 47 | builder.Services.AddKeyedTransient<IBot, EchoBot>("EchoBot"); |
| 48 | |
| 49 | WebApplication app = builder.Build(); |
| 50 | |
| 51 | // Get the keyed adapters |
| 52 | IBotFrameworkHttpAdapter adapterOne = app.Services.GetRequiredKeyedService<IBotFrameworkHttpAdapter>("AdapterOne"); |
| 53 | IBotFrameworkHttpAdapter adapterTwo = app.Services.GetRequiredKeyedService<IBotFrameworkHttpAdapter>("AdapterTwo"); |
| 54 | |
| 55 | // Map endpoints with their respective adapters and authorization policies |
| 56 | app.MapPost("/api/messages", (HttpRequest request, HttpResponse response, [FromKeyedServices("TeamsBot")] IBot bot, CancellationToken ct) => |
| 57 | adapterOne.ProcessAsync(request, response, bot, ct)).RequireAuthorization("AdapterOne"); |
| 58 | |
| 59 | app.MapPost("/api/v2/messages", (HttpRequest request, HttpResponse response, [FromKeyedServices("EchoBot")] IBot bot, CancellationToken ct) => |
| 60 | adapterTwo.ProcessAsync(request, response, bot, ct)).RequireAuthorization("AdapterTwo"); |
| 61 | |
| 62 | app.Run(); |
| 63 | |