mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[core] move message auto-delete service from code-watcher to core
This commit is contained in:
@@ -1,17 +1,9 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Kruzya.TelegramBot.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Kruzya.TelegramBot.Core;
|
||||
|
||||
namespace West.TelegramBot.CodeWatcher
|
||||
{
|
||||
public class CodeWatcher : Module
|
||||
{
|
||||
public CodeWatcher(Core core) : base(core) {}
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<ConcurrentBag<DeleteRequest>>();
|
||||
services.AddHostedService<DeleteService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using BotFramework;
|
||||
using BotFramework.Attributes;
|
||||
using BotFramework.Enums;
|
||||
using Kruzya.TelegramBot.Core.AutoDelete;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
Reference in New Issue
Block a user