mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
🚧 Core re-init
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
/packages/
|
||||||
|
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
appsettings.json
|
||||||
+152
@@ -0,0 +1,152 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.Core
|
||||||
|
{
|
||||||
|
public sealed class Core
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The Core API version.
|
||||||
|
/// </summary>
|
||||||
|
public UInt32 ApiVersion => 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The array that contains all modules instances.
|
||||||
|
/// </summary>
|
||||||
|
public Module[] Modules => _modules.ToArray();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current bot configuration.
|
||||||
|
/// </summary>
|
||||||
|
public IConfiguration Configuration => _configuration;
|
||||||
|
|
||||||
|
#region Private properties
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The array that contains all modules instances.
|
||||||
|
/// </summary>
|
||||||
|
private List<Module> _modules = new List<Module>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current bot configuration.
|
||||||
|
/// </summary>
|
||||||
|
private IConfiguration _configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The all directories for loading our module assemblies.
|
||||||
|
/// </summary>
|
||||||
|
private List<string> _directories
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
// For start, load all assemblies in required directory.
|
||||||
|
// And register in internal temporary array.
|
||||||
|
var modulesDirectory = new List<string>(new string[]
|
||||||
|
{
|
||||||
|
Path.Join(Assembly.GetExecutingAssembly().Location, "modules"),
|
||||||
|
Path.Join(Environment.CurrentDirectory, "modules")
|
||||||
|
});
|
||||||
|
|
||||||
|
// Also lookup load directories from config.
|
||||||
|
modulesDirectory.AddRange(_configuration.GetSection("ModuleDirectories").GetChildren().Select(q => q.Value)
|
||||||
|
.ToList());
|
||||||
|
|
||||||
|
return modulesDirectory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public Core(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
|
||||||
|
Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes all required submodules.
|
||||||
|
/// </summary>
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
var types = new List<Type>();
|
||||||
|
foreach (var directory in _directories)
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var file in Directory.EnumerateFiles(directory, "*.dll", SearchOption.AllDirectories))
|
||||||
|
{
|
||||||
|
var assemblyTypes = LoadAssembly(file);
|
||||||
|
if (assemblyTypes == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
types.AddRange(assemblyTypes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads the assembly and appends all Module types to list.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The full path to assembly file</param>
|
||||||
|
/// <returns><see cref="IEnumerable{T}"/> if success, null otherwise.</returns>
|
||||||
|
private IEnumerable<Type> LoadAssembly(string path)
|
||||||
|
{
|
||||||
|
Assembly assembly;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
assembly = Assembly.LoadFile(path);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// don't load any invalid module.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return assembly.GetExportedTypes().Where(x => !x.IsAbstract && x.IsSubclassOf(typeof(Module)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures if module is loaded and started. Note that call can change module event chain.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The Module type.</typeparam>
|
||||||
|
/// <returns>Module instance.</returns>
|
||||||
|
public T EnsureLoaded<T>()
|
||||||
|
where T : Module
|
||||||
|
{
|
||||||
|
return (T) EnsureLoaded(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures if module is loaded and started. Note that call can change module event chain.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="moduleType">The Module type.</param>
|
||||||
|
/// <returns>Module instance.</returns>
|
||||||
|
/// <exception cref="Exception">The given type is not extends the Module class</exception>
|
||||||
|
public Module EnsureLoaded(Type moduleType)
|
||||||
|
{
|
||||||
|
if (!moduleType.IsSubclassOf(typeof(Module)))
|
||||||
|
{
|
||||||
|
throw new Exception("Received incorrect module type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var module = _modules.FirstOrDefault(module => module.GetType() == moduleType);
|
||||||
|
if (module == null)
|
||||||
|
{
|
||||||
|
module = (Module) Activator.CreateInstance(moduleType, this);
|
||||||
|
_modules.Add(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<AssemblyName>TelegramBot</AssemblyName>
|
||||||
|
<RootNamespace>Kruzya.TelegramBot.Core</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AleXr64.BotFramework" Version="0.0.17" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.Core
|
||||||
|
{
|
||||||
|
public abstract class Module
|
||||||
|
{
|
||||||
|
protected readonly Core Core;
|
||||||
|
protected IConfiguration Configuration => Core.Configuration;
|
||||||
|
|
||||||
|
public Module(Core core)
|
||||||
|
{
|
||||||
|
Core = core;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures if module is loaded and started. Note that call can change module event chain.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The Module type.</typeparam>
|
||||||
|
/// <returns>Module instance.</returns>
|
||||||
|
protected T EnsureLoaded<T>() where T : Module
|
||||||
|
{
|
||||||
|
return (T) Core.EnsureLoaded(typeof(T));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.Core
|
||||||
|
{
|
||||||
|
internal static class ModuleArrayExtension
|
||||||
|
{
|
||||||
|
public static void ConfigureServices(this Module[] modules, IServiceCollection services)
|
||||||
|
{
|
||||||
|
foreach (var module in modules) module.ConfigureServices(services);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Configure(this Module[] modules, IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
foreach (var module in modules) module.Configure(app, env);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.Core
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:28272",
|
||||||
|
"sslPort": 44341
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": false,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Core": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"launchBrowser": false,
|
||||||
|
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using BotFramework;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Kruzya.TelegramBot.Core
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
private Core _core;
|
||||||
|
|
||||||
|
public Startup(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
// TODO: add Core to DI.
|
||||||
|
_core = new Core(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddTelegramBot();
|
||||||
|
services.AddLogging(builder => builder.AddConsole());
|
||||||
|
|
||||||
|
_core.Modules.ConfigureServices(services);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
_core.Modules.Configure(app, env);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{B41CB31A-641E-4079-87ED-5CE310B2D8C1}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B41CB31A-641E-4079-87ED-5CE310B2D8C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B41CB31A-641E-4079-87ED-5CE310B2D8C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B41CB31A-641E-4079-87ED-5CE310B2D8C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B41CB31A-641E-4079-87ED-5CE310B2D8C1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(NestedProjects) = preSolution
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
Reference in New Issue
Block a user