mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[code-watcher] handle message edit & message delayed delete service
Message delayed deleter service deals with issue when you need to delete some message with delay without blocking the execution. This is replacement for dumb Task.Delay call. Use of it could be used before as exploit, to stop responding for a long time.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using BotFramework;
|
||||
using System.Collections.Concurrent;
|
||||
using BotFramework;
|
||||
using BotFramework.Attributes;
|
||||
using BotFramework.Enums;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
@@ -10,12 +11,19 @@ namespace West.TelegramBot.CodeWatcher;
|
||||
|
||||
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()
|
||||
{
|
||||
var message = RawUpdate.Message!;
|
||||
var message = RawUpdate.Message ?? RawUpdate.EditedMessage;
|
||||
|
||||
if (message.Entities == null)
|
||||
if (message?.Entities == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -51,12 +59,17 @@ public class Handler : BotEventHandler
|
||||
public async Task DeleteAndNotifyAsync(Message message)
|
||||
{
|
||||
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
||||
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
||||
(message.From?.ToHtml() ?? "") + ", не стоит публиковать столь большой код прямо в чат. " +
|
||||
"Вместо этого загрузите его на https://pastebin.com и отправьте в виде ссылки.",
|
||||
ParseMode.Html);
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
await Bot.DeleteMessageAsync(Chat.Id, notifyMsg.MessageId);
|
||||
|
||||
// TODO: maybe write a wrapper-service around collection to avoid direct interactions with the collection, West, 07.07.2022, 16:53
|
||||
_requests.Add(new DeleteRequest
|
||||
{
|
||||
ChatId = notifyMsg.Chat.Id,
|
||||
MessageId = notifyMsg.MessageId,
|
||||
DeleteAt = DateTime.Now.AddSeconds(30)
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user