microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Controllers/MessageController.cs
33lines · modecode
| 1 | using Microsoft.AspNetCore.Http; |
| 2 | using Microsoft.AspNetCore.Mvc; |
| 3 | using Microsoft.Extensions.DependencyInjection; |
| 4 | using Microsoft.Extensions.Hosting; |
| 5 | using Microsoft.Teams.Api.Activities; |
| 6 | using Microsoft.Teams.Api.Auth; |
| 7 | using Microsoft.Teams.Apps.Extensions; |
| 8 | |
| 9 | namespace Microsoft.Teams.Plugins.AspNetCore.Controllers; |
| 10 | |
| 11 | [ApiController] |
| 12 | public class MessageController : ControllerBase |
| 13 | { |
| 14 | private readonly AspNetCorePlugin _plugin; |
| 15 | private readonly IHostApplicationLifetime _lifetime; |
| 16 | |
| 17 | public MessageController(AspNetCorePlugin plugin, IHostApplicationLifetime lifetime) |
| 18 | { |
| 19 | _plugin = plugin; |
| 20 | _lifetime = lifetime; |
| 21 | } |
| 22 | |
| 23 | [HttpPost("/api/messages")] |
| 24 | public async Task<IResult> OnMessage([FromBody] Activity activity) |
| 25 | { |
| 26 | var authHeader = HttpContext.Request.Headers.Authorization.FirstOrDefault() ?? throw new UnauthorizedAccessException(); |
| 27 | var token = new JsonWebToken(authHeader.Replace("Bearer ", "")); |
| 28 | var context = HttpContext.RequestServices.GetRequiredService<TeamsContext>(); |
| 29 | context.Token = token; |
| 30 | var res = await _plugin.Do(token, activity, _lifetime.ApplicationStopping); |
| 31 | return Results.Json(res.Body, statusCode: (int)res.Status); |
| 32 | } |
| 33 | } |