گام ششم: واکنش به آپدیت بخش 1
جهت سادهسازی کدهای تکراری 2 کلاس هلپر ساختیم.Helpers\ScopeHelpers.cs
و برای جلوگیری از هاردکد کردن یک کلاس ثوابت ساختیم:Services\Bot\BotConsts.cs
فهرست آموزش@BaleBotNet | کلاینت داتنت بازوی بله
جهت سادهسازی کدهای تکراری 2 کلاس هلپر ساختیم.Helpers\ScopeHelpers.cs
namespace MyBaleBot.Helpers;
public static class ScopeHelpers
{
public static T Get<T>(this IServiceScope scope)
where T : notnull => scope.ServiceProvider.GetRequiredService<T>();
}
Helpers\UpdateReciverHelpers.cs
using BaleBotNet.Enums;
using BaleBotNet.Types;
namespace MyBaleBot.Helpers;
public static class UpdateReciverHelpers
{
public static bool IsPrivate(this Message message) => message.Chat.Type == ChatType.Private;
public static bool IsGroup(this Message message) => message.Chat.Type == ChatType.Group;
public static bool IsFromChat(this Message message, long chatId) => message.Chat.Id == chatId;
public static bool IsFromChat(this CallbackQuery query, long chatId) =>
query.Message?.Chat?.Id == chatId;
public static bool IsCommand(this Message message, string command)
{
var firstPart = message.Text?.Split(" ").ElementAtOrDefault(0);
if (firstPart == null)
return false;
return firstPart.Equals(command, StringComparison.OrdinalIgnoreCase);
}
}
و برای جلوگیری از هاردکد کردن یک کلاس ثوابت ساختیم:Services\Bot\BotConsts.cs
namespace MyBaleBot.Services.Bot;
public static class BotConsts
{
public static class Commands
{
public const string Start = "/start";
public const string Whois = "/whois";
}
public static readonly Dictionary<string, string> CommandsMenu =
new() { { Commands.Start, "شروع مجدد" }, { Commands.Whois, "من کیام؟" } };
}
فهرست آموزش@BaleBotNet | کلاینت داتنت بازوی بله
۱۷:۴۵