diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs new file mode 100644 index 00000000..d4106b7a --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Builder; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Abstraction.Plugins +{ + public interface IBotSharpAppPlugin: IBotSharpPlugin + { + void Configure(IApplicationBuilder app); + } +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index cf65850d..40e994b1 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -40,6 +40,8 @@ public static class BotSharpServiceCollectionExtensions throw new ArgumentNullException(nameof(app)); } + app.ApplicationServices.GetRequiredService().Configure(app); + return app; } @@ -77,5 +79,7 @@ public static class BotSharpServiceCollectionExtensions var loader = new PluginLoader(services, config, pluginSettings); loader.Load(); + + services.AddSingleton(loader); } } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index 79cdd134..6e79dc03 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using System.Drawing; using System.IO; @@ -13,7 +14,7 @@ public class PluginLoader private readonly PluginLoaderSettings _settings; private static List _modules = new List(); - public PluginLoader(IServiceCollection services, + public PluginLoader(IServiceCollection services, IConfiguration config, PluginLoaderSettings settings) { @@ -59,4 +60,20 @@ public class PluginLoader } }); } + + public void Configure(IApplicationBuilder app) + { + if(_modules.Count == 0) + { + Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow); + } + + _modules.ForEach(module => + { + if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null) + { + (module as IBotSharpAppPlugin).Configure(app); + } + }); + } } diff --git a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj index 4dc14bdf..611146dd 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj +++ b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj @@ -24,7 +24,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.WeChat/BotSharpMessageHandler.cs b/src/Plugins/BotSharp.Plugin.WeChat/BotSharpMessageHandler.cs index f25c882d..d12a6f45 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/BotSharpMessageHandler.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/BotSharpMessageHandler.cs @@ -18,6 +18,9 @@ namespace BotSharp.Plugin.WeChat { public class BotSharpMessageHandler : Senparc.Weixin.MP.MessageHandlers.MessageHandler { + public static Func GenerateMessageHandler = (stream, postModel, maxRecordCount, serviceProvider) + => new BotSharpMessageHandler(stream, postModel, maxRecordCount, false /* 是否只允许处理加密消息,以提高安全性 */, serviceProvider: serviceProvider); + public BotSharpMessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0, bool onlyAllowEncryptMessage = false, DeveloperInfo developerInfo = null, IServiceProvider serviceProvider = null) : base(inputStream, postModel, maxRecordCount, onlyAllowEncryptMessage, developerInfo, serviceProvider) { } diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs index f5b24a1c..6ed9fb72 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs @@ -4,16 +4,20 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; -using Senparc.Weixin.AspNet; +using Senparc.CO2NET.AspNet; +using Senparc.CO2NET; using Senparc.Weixin.RegisterServices; using System; using System.Collections.Generic; using System.Text; +using Senparc.Weixin; +using Senparc.Weixin.MP; +using Senparc.Weixin.MP.MessageHandlers.Middleware; namespace BotSharp.Plugin.WeChat { - public class WeChatPlugin : IBotSharpPlugin + public class WeChatPlugin : IBotSharpAppPlugin { public void RegisterDI(IServiceCollection services, IConfiguration config) { @@ -26,9 +30,20 @@ namespace BotSharp.Plugin.WeChat services.TryAddSingleton(s => s.GetRequiredService()); } - public void ConfigurateApplication(IApplicationBuilder app) + public void Configure(IApplicationBuilder app) { - // TODO: app.UseSenparcWeixin + var env = app.ApplicationServices.GetRequiredService(); + var register = app.UseSenparcGlobal(env); + register.UseSenparcWeixin(null, (svc, settings) => + { + svc.RegisterMpAccount(settings, "WeChat"); + }, app.ApplicationServices); + + app.UseMessageHandlerForMp("/WeixinAsync", BotSharpMessageHandler.GenerateMessageHandler, options => + { + options.AccountSettingFunc = context => Senparc.Weixin.Config.SenparcWeixinSetting; + }); + } } }