-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFluentCommandProcessor.cs
More file actions
90 lines (74 loc) · 2.91 KB
/
FluentCommandProcessor.cs
File metadata and controls
90 lines (74 loc) · 2.91 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Text.Json;
using Botticelli.Bot.Interfaces.Processors;
using Botticelli.Bot.Utils;
using Botticelli.Client.Analytics;
using Botticelli.Framework.Commands.Validators;
using Botticelli.Interfaces;
using Botticelli.Shared.API.Client.Requests;
using Botticelli.Shared.ValueObjects;
using Microsoft.Extensions.Logging;
namespace Botticelli.Framework.Commands.Processors;
public abstract class FluentCommandProcessor<TCommand>(
ILogger logger,
MetricsProcessor metricsProcessor,
ICommandValidator<TCommand> commandValidator)
: ICommandProcessor
where TCommand : class, IFluentCommand
{
protected IBot? Bot;
public string CommandText { get; init; }
public async Task ProcessAsync(Message message, CancellationToken token)
{
logger.LogDebug("{processorName}.ProcessAsync() : processing a message {messageUid}: {message}",
nameof(FluentCommandProcessor<TCommand>),
message.Uid,
JsonSerializer.Serialize(message));
if (!CheckCommand(message))
{
logger.LogDebug("{processorName}.ProcessAsync() : processing a message {messageUid}: failed",
nameof(FluentCommandProcessor<TCommand>),
message.Uid);
return;
}
if (await commandValidator.Validate(message))
{
logger.LogDebug("{processorName}.ProcessAsync() : processing a message {messageUid}: command is valid",
nameof(FluentCommandProcessor<TCommand>),
message.Uid);
SendMetric();
await InnerProcess(message, token);
}
else
{
var errMessageRequest = new SendMessageRequest
{
Message =
{
Body = commandValidator.Help()
}
};
logger.LogDebug("{processorName}.ProcessAsync() : processing a message {messageUid}: command is NOT valid!",
nameof(FluentCommandProcessor<TCommand>),
message.Uid);
await Bot.SendMessageAsync(errMessageRequest, token);
}
}
public void SetBot(IBot bot)
{
Bot = bot;
}
public void SetServiceProvider(IServiceProvider sp)
{
}
private bool CheckCommand(Message message)
{
return message.Body?.ToLowerInvariant().Trim() == CommandText.ToLowerInvariant().Trim();
}
private void SendMetric()
{
logger.LogDebug("{processorName}.SendMetric() : sending a metric...", nameof(FluentCommandProcessor<TCommand>));
metricsProcessor.Process($"{GetType().Name.Replace("Processor", string.Empty)}Command",
BotDataUtils.GetBotId()!);
}
protected abstract Task InnerProcess(Message message, CancellationToken token);
}