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