mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Merge pull request #17 from Bubuni-Team/refactor/code-watcher
[code-watcher] Refactorings
This commit is contained in:
@@ -1,9 +1,17 @@
|
|||||||
using Kruzya.TelegramBot.Core;
|
using System.Collections.Concurrent;
|
||||||
|
using Kruzya.TelegramBot.Core;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace West.TelegramBot.CodeWatcher
|
namespace West.TelegramBot.CodeWatcher
|
||||||
{
|
{
|
||||||
public class CodeWatcher : Module
|
public class CodeWatcher : Module
|
||||||
{
|
{
|
||||||
public CodeWatcher(Core core) : base(core) {}
|
public CodeWatcher(Core core) : base(core) {}
|
||||||
|
|
||||||
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<ConcurrentBag<DeleteRequest>>();
|
||||||
|
services.AddHostedService<DeleteService>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher;
|
||||||
|
|
||||||
|
public class DeleteRequest
|
||||||
|
{
|
||||||
|
public ChatId ChatId { get; set; }
|
||||||
|
public int MessageId { get; set; }
|
||||||
|
public DateTime DeleteAt { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
|
using BotFramework.Abstractions;
|
||||||
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Exceptions;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher;
|
||||||
|
|
||||||
|
public class DeleteService : AbstractTimedHostedService
|
||||||
|
{
|
||||||
|
private readonly ConcurrentBag<DeleteRequest> _deleteRequests;
|
||||||
|
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(10);
|
||||||
|
|
||||||
|
public DeleteService(ILogger<DeleteService> logger, IBotInstance bot, ConcurrentBag<DeleteRequest> deleteRequests) : base(logger,
|
||||||
|
bot)
|
||||||
|
{
|
||||||
|
_deleteRequests = deleteRequests;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task OnRun()
|
||||||
|
{
|
||||||
|
var now = DateTime.Now;
|
||||||
|
var skippedRequestList = new List<DeleteRequest>();
|
||||||
|
|
||||||
|
_logger.LogDebug("Spinning up the delete requests loop. Request count: {RequestCount}", _deleteRequests.Count);
|
||||||
|
for (var i = 0; i < _deleteRequests.Count; i++)
|
||||||
|
{
|
||||||
|
if (!_deleteRequests.TryTake(out var request))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Nothing to take, breaking...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.DeleteAt < now)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _bot.BotClient.DeleteMessageAsync(request.ChatId, request.MessageId);
|
||||||
|
}
|
||||||
|
catch (ApiRequestException e)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error deleting message, code - {ErrorCode}, msg - {ErrorMessage}", e.ErrorCode,
|
||||||
|
e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Skipping {MessageId}", request.MessageId);
|
||||||
|
skippedRequestList.Add(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var request in skippedRequestList)
|
||||||
|
{
|
||||||
|
_deleteRequests.Add(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using BotFramework;
|
using System.Collections.Concurrent;
|
||||||
|
using BotFramework;
|
||||||
using BotFramework.Attributes;
|
using BotFramework.Attributes;
|
||||||
using BotFramework.Enums;
|
using BotFramework.Enums;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
@@ -10,12 +11,19 @@ namespace West.TelegramBot.CodeWatcher;
|
|||||||
|
|
||||||
public class Handler : BotEventHandler
|
public class Handler : BotEventHandler
|
||||||
{
|
{
|
||||||
[Message(InChat.Public, MessageFlag.HasText)]
|
private readonly ConcurrentBag<DeleteRequest> _requests;
|
||||||
|
|
||||||
|
public Handler(ConcurrentBag<DeleteRequest> requests)
|
||||||
|
{
|
||||||
|
_requests = requests;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Update(InChat.Public, UpdateFlag.Message | UpdateFlag.EditedMessage)]
|
||||||
public async Task HandleMessage()
|
public async Task HandleMessage()
|
||||||
{
|
{
|
||||||
var message = RawUpdate.Message!;
|
var message = RawUpdate.Message ?? RawUpdate.EditedMessage;
|
||||||
|
|
||||||
if (message.Entities == null)
|
if (message?.Entities == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -34,6 +42,11 @@ public class Handler : BotEventHandler
|
|||||||
.SplitToLines()
|
.SplitToLines()
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
|
if (entityLines.Count < 2)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (entityLines.Count >= 20 || entityLines.Any(line => line.Length > 160))
|
if (entityLines.Count >= 20 || entityLines.Any(line => line.Length > 160))
|
||||||
{
|
{
|
||||||
await DeleteAndNotifyAsync(message);
|
await DeleteAndNotifyAsync(message);
|
||||||
@@ -46,12 +59,17 @@ public class Handler : BotEventHandler
|
|||||||
public async Task DeleteAndNotifyAsync(Message message)
|
public async Task DeleteAndNotifyAsync(Message message)
|
||||||
{
|
{
|
||||||
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||||
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
||||||
(message.From?.ToHtml() ?? "") + ", не стоит публиковать столь большой код прямо в чат. " +
|
(message.From?.ToHtml() ?? "") + ", не стоит публиковать столь большой код прямо в чат. " +
|
||||||
"Вместо этого загрузите его на https://pastebin.com и отправьте в виде ссылки.",
|
"Вместо этого загрузите его на https://pastebin.com и отправьте в виде ссылки.",
|
||||||
ParseMode.Html);
|
ParseMode.Html);
|
||||||
|
|
||||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
// TODO: maybe write a wrapper-service around collection to avoid direct interactions with the collection, West, 07.07.2022, 16:53
|
||||||
await Bot.DeleteMessageAsync(Chat.Id, notifyMsg.MessageId);
|
_requests.Add(new DeleteRequest
|
||||||
|
{
|
||||||
|
ChatId = notifyMsg.Chat.Id,
|
||||||
|
MessageId = notifyMsg.MessageId,
|
||||||
|
DeleteAt = DateTime.Now.AddSeconds(30)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user