2023-08-19 00:15:50 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2023-05-27 01:58:31 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2023-06-03 02:07:30 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-10-11 04:11:06 +00:00
|
|
|
using BotSharp.Core.Plugins;
|
2024-01-15 19:15:18 +00:00
|
|
|
using BotSharp.Abstraction.Settings;
|
2024-03-18 19:36:41 +00:00
|
|
|
using BotSharp.Abstraction.Options;
|
|
|
|
|
using BotSharp.Abstraction.Messaging;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2023-07-21 22:17:16 +00:00
|
|
|
|
2023-05-27 01:58:31 +00:00
|
|
|
namespace BotSharp.Core;
|
|
|
|
|
|
2023-11-29 23:23:14 +00:00
|
|
|
public static class BotSharpCoreExtensions
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
2024-03-18 19:36:41 +00:00
|
|
|
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action<BotSharpOptions>? configOptions = null)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
2024-01-15 19:15:18 +00:00
|
|
|
services.AddScoped<ISettingService, SettingService>();
|
2023-06-11 23:46:02 +00:00
|
|
|
services.AddScoped<IUserService, UserService>();
|
2023-10-23 23:20:18 +00:00
|
|
|
|
2023-08-14 18:16:35 +00:00
|
|
|
RegisterPlugins(services, config);
|
2024-03-18 19:36:41 +00:00
|
|
|
ConfigureBotSharpOptions(services, configOptions);
|
2023-08-14 18:16:35 +00:00
|
|
|
return services;
|
|
|
|
|
}
|
2023-07-21 21:56:14 +00:00
|
|
|
|
2023-08-14 18:16:35 +00:00
|
|
|
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped<IBotSharpRepository>(sp =>
|
|
|
|
|
{
|
2023-09-01 22:33:36 +00:00
|
|
|
var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
|
2023-08-14 18:16:35 +00:00
|
|
|
return DataContextHelper.GetDbContext<BotSharpDbContext, DbContext4SqlServer>(myDatabaseSettings, sp);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 04:54:22 +00:00
|
|
|
//public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
|
|
|
|
|
//{
|
|
|
|
|
// services.AddScoped<IBotSharpRepository>(sp =>
|
|
|
|
|
// {
|
|
|
|
|
// var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
|
|
|
|
|
// return new FileRepository(myDatabaseSettings, sp);
|
|
|
|
|
// });
|
2023-07-21 21:56:14 +00:00
|
|
|
|
2023-09-03 04:54:22 +00:00
|
|
|
// return services;
|
|
|
|
|
//}
|
2023-05-27 01:58:31 +00:00
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
if (app == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(app));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 22:16:21 +00:00
|
|
|
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
|
|
|
|
|
|
2023-05-27 01:58:31 +00:00
|
|
|
return app;
|
|
|
|
|
}
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2024-03-18 19:36:41 +00:00
|
|
|
private static void ConfigureBotSharpOptions(IServiceCollection services, Action<BotSharpOptions>? configure)
|
|
|
|
|
{
|
|
|
|
|
var options = new BotSharpOptions();
|
|
|
|
|
if (configure != null)
|
|
|
|
|
{
|
|
|
|
|
configure(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ValidateJsonConverters(options);
|
|
|
|
|
services.AddSingleton(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ValidateJsonConverters(BotSharpOptions options)
|
|
|
|
|
{
|
|
|
|
|
var jsonConverters = options.JsonSerializerOptions.Converters;
|
|
|
|
|
if (jsonConverters != null)
|
|
|
|
|
{
|
|
|
|
|
// Remove the default rich message/template message converters if there are user-defined converters
|
|
|
|
|
if (jsonConverters.Count(x => x.Type?.Name == nameof(IRichMessage)) > 1)
|
|
|
|
|
{
|
|
|
|
|
var defaultRichMessageConverter = jsonConverters.First(x => x.Type?.Name == nameof(IRichMessage));
|
|
|
|
|
jsonConverters.Remove(defaultRichMessageConverter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (jsonConverters.Count(x => x.Type?.Name == nameof(ITemplateMessage)) > 1)
|
|
|
|
|
{
|
|
|
|
|
var defaultTemplateMessageConverter = jsonConverters.First(x => x.Type?.Name == nameof(ITemplateMessage));
|
|
|
|
|
jsonConverters.Remove(defaultTemplateMessageConverter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 02:56:04 +00:00
|
|
|
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
2023-10-11 04:11:06 +00:00
|
|
|
var pluginSettings = new PluginSettings();
|
2023-06-17 02:42:35 +00:00
|
|
|
config.Bind("PluginLoader", pluginSettings);
|
2024-01-15 19:15:18 +00:00
|
|
|
services.AddScoped(provider =>
|
|
|
|
|
{
|
|
|
|
|
var settingService = provider.GetRequiredService<ISettingService>();
|
|
|
|
|
return settingService.Bind<PluginSettings>("PluginLoader");
|
|
|
|
|
});
|
2023-06-11 23:46:02 +00:00
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
var loader = new PluginLoader(services, config, pluginSettings);
|
2023-09-24 16:20:02 +00:00
|
|
|
loader.Load(assembly =>
|
|
|
|
|
{
|
|
|
|
|
// Register routing handlers
|
|
|
|
|
var handlers = assembly.GetTypes()
|
|
|
|
|
.Where(x => x.IsClass)
|
|
|
|
|
.Where(x => x.GetInterface(nameof(IRoutingHandler)) != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var handler in handlers)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped(typeof(IRoutingHandler), handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register function callback
|
|
|
|
|
var functions = assembly.GetTypes()
|
|
|
|
|
.Where(x => x.IsClass)
|
|
|
|
|
.Where(x => x.GetInterface(nameof(IFunctionCallback)) != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var function in functions)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped(typeof(IFunctionCallback), function);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-06-26 22:16:21 +00:00
|
|
|
|
|
|
|
|
services.AddSingleton(loader);
|
2023-06-10 02:56:04 +00:00
|
|
|
}
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|