-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBotCommandHandlerBase.cs
55 lines (45 loc) · 1.87 KB
/
BotCommandHandlerBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Threading.Tasks;
using CommunityBot.Contracts;
using CommunityBot.Handlers.Results;
using CommunityBot.Helpers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace CommunityBot.Handlers.BotCommands
{
public abstract class BotCommandHandlerBase : UpdateHandlerBase
{
public BotCommandHandlerBase(
IOptions<BotConfigurationOptions> options,
ILoggerFactory logger)
: base(options, logger)
{
}
protected override UpdateType[] AllowedUpdates => new[] {UpdateType.Message};
protected abstract BotCommandConfig Config { get; }
protected override bool CanHandle(Update update)
{
return update.Message.ContainCommand(Config.BotCommand);
}
protected override async Task<IUpdateHandlerResult> HandleUpdateInternal(Update update)
{
if (Config.IsForAdmin && !IsFromAdmin(update))
{
return ReplyPlainText(update, "Данная команда доступна только администраторам!");
}
if (Config.AllowOnlyInPrivate && !update.Message.IsPrivate())
{
return ReplyPlainText(update, "Команда доступна только в ЛС");
}
var (_, commandArg) = update.Message.GetFirstBotCommand()!.Value;
if (Config.ArgRequiredMessage.IsNotBlank() && commandArg.IsBlank())
{
return ReplyPlainText(update, Config.ArgRequiredMessage!);
}
return await HandleUpdateInternal(update, commandArg);
}
protected abstract Task<IUpdateHandlerResult> HandleUpdateInternal(Update update, string commandArg);
}
}