microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/DevToolsPlugin.cs
164lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Reflection; |
| 5 | using System.Text; |
| 6 | |
| 7 | using Microsoft.AspNetCore.Builder; |
| 8 | using Microsoft.AspNetCore.Hosting.Server; |
| 9 | using Microsoft.AspNetCore.Hosting.Server.Features; |
| 10 | using Microsoft.AspNetCore.Http.Features; |
| 11 | using Microsoft.Extensions.DependencyInjection; |
| 12 | using Microsoft.Extensions.FileProviders; |
| 13 | using Microsoft.Teams.Apps; |
| 14 | using Microsoft.Teams.Apps.Events; |
| 15 | using Microsoft.Teams.Apps.Plugins; |
| 16 | using Microsoft.Extensions.Logging; |
| 17 | using Microsoft.Extensions.Logging.Abstractions; |
| 18 | using Microsoft.Teams.Common.Text; |
| 19 | using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions; |
| 20 | using Microsoft.Teams.Plugins.AspNetCore.DevTools.Models; |
| 21 | |
| 22 | namespace Microsoft.Teams.Plugins.AspNetCore.DevTools; |
| 23 | |
| 24 | [Plugin] |
| 25 | public class DevToolsPlugin : IAspNetCorePlugin |
| 26 | { |
| 27 | [Dependency("AppId", optional: true)] |
| 28 | public string? AppId { get; set; } |
| 29 | |
| 30 | [Dependency("AppName", optional: true)] |
| 31 | public string? AppName { get; set; } |
| 32 | |
| 33 | public event EventFunction Events; |
| 34 | |
| 35 | internal MetaData MetaData => new() { Id = AppId, Name = AppName, Pages = _pages }; |
| 36 | internal readonly WebSocketCollection Sockets = []; |
| 37 | |
| 38 | private readonly ISenderPlugin _sender; |
| 39 | private readonly IServiceProvider _services; |
| 40 | private readonly IList<Page> _pages = []; |
| 41 | private readonly TeamsDevToolsSettings _settings; |
| 42 | private readonly ILogger<DevToolsPlugin> Logger; |
| 43 | |
| 44 | public DevToolsPlugin(AspNetCorePlugin sender, IServiceProvider provider, ILogger<DevToolsPlugin>? logger = null) |
| 45 | { |
| 46 | _sender = sender; |
| 47 | _services = provider; |
| 48 | _settings = provider.GetRequiredService<TeamsDevToolsSettings>(); |
| 49 | Logger = logger ?? NullLogger<DevToolsPlugin>.Instance; |
| 50 | } |
| 51 | |
| 52 | public IApplicationBuilder Configure(IApplicationBuilder builder) |
| 53 | { |
| 54 | builder.UseWebSockets(new WebSocketOptions() |
| 55 | { |
| 56 | AllowedOrigins = { "*" } |
| 57 | }); |
| 58 | |
| 59 | builder.UseStaticFiles(new StaticFileOptions() |
| 60 | { |
| 61 | FileProvider = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly(), "web"), |
| 62 | ServeUnknownFileTypes = true, |
| 63 | RequestPath = "/devtools" |
| 64 | }); |
| 65 | |
| 66 | builder.Use(async (context, next) => |
| 67 | { |
| 68 | try |
| 69 | { |
| 70 | await next(context); |
| 71 | } |
| 72 | catch (Exception ex) |
| 73 | { |
| 74 | Logger.LogError(ex, "http error"); |
| 75 | throw new Exception(ex.Message, innerException: ex); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | return builder; |
| 80 | } |
| 81 | |
| 82 | public DevToolsPlugin AddPage(Page page) |
| 83 | { |
| 84 | _pages.Add(page); |
| 85 | Logger.LogDebug($"page '{page.Name}' added at '{page.Url}'"); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public Task OnInit(App app, CancellationToken cancellationToken = default) |
| 90 | { |
| 91 | foreach (var page in _settings.Pages) |
| 92 | { |
| 93 | AddPage(page); |
| 94 | } |
| 95 | |
| 96 | Logger.LogWarning( |
| 97 | new StringBuilder() |
| 98 | .Bold( |
| 99 | new StringBuilder() |
| 100 | .Yellow("⚠️ Devtools are not secure and should not be used production environments ⚠️") |
| 101 | .ToString() |
| 102 | ).ToString() |
| 103 | ); |
| 104 | |
| 105 | return Task.CompletedTask; |
| 106 | } |
| 107 | |
| 108 | public Task OnStart(App app, CancellationToken cancellationToken = default) |
| 109 | { |
| 110 | var server = _services.GetRequiredService<IServer>(); |
| 111 | var addresses = server.Features.GetRequiredFeature<IServerAddressesFeature>().Addresses; |
| 112 | |
| 113 | foreach (var address in addresses) |
| 114 | { |
| 115 | Logger.LogInformation($"Available at {address}/devtools"); |
| 116 | } |
| 117 | |
| 118 | Logger.LogDebug("OnStart"); |
| 119 | return Task.CompletedTask; |
| 120 | } |
| 121 | |
| 122 | public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default) |
| 123 | { |
| 124 | Logger.LogDebug("OnError"); |
| 125 | return Task.CompletedTask; |
| 126 | } |
| 127 | |
| 128 | public async Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default) |
| 129 | { |
| 130 | Logger.LogDebug("OnActivity"); |
| 131 | |
| 132 | await Sockets.Emit( |
| 133 | DevTools.Events.ActivityEvent.Received( |
| 134 | @event.Activity, |
| 135 | @event.Activity.Conversation |
| 136 | ), |
| 137 | cancellationToken |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | public async Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default) |
| 142 | { |
| 143 | Logger.LogDebug("OnActivitySent"); |
| 144 | |
| 145 | await Sockets.Emit( |
| 146 | DevTools.Events.ActivityEvent.Sent( |
| 147 | @event.Activity, |
| 148 | @event.Activity.Conversation |
| 149 | ), |
| 150 | cancellationToken |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default) |
| 155 | { |
| 156 | Logger.LogDebug("OnActivityResponse"); |
| 157 | return Task.CompletedTask; |
| 158 | } |
| 159 | |
| 160 | public Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default) |
| 161 | { |
| 162 | return _sender.Do(@event, cancellationToken); |
| 163 | } |
| 164 | } |