Update dependencies

This commit is contained in:
Sergey Nazarov 2024-03-30 12:08:54 +03:00
parent c8da9e618b
commit b96224b6e0
8 changed files with 42 additions and 30 deletions

View File

@ -7,8 +7,8 @@
<InsightTelegramBotVersion>0.16.0</InsightTelegramBotVersion>
</PropertyGroup>
<ItemGroup Label="Nocr">
<PackageVersion Include="Nocr.TextMatcher.Api.Contracts" Version="0.4.23" />
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.4.23" />
<PackageVersion Include="Nocr.TextMatcher.Api.Contracts" Version="0.4.25" />
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.4.25" />
<PackageVersion Include="Nocr.Users.Api.Contracts" Version="0.4.23" />
</ItemGroup>
<ItemGroup Label="Rebus">

View File

@ -6,6 +6,8 @@ using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Users;
using Nocr.TextMatcher.Api.Contracts.TextMatches;
using Nocr.TextMatcher.Api.Contracts.TextMatches.Requests;
using Nocr.TextMatcher.Contracts;
using Nocr.Users.Api.Contracts.Users.Dto;
using Telegram.Bot.Types;
namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeMessage;
@ -14,7 +16,7 @@ public class SubscribeMessageHandler : IMatchingUpdateHandler<SubscribeMessageMa
{
private readonly IMessageDispatcherQueue _messageQueue;
private readonly IUsersService _usersService;
private readonly ITextMatchesController _textMatchesController;
private readonly ITextSubscriptionsController _textSubscriptionsController;
/// <summary>
/// Regex to match command "/subscribe @username match_type keywords". <br/>
@ -25,12 +27,12 @@ public class SubscribeMessageHandler : IMatchingUpdateHandler<SubscribeMessageMa
RegexOptions.Compiled);
public SubscribeMessageHandler(IMessageDispatcherQueue messageQueue, IUsersService usersService,
ITextMatchesController textMatchesController)
ITextSubscriptionsController textSubscriptionsController)
{
_messageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue));
_usersService = usersService ?? throw new ArgumentNullException(nameof(usersService));
_textMatchesController =
textMatchesController ?? throw new ArgumentNullException(nameof(textMatchesController));
_textSubscriptionsController =
textSubscriptionsController ?? throw new ArgumentNullException(nameof(textSubscriptionsController));
}
public async Task Handle(Update update, CancellationToken cancellationToken = default)
@ -48,21 +50,29 @@ public class SubscribeMessageHandler : IMatchingUpdateHandler<SubscribeMessageMa
}
var username = match.Groups[1].Value.TrimStart('@');
var rule = match.Groups[2].Value;
if (!Enum.TryParse<TextSubscriptionRule>(match.Groups[2].Value, true, out var rule))
{
_messageQueue.Enqueue(new TextMessage(telegramId)
{
Text = "Не удалось извлечь тип подписки"
});
return;
}
var template = match.Groups[3].Value;
var user = await _usersService.GetOrCreate(telegramId, update.Message.From.Username, cancellationToken);
var matchId = await _textMatchesController.Create(new CreateTextMatchRequest
var subscriptionId = await _textSubscriptionsController.Create(new CreateTextSubscriptionRequest
{
UserId = user.Id,
ChatUsername = username,
Rule = (TextMatchRule)Convert.ToInt32(rule),
Rule = rule,
Template = template,
}, cancellationToken);
_messageQueue.Enqueue(new TextMessage(telegramId)
{
Text = $"Подписка создана: {matchId}"
Text = $"Подписка создана: {subscriptionId}"
});
}
}

View File

@ -5,6 +5,7 @@ using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Matches;
using Nocr.TelegramClient.AppServices.Users;
using Nocr.TextMatcher.Api.Contracts.TextMatches;
using Nocr.Users.Api.Contracts.Users.Dto;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
@ -14,14 +15,14 @@ public class SubscriptionsMessageHandler : IMatchingUpdateHandler<SubscriptionsM
{
private readonly IMessageDispatcherQueue _messageQueue;
private readonly IUsersService _usersService;
private readonly ITextMatchesController _matchesController;
private readonly ITextSubscriptionsController _textSubscriptionsController;
public SubscriptionsMessageHandler(IMessageDispatcherQueue messageQueue, IUsersService usersService,
ITextMatchesController matchesController)
ITextSubscriptionsController textSubscriptionsController)
{
_messageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue));
_usersService = usersService ?? throw new ArgumentNullException(nameof(usersService));
_matchesController = matchesController ?? throw new ArgumentNullException(nameof(matchesController));
_textSubscriptionsController = textSubscriptionsController ?? throw new ArgumentNullException(nameof(textSubscriptionsController));
}
public async Task Handle(Update update, CancellationToken cancellationToken = default)
@ -29,7 +30,7 @@ public class SubscriptionsMessageHandler : IMatchingUpdateHandler<SubscriptionsM
var telegramId = update.Message.From.Id;
var user = await _usersService.GetOrCreate(telegramId, update.Message.From.Username, cancellationToken);
var subscriptions = await _matchesController.GetByUserId(user.Id, cancellationToken);
var subscriptions = await _textSubscriptionsController.GetByUserId(user.Id, cancellationToken);
foreach (var subscription in subscriptions)
{

View File

@ -10,7 +10,7 @@ using Telegram.Bot.Types.Enums;
namespace Nocr.TelegramClient.AppServices.Matches.Handlers;
public class TextMatchMatchedHandler : IHandleMessages<TextMatchMatched>
public class TextMatchMatchedHandler : IHandleMessages<TextSubscriptionMatched>
{
private readonly ILogger<TextMatchMatchedHandler> _logger;
private readonly IBot _bot;
@ -23,13 +23,13 @@ public class TextMatchMatchedHandler : IHandleMessages<TextMatchMatched>
_usersService = usersService ?? throw new ArgumentNullException(nameof(usersService));
}
public async Task Handle(TextMatchMatched message)
public async Task Handle(TextSubscriptionMatched message)
{
var user = await _usersService.GetById(message.MatchUserId);
var user = await _usersService.GetById(message.SubscriptionUserId);
if (user == null)
{
_logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found", message.MatchUserId,
message.MatchId, message.Id);
_logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found", message.SubscriptionUserId,
message.SubscriptionId, message.Id);
return;
}
@ -44,7 +44,7 @@ public class TextMatchMatchedHandler : IHandleMessages<TextMatchMatched>
var fromUsername = string.IsNullOrWhiteSpace(message.From) ? "anonymous" : message.From;
var textMessage = new TextMessage(long.Parse(identity.Identity))
{
Text = $"[{message.PublishedDateTime:MM.dd.yyyy HH:mm:ss}] Найдено совпадение.\nТип совпадения: <b>'{((TextMatchRule)message.Rule).TextView()}'</b>\nШаблон: <b>'{message.Template}'</b>\n{fromUsername} в @{message.ChatUsername}: <i>{message.Text}</i>",
Text = $"[{message.PublishedDateTime:MM.dd.yyyy HH:mm:ss}] Найдено совпадение.\nТип совпадения: <b>'{message.Rule.TextView()}'</b>\nШаблон: <b>'{message.Template}'</b>\n{fromUsername} в @{message.ChatUsername}: <i>{message.Text}</i>",
ParseMode = ParseMode.Html
};

View File

@ -1,11 +1,12 @@
using Nocr.TextMatcher.Api.Contracts.TextMatches;
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
using Nocr.TextMatcher.Contracts;
namespace Nocr.TelegramClient.AppServices.Matches;
public static class TextMatchExtensions
{
public static string TextView(this TextMatchData textMatch)
public static string TextView(this TextSubscriptionData textMatch)
{
var activeText = textMatch.Active ? "Активна" : "Не активна";
var activeCommandText = textMatch.Active
@ -15,15 +16,15 @@ public static class TextMatchExtensions
return $"[{textMatch.Id}] (@{textMatch.ChatUsername}) {activeText}: '{textMatch.Rule.TextView()}' > '{textMatch.Template}'\n{activeCommandText}\n{deleteCommandText}";
}
public static string TextView(this TextMatchRule rule)
public static string TextView(this TextSubscriptionRule rule)
{
switch (rule)
{
case TextMatchRule.Full:
case TextSubscriptionRule.Full:
return "Полное";
case TextMatchRule.AllWords:
case TextSubscriptionRule.AllWords:
return "Все слова из списка";
case TextMatchRule.AnyWord:
case TextSubscriptionRule.AnyWord:
return "Одно слово из списка";
default:
throw new IndexOutOfRangeException(nameof(rule));

View File

@ -36,13 +36,13 @@ public static class ServiceCollectionExtensions
});
services.Configure<TextMatcherRestEaseOptions>(configuration.GetSection(nameof(TextMatcherRestEaseOptions)));
services.AddScoped<ITextMatchesController>(ctx =>
services.AddScoped<ITextSubscriptionsController>(ctx =>
{
var options = ctx.GetRequiredService<IOptions<TextMatcherRestEaseOptions>>().Value;
var httpClientFactory = ctx.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient(nameof(ITextMatchesController));
var client = httpClientFactory.CreateClient(nameof(ITextSubscriptionsController));
client.BaseAddress = new Uri(options.BasePath);
return RestClient.For<ITextMatchesController>(client);
return RestClient.For<ITextSubscriptionsController>(client);
});
services.AddScoped<IUsersService, UsersService>();

View File

@ -4,7 +4,7 @@ namespace Nocr.TelegramClient.AppServices.Users;
public interface IUsersService
{
public Task<UserData?> GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default);
public Task<UserData> GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default);
public Task<UserData?> GetById(long id, CancellationToken cancellationToken = default);
}

View File

@ -13,7 +13,7 @@ public sealed class UsersService : IUsersService
_usersController = usersController ?? throw new ArgumentNullException(nameof(usersController));
}
public async Task<UserData?> GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default)
public async Task<UserData> GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default)
{
var user = await _usersController.GetByIdentity(UserIdentityType.TelegramId, telegramId.ToString(), cancellationToken);
if (user == null)