🚧 Core improvements

This commit is contained in:
2020-03-01 22:46:07 +04:00
parent bae09d5ba4
commit a7b3f97f1a
6 changed files with 73 additions and 4 deletions
+9 -1
View File
@@ -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<string>(new string[]
{
Path.Join(Assembly.GetExecutingAssembly().Location, "modules"),
Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "modules"),
Path.Join(Environment.CurrentDirectory, "modules")
});
@@ -92,6 +93,12 @@ namespace Kruzya.TelegramBot.Core
types.AddRange(assemblyTypes);
}
}
// Instantiate them.
foreach (var module in types)
{
EnsureLoaded(module);
}
}
/// <summary>
@@ -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);
}
+22
View File
@@ -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<T> GetJsonAsync<T>(this HttpClient client, Uri requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
public static async Task<T> GetJsonAsync<T>(this HttpClient client, string requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
}
}
@@ -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
{
+17
View File
@@ -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);
}
}
}
+21
View File
@@ -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("<a href=\"tg://user?id={member.Id}\">");
if (!string.IsNullOrWhiteSpace(user.Username))
text.Append(user.Username);
else
text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode());
text.Append("</a>");
return text.ToString();
}
}
}
+1
View File
@@ -1,4 +1,5 @@
using BotFramework;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;