microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
teams.net/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.BotBuilder/Controllers
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.BotBuilder/Controllers/MessageController.cs
49lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Http; |
| 5 | using Microsoft.AspNetCore.Mvc; |
| 6 | using Microsoft.Bot.Builder; |
| 7 | using Microsoft.Bot.Builder.Integration.AspNet.Core; |
| 8 | using Microsoft.Extensions.Hosting; |
| 9 | |
| 10 | namespace Microsoft.Teams.Plugins.AspNetCore.BotBuilder |
| 11 | { |
| 12 | [ApiController] |
| 13 | public class MessageController : ControllerBase |
| 14 | { |
| 15 | private readonly IBotFrameworkHttpAdapter _adapter; |
| 16 | private readonly IBot _bot; |
| 17 | |
| 18 | private readonly AspNetCorePlugin _plugin; |
| 19 | |
| 20 | private readonly IHostApplicationLifetime _lifetime; |
| 21 | |
| 22 | public MessageController(IBotFrameworkHttpAdapter adapter, IBot bot, AspNetCorePlugin plugin, IHostApplicationLifetime lifetime) |
| 23 | { |
| 24 | _plugin = plugin; |
| 25 | _lifetime = lifetime; |
| 26 | _adapter = adapter; |
| 27 | _bot = bot; |
| 28 | } |
| 29 | |
| 30 | [HttpPost("/api/messages")] |
| 31 | public async Task<IResult> PostAsync() |
| 32 | { |
| 33 | // Enable buffering so that the request can be read by the adapter and the plugin |
| 34 | HttpContext.Request.EnableBuffering(); |
| 35 | |
| 36 | // Delegate the processing of the HTTP POST to the adapter. |
| 37 | // The adapter will invoke the bot. |
| 38 | await _adapter.ProcessAsync(HttpContext.Request, HttpContext.Response, _bot); |
| 39 | |
| 40 | if (Response.HasStarted) |
| 41 | { |
| 42 | return Results.Empty; |
| 43 | } |
| 44 | |
| 45 | // Fallback logic use the plugin to process the activity |
| 46 | return await _plugin.Do(HttpContext, _lifetime.ApplicationStopping); |
| 47 | } |
| 48 | } |
| 49 | } |