2023-06-24 09:25:55 +00:00
|
|
|
using BotSharp.Abstraction.Plugins;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2023-06-26 22:16:21 +00:00
|
|
|
using Senparc.CO2NET.AspNet;
|
|
|
|
|
using Senparc.CO2NET;
|
2023-06-24 09:25:55 +00:00
|
|
|
using Senparc.Weixin.RegisterServices;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2023-06-26 22:16:21 +00:00
|
|
|
using Senparc.Weixin;
|
|
|
|
|
using Senparc.Weixin.MP;
|
|
|
|
|
using Senparc.Weixin.MP.MessageHandlers.Middleware;
|
2023-06-24 09:25:55 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.WeChat
|
|
|
|
|
{
|
|
|
|
|
|
2023-06-26 22:16:21 +00:00
|
|
|
public class WeChatPlugin : IBotSharpAppPlugin
|
2023-06-24 09:25:55 +00:00
|
|
|
{
|
|
|
|
|
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
|
|
|
|
|
services.AddSenparcWeixinServices(config);
|
|
|
|
|
|
|
|
|
|
services.AddHostedService<WeChatBackgroundService>();
|
|
|
|
|
|
|
|
|
|
services.TryAddSingleton<IMessageQueue>(s => s.GetRequiredService<WeChatBackgroundService>());
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 22:16:21 +00:00
|
|
|
public void Configure(IApplicationBuilder app)
|
2023-06-24 09:25:55 +00:00
|
|
|
{
|
2023-06-26 22:16:21 +00:00
|
|
|
var env = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-24 09:25:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|