From a7b3f97f1a6917874d63c36c69e6dd97d7c45b07 Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Sun, 1 Mar 2020 22:46:07 +0400 Subject: [PATCH] :construction: Core improvements --- Core/Core.cs | 12 ++++++++-- Core/Extensions/HttpClientExtension.cs | 22 +++++++++++++++++++ Core/{ => Extensions}/ModuleArrayExtension.cs | 2 +- Core/Extensions/StringExtension.cs | 17 ++++++++++++++ Core/Extensions/UserExtension.cs | 21 ++++++++++++++++++ Core/Startup.cs | 3 ++- 6 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 Core/Extensions/HttpClientExtension.cs rename Core/{ => Extensions}/ModuleArrayExtension.cs (90%) create mode 100644 Core/Extensions/StringExtension.cs create mode 100644 Core/Extensions/UserExtension.cs diff --git a/Core/Core.cs b/Core/Core.cs index 3585bb8..9d342a9 100644 --- a/Core/Core.cs +++ b/Core/Core.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; namespace Kruzya.TelegramBot.Core { @@ -47,7 +48,7 @@ namespace Kruzya.TelegramBot.Core // And register in internal temporary array. var modulesDirectory = new List(new string[] { - Path.Join(Assembly.GetExecutingAssembly().Location, "modules"), + Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "modules"), Path.Join(Environment.CurrentDirectory, "modules") }); @@ -58,7 +59,7 @@ namespace Kruzya.TelegramBot.Core return modulesDirectory; } } - + #endregion public Core(IConfiguration configuration) @@ -92,6 +93,12 @@ namespace Kruzya.TelegramBot.Core types.AddRange(assemblyTypes); } } + + // Instantiate them. + foreach (var module in types) + { + EnsureLoaded(module); + } } /// @@ -143,6 +150,7 @@ namespace Kruzya.TelegramBot.Core if (module == null) { module = (Module) Activator.CreateInstance(moduleType, this); + Console.WriteLine($"Started module {module.GetType().FullName}"); _modules.Add(module); } diff --git a/Core/Extensions/HttpClientExtension.cs b/Core/Extensions/HttpClientExtension.cs new file mode 100644 index 0000000..452da7c --- /dev/null +++ b/Core/Extensions/HttpClientExtension.cs @@ -0,0 +1,22 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace Kruzya.TelegramBot.Core.Extensions +{ + public static class HttpClientExtension + { + public static async Task GetJsonAsync(this HttpClient client, Uri requestUri) + { + var response = await client.GetStringAsync(requestUri); + return JsonConvert.DeserializeObject(response); + } + + public static async Task GetJsonAsync(this HttpClient client, string requestUri) + { + var response = await client.GetStringAsync(requestUri); + return JsonConvert.DeserializeObject(response); + } + } +} \ No newline at end of file diff --git a/Core/ModuleArrayExtension.cs b/Core/Extensions/ModuleArrayExtension.cs similarity index 90% rename from Core/ModuleArrayExtension.cs rename to Core/Extensions/ModuleArrayExtension.cs index 738804c..2e615a0 100644 --- a/Core/ModuleArrayExtension.cs +++ b/Core/Extensions/ModuleArrayExtension.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; -namespace Kruzya.TelegramBot.Core +namespace Kruzya.TelegramBot.Core.Extensions { internal static class ModuleArrayExtension { diff --git a/Core/Extensions/StringExtension.cs b/Core/Extensions/StringExtension.cs new file mode 100644 index 0000000..62f9ab5 --- /dev/null +++ b/Core/Extensions/StringExtension.cs @@ -0,0 +1,17 @@ +using System.Web; + +namespace Kruzya.TelegramBot.Core.Extensions +{ + public static class StringExtension + { + public static string HtmlEncode(this string content) + { + return HttpUtility.HtmlEncode(content); + } + + public static string HtmlDecode(this string content) + { + return HttpUtility.HtmlDecode(content); + } + } +} \ No newline at end of file diff --git a/Core/Extensions/UserExtension.cs b/Core/Extensions/UserExtension.cs new file mode 100644 index 0000000..ad34cc9 --- /dev/null +++ b/Core/Extensions/UserExtension.cs @@ -0,0 +1,21 @@ +using System.Text; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.Core.Extensions +{ + public static class UserExtension + { + public static string ToHtml(this User user) + { + var text = new StringBuilder(); + text.Append(""); + if (!string.IsNullOrWhiteSpace(user.Username)) + text.Append(user.Username); + else + text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode()); + text.Append(""); + + return text.ToString(); + } + } +} \ No newline at end of file diff --git a/Core/Startup.cs b/Core/Startup.cs index 31fa4d0..b8e20e0 100644 --- a/Core/Startup.cs +++ b/Core/Startup.cs @@ -1,4 +1,5 @@ using BotFramework; +using Kruzya.TelegramBot.Core.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -23,7 +24,7 @@ namespace Kruzya.TelegramBot.Core { services.AddTelegramBot(); services.AddLogging(builder => builder.AddConsole()); - + _core.Modules.ConfigureServices(services); }