nazarovsa/message_link (#5)
Reviewed-on: #5 Co-authored-by: Sergey Nazarov <insight.appdev@gmail.com> Co-committed-by: Sergey Nazarov <insight.appdev@gmail.com>
This commit is contained in:
parent
ba8c94d962
commit
43dbf5b724
@ -7,11 +7,12 @@
|
||||
<InsightTelegramBotVersion>0.16.0</InsightTelegramBotVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="FormatWith" Version="3.0.1" />
|
||||
<PackageVersion Include="Insight.Localizer" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Label="Nocr">
|
||||
<PackageVersion Include="Nocr.TextMatcher.Api.Contracts" Version="0.4.25"/>
|
||||
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.4.25"/>
|
||||
<PackageVersion Include="Nocr.TextMatcher.Api.Contracts" Version="0.5.9" />
|
||||
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.5.9" />
|
||||
<PackageVersion Include="Nocr.Users.Api.Contracts" Version="0.4.23" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Label="Rebus">
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using Insight.TelegramBot;
|
||||
using Insight.TelegramBot.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using Insight.Localizer;
|
||||
using Insight.TelegramBot;
|
||||
using Insight.TelegramBot.Handling.Handlers;
|
||||
using Insight.TelegramBot.Models;
|
||||
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Insight.TelegramBot;
|
||||
using Insight.TelegramBot.Handling.Handlers;
|
||||
using Insight.TelegramBot.Models;
|
||||
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
|
||||
@ -7,7 +6,6 @@ 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;
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
using Insight.TelegramBot;
|
||||
using Insight.Localizer;
|
||||
using Insight.TelegramBot.Handling.Handlers;
|
||||
using Insight.TelegramBot.Models;
|
||||
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;
|
||||
|
||||
@ -13,16 +12,21 @@ namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscriptionsMessage
|
||||
|
||||
public class SubscriptionsMessageHandler : IMatchingUpdateHandler<SubscriptionsMessageMatcher>
|
||||
{
|
||||
private readonly ILocalizer _localizer;
|
||||
private readonly IMessageDispatcherQueue _messageQueue;
|
||||
private readonly IUsersService _usersService;
|
||||
private readonly ITextSubscriptionsController _textSubscriptionsController;
|
||||
|
||||
public SubscriptionsMessageHandler(IMessageDispatcherQueue messageQueue, IUsersService usersService,
|
||||
public SubscriptionsMessageHandler(ILocalizer localizer,
|
||||
IMessageDispatcherQueue messageQueue,
|
||||
IUsersService usersService,
|
||||
ITextSubscriptionsController textSubscriptionsController)
|
||||
{
|
||||
_localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
|
||||
_messageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue));
|
||||
_usersService = usersService ?? throw new ArgumentNullException(nameof(usersService));
|
||||
_textSubscriptionsController = textSubscriptionsController ?? throw new ArgumentNullException(nameof(textSubscriptionsController));
|
||||
_textSubscriptionsController = textSubscriptionsController ??
|
||||
throw new ArgumentNullException(nameof(textSubscriptionsController));
|
||||
}
|
||||
|
||||
public async Task Handle(Update update, CancellationToken cancellationToken = default)
|
||||
@ -36,8 +40,7 @@ public class SubscriptionsMessageHandler : IMatchingUpdateHandler<SubscriptionsM
|
||||
{
|
||||
var textMessage = new TextMessage(telegramId)
|
||||
{
|
||||
// TODO: Add localizer
|
||||
Text = subscription.TextView(),
|
||||
Text = subscription.TextView(_localizer),
|
||||
ParseMode = ParseMode.Html
|
||||
};
|
||||
|
||||
|
||||
@ -0,0 +1,68 @@
|
||||
namespace Nocr.TelegramClient.AppServices.Links;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly struct PublicTelegramMessageLink
|
||||
{
|
||||
// https://t.me/baraholka_tbi/1186860
|
||||
// tg://resolve?domain=baraholka_tbi&post=1186860
|
||||
// t.me/<username>/<id>?single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
|
||||
|
||||
/// <summary>
|
||||
/// Username чата или канала
|
||||
/// </summary>
|
||||
public string Username { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор сообщения
|
||||
/// </summary>
|
||||
public long MessageId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор треда
|
||||
/// </summary>
|
||||
public long? ThreadId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Отображать в web-preview одно изображения из поста или несколько
|
||||
/// </summary>
|
||||
public bool SingleView { get; }
|
||||
|
||||
/// <summary>
|
||||
/// https://
|
||||
/// </summary>
|
||||
public string WebLink { get; }
|
||||
|
||||
/// <summary>
|
||||
/// tg://
|
||||
/// </summary>
|
||||
public string TelegramLink { get; }
|
||||
|
||||
public PublicTelegramMessageLink(bool singleView, string username, long messageId, long? threadId = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(username));
|
||||
}
|
||||
|
||||
SingleView = singleView;
|
||||
Username = username;
|
||||
MessageId = messageId;
|
||||
ThreadId = threadId;
|
||||
(WebLink, TelegramLink) = BuildLinks();
|
||||
}
|
||||
|
||||
private (string, string) BuildLinks()
|
||||
{
|
||||
var webLink = ThreadId.HasValue
|
||||
? $"https://t.me/{Username}/{ThreadId.Value}/{MessageId}?{(SingleView ? "single" : string.Empty)}"
|
||||
: $"https://t.me/{Username}/{MessageId}?{(SingleView ? "single" : string.Empty)}";
|
||||
|
||||
var telegramLink = ThreadId.HasValue
|
||||
? $"tg://resolve?domain={Username}&post={MessageId}{(SingleView ? "&single" : string.Empty)}&thread={ThreadId.Value}"
|
||||
: $"tg://resolve?domain={Username}&post={MessageId}{(SingleView ? "&single" : string.Empty)}";
|
||||
|
||||
return (webLink, telegramLink);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
using Insight.TelegramBot;
|
||||
using FormatWith;
|
||||
using Insight.Localizer;
|
||||
using Insight.TelegramBot.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
|
||||
using Nocr.TelegramClient.AppServices.Links;
|
||||
using Nocr.TelegramClient.AppServices.Users;
|
||||
using Nocr.TextMatcher.Api.Contracts.TextMatches;
|
||||
using Nocr.TextMatcher.Async.Api.Contracts;
|
||||
using Nocr.Users.Api.Contracts.Users;
|
||||
using Rebus.Handlers;
|
||||
@ -13,22 +15,29 @@ namespace Nocr.TelegramClient.AppServices.Matches.Handlers;
|
||||
public class TextMatchMatchedHandler : IHandleMessages<TextSubscriptionMatched>
|
||||
{
|
||||
private readonly ILogger<TextMatchMatchedHandler> _logger;
|
||||
private readonly IBot _bot;
|
||||
private readonly ILocalizer _localizer;
|
||||
private readonly IMessageDispatcherQueue _messageDispatcherQueue;
|
||||
private readonly IUsersService _usersService;
|
||||
|
||||
public TextMatchMatchedHandler(ILogger<TextMatchMatchedHandler> logger, IBot bot, IUsersService usersService)
|
||||
public TextMatchMatchedHandler(ILogger<TextMatchMatchedHandler> logger, ILocalizer localizer,
|
||||
IMessageDispatcherQueue messageDispatcherQueue, IUsersService usersService)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_bot = bot ?? throw new ArgumentNullException(nameof(bot));
|
||||
_localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
|
||||
_messageDispatcherQueue =
|
||||
messageDispatcherQueue ?? throw new ArgumentNullException(nameof(messageDispatcherQueue));
|
||||
_usersService = usersService ?? throw new ArgumentNullException(nameof(usersService));
|
||||
}
|
||||
|
||||
public async Task Handle(TextSubscriptionMatched message)
|
||||
{
|
||||
// TODO: Set from user context
|
||||
Localizer.CurrentCulture = "ru-ru";
|
||||
var user = await _usersService.GetById(message.SubscriptionUserId);
|
||||
if (user == null)
|
||||
{
|
||||
_logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found", message.SubscriptionUserId,
|
||||
_logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found",
|
||||
message.SubscriptionUserId,
|
||||
message.SubscriptionId, message.Id);
|
||||
return;
|
||||
}
|
||||
@ -41,13 +50,25 @@ public class TextMatchMatchedHandler : IHandleMessages<TextSubscriptionMatched>
|
||||
return;
|
||||
}
|
||||
|
||||
var link = new PublicTelegramMessageLink(false, message.ChatUsername, message.MessageId);
|
||||
var fromUsername = string.IsNullOrWhiteSpace(message.From) ? "anonymous" : message.From;
|
||||
var text = _localizer.Get("messages", "TextSubscriptionMatched")
|
||||
.FormatWith(new
|
||||
{
|
||||
SubscriptionRule = message.Rule.TextView(_localizer),
|
||||
message.Template,
|
||||
FromUsername = fromUsername,
|
||||
message.ChatUsername,
|
||||
MessageText = message.Text,
|
||||
Link = link.WebLink
|
||||
});
|
||||
|
||||
|
||||
var textMessage = new TextMessage(long.Parse(identity.Identity))
|
||||
{
|
||||
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>",
|
||||
Text = text,
|
||||
ParseMode = ParseMode.Html
|
||||
};
|
||||
|
||||
await _bot.SendMessageAsync(textMessage);
|
||||
_messageDispatcherQueue.Enqueue(textMessage);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Nocr.TextMatcher.Api.Contracts.TextMatches;
|
||||
using FormatWith;
|
||||
using Insight.Localizer;
|
||||
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
|
||||
using Nocr.TextMatcher.Contracts;
|
||||
|
||||
@ -6,26 +7,39 @@ namespace Nocr.TelegramClient.AppServices.Matches;
|
||||
|
||||
public static class TextMatchExtensions
|
||||
{
|
||||
public static string TextView(this TextSubscriptionData textMatch)
|
||||
public static string TextView(this TextSubscriptionData textSubscription, ILocalizer localizer)
|
||||
{
|
||||
var activeText = textMatch.Active ? "Активна" : "Не активна";
|
||||
var activeCommandText = textMatch.Active
|
||||
? $"Деактивировать: /deactivate_{textMatch.Id}"
|
||||
: $"Активировать: /activate_{textMatch.Id}";
|
||||
var deleteCommandText = $"Удалить: /delete_subscription_{textMatch.Id}";
|
||||
return $"[{textMatch.Id}] (@{textMatch.ChatUsername}) {activeText}: '{textMatch.Rule.TextView()}' > '{textMatch.Template}'\n{activeCommandText}\n{deleteCommandText}";
|
||||
var activeText = textSubscription.Active
|
||||
? localizer.Get(nameof(TextSubscriptionData), "Active")
|
||||
: localizer.Get(nameof(TextSubscriptionData), "Inactive");
|
||||
var activeCommandText = textSubscription.Active
|
||||
? GetFormattedCommand("Deactivate", textSubscription.Id)
|
||||
: GetFormattedCommand("Activate", textSubscription.Id);
|
||||
|
||||
var deleteCommandText = GetFormattedCommand("Delete", textSubscription.Id);
|
||||
return
|
||||
$"[{textSubscription.Id}] (@{textSubscription.ChatUsername}) {activeText}: '{textSubscription.Rule.TextView(localizer)}' > '{textSubscription.Template}'\n{activeCommandText}\n{deleteCommandText}";
|
||||
|
||||
string GetFormattedCommand(string key, long subscriptionId)
|
||||
{
|
||||
return localizer.Get(nameof(TextSubscriptionData), key)
|
||||
.FormatWith(new
|
||||
{
|
||||
SubscriptionId = subscriptionId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static string TextView(this TextSubscriptionRule rule)
|
||||
public static string TextView(this TextSubscriptionRule rule, ILocalizer localizer)
|
||||
{
|
||||
switch (rule)
|
||||
{
|
||||
case TextSubscriptionRule.Full:
|
||||
return "Полное";
|
||||
return localizer.Get(nameof(TextSubscriptionRule), nameof(TextSubscriptionRule.Full));
|
||||
case TextSubscriptionRule.AllWords:
|
||||
return "Все слова из списка";
|
||||
return localizer.Get(nameof(TextSubscriptionRule), nameof(TextSubscriptionRule.AllWords));
|
||||
case TextSubscriptionRule.AnyWord:
|
||||
return "Одно слово из списка";
|
||||
return localizer.Get(nameof(TextSubscriptionRule), nameof(TextSubscriptionRule.AnyWord));
|
||||
default:
|
||||
throw new IndexOutOfRangeException(nameof(rule));
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FormatWith" />
|
||||
<PackageReference Include="Insight.Localizer" />
|
||||
<PackageReference Include="Insight.TelegramBot" />
|
||||
<PackageReference Include="Insight.TelegramBot.Handling" />
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Nocr.TelegramClient.AppServices.Bots;
|
||||
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
|
||||
using Nocr.TelegramClient.AppServices.Matches;
|
||||
using Nocr.TelegramClient.AppServices.Matches.Handlers;
|
||||
using Nocr.TelegramClient.AppServices.Options;
|
||||
using Nocr.TelegramClient.AppServices.Users;
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"Active": "Активна",
|
||||
"Inactive": "Не активна",
|
||||
"Activate": "Активировать: /activate_subscription_{SubscriptionId}",
|
||||
"Deactivate": "Деактивировать: /deactivate_subscription_{SubscriptionId}",
|
||||
"Delete": "Удалить: /delete_subscription_{SubscriptionId}"
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"Full": "Полное",
|
||||
"AllWords": "Все слова из списка",
|
||||
"AnyWord": "Одно слово из списка"
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
{
|
||||
"StartMessage": "Привет! Я, Nocr 🤖!"
|
||||
"StartMessage": "Привет! Я, Nocr 🤖!",
|
||||
"TextSubscriptionMatched": "Тип совпадения: <b>{SubscriptionRule}</b>\nШаблон: <b>{Template}</b>\n{FromUsername} в @{ChatUsername}:\n<i>{MessageText}</i>\n\n{Link}"
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user