microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/CompatBot/Program.cs
60lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | |
| 5 | using CompatBot; |
| 6 | using Microsoft.Bot.Builder; |
| 7 | using Microsoft.Bot.Builder.Integration.AspNet.Core; |
| 8 | using Microsoft.Bot.Schema; |
| 9 | using Microsoft.Teams.Apps.BotBuilder; |
| 10 | using Microsoft.Teams.Core; |
| 11 | |
| 12 | // using Microsoft.Bot.Connector.Authentication; |
| 13 | |
| 14 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 15 | builder.AddTeamsBotFrameworkHttpAdapter(); |
| 16 | |
| 17 | //builder.Services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>(); |
| 18 | //builder.Services.AddSingleton<IBotFrameworkHttpAdapter>(provider => |
| 19 | // new CloudAdapter( |
| 20 | // provider.GetRequiredService<BotFrameworkAuthentication>(), |
| 21 | // provider.GetRequiredService<ILogger<CloudAdapter>>())); |
| 22 | |
| 23 | |
| 24 | MemoryStorage storage = new(); |
| 25 | ConversationState conversationState = new(storage); |
| 26 | builder.Services.AddSingleton(conversationState); |
| 27 | builder.Services.AddTransient<IBot, EchoBot>(); |
| 28 | |
| 29 | WebApplication app = builder.Build(); |
| 30 | |
| 31 | TeamsBotFrameworkHttpAdapter compatAdapter = (TeamsBotFrameworkHttpAdapter)app.Services.GetRequiredService<IBotFrameworkHttpAdapter>(); |
| 32 | compatAdapter.Use(new MyCompatMiddleware()); |
| 33 | compatAdapter.Use(new MyCompatMiddleware()); |
| 34 | compatAdapter.OnTurnError = async (turnContext, exception) => |
| 35 | { |
| 36 | await turnContext.SendActivityAsync(MessageFactory.Text("Oops, something went wrong! Please try again later.")); |
| 37 | }; |
| 38 | |
| 39 | app.MapPost("/api/messages", async (IBotFrameworkHttpAdapter adapter, IBot bot, HttpRequest request, HttpResponse response, CancellationToken ct) => |
| 40 | await adapter.ProcessAsync(request, response, bot, ct)).RequireAuthorization(); |
| 41 | |
| 42 | app.MapGet("/api/notify/{cid}", async (IBotFrameworkHttpAdapter adapter, string cid, CancellationToken ct) => |
| 43 | { |
| 44 | Activity proactive = new() |
| 45 | { |
| 46 | Conversation = new() { Id = cid }, |
| 47 | ServiceUrl = "https://smba.trafficmanager.net/teams" |
| 48 | }; |
| 49 | await ((BotAdapter)adapter).ContinueConversationAsync( |
| 50 | string.Empty, |
| 51 | proactive.GetConversationReference(), |
| 52 | async (turnContext, ct) => |
| 53 | { |
| 54 | await turnContext.SendActivityAsync( |
| 55 | MessageFactory.Text($"Proactive. <br/> SDK `{BotApplication.Version}` at {DateTime.Now:T}"), ct); |
| 56 | }, |
| 57 | ct); |
| 58 | }); |
| 59 | |
| 60 | app.Run(); |