microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/PABot/Program.cs
45lines · 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.Core; |
| 7 | using PABot; |
| 8 | using PABot.Bots; |
| 9 | using PABot.Dialogs; |
| 10 | |
| 11 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 12 | |
| 13 | // Register TeamsBotApplication and all dependencies (uses MsalBot and MsalAgent configuration sections) |
| 14 | builder.Services.AddTeamsBotApplications(); |
| 15 | |
| 16 | // Register adapter using the TeamsBotApplication |
| 17 | builder.Services.AddSingleton<IBotFrameworkHttpAdapter>(sp => |
| 18 | { |
| 19 | return new AdapterWithErrorHandler( |
| 20 | sp.GetRequiredService<BotApplication>(), |
| 21 | sp.GetRequiredService<IHttpContextAccessor>(), |
| 22 | sp.GetRequiredService<IConfiguration>(), |
| 23 | sp.GetRequiredService<ILogger<IBotFrameworkHttpAdapter>>(), |
| 24 | sp.GetRequiredService<IStorage>(), |
| 25 | sp.GetRequiredService<ConversationState>()); |
| 26 | }); |
| 27 | |
| 28 | // Register bot state and dialog |
| 29 | builder.Services.AddSingleton<IStorage, MemoryStorage>(); |
| 30 | builder.Services.AddSingleton<UserState>(); |
| 31 | builder.Services.AddSingleton<ConversationState>(); |
| 32 | builder.Services.AddSingleton<MainDialog>(); |
| 33 | |
| 34 | // Register bot (pick between TeamsBot & EchoBot) |
| 35 | // builder.Services.AddTransient<IBot, TeamsBot<MainDialog>>(); |
| 36 | // builder.Services.AddTransient<IBot, EchoBot>(); |
| 37 | builder.Services.AddTransient<IBot, SsoBot>(); |
| 38 | |
| 39 | WebApplication app = builder.Build(); |
| 40 | |
| 41 | // Map endpoint with BotAdapter authorization policy |
| 42 | app.MapPost("/api/messages", (HttpRequest request, HttpResponse response, IBot bot, CancellationToken ct, IBotFrameworkHttpAdapter adapter) => |
| 43 | adapter.ProcessAsync(request, response, bot, ct)).RequireAuthorization("BotAdapter"); |
| 44 | |
| 45 | app.Run(); |
| 46 | |