mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[code-watcher] Add CodeWatcherMode option.
There are three option values:
- Disabled: module does nothing (wow).
- Remove: module removes matching messages and sends temporary message
to instruct user what to do.
- Upload: module uploads detected code to configured Haste Server.
This commit is contained in:
@@ -7,16 +7,22 @@ using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using West.TelegramBot.CodeWatcher.Options;
|
||||
using West.TelegramBot.CodeWatcher.Service;
|
||||
|
||||
namespace West.TelegramBot.CodeWatcher;
|
||||
|
||||
public class Handler : BotEventHandler
|
||||
{
|
||||
private readonly ConcurrentBag<DeleteRequest> _requests;
|
||||
private readonly CodeWatcherMode _mode;
|
||||
private readonly IHasteService _haste;
|
||||
|
||||
public Handler(ConcurrentBag<DeleteRequest> requests)
|
||||
public Handler(ConcurrentBag<DeleteRequest> requests, CodeWatcherMode mode, IHasteService haste)
|
||||
{
|
||||
_requests = requests;
|
||||
_mode = mode;
|
||||
_haste = haste;
|
||||
}
|
||||
|
||||
[Update(InChat.Public, UpdateFlag.Message | UpdateFlag.EditedMessage)]
|
||||
@@ -35,7 +41,6 @@ public class Handler : BotEventHandler
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!await Bot.CanDeleteMessagesAsync(Chat))
|
||||
{
|
||||
return;
|
||||
@@ -69,14 +74,43 @@ public class Handler : BotEventHandler
|
||||
|
||||
if (entityLines.Count >= 20 || entityLines.Any(line => line.Length > 160))
|
||||
{
|
||||
await DeleteAndNotifyAsync(message);
|
||||
await ProcessDetectedCode(message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteAndNotifyAsync(Message message)
|
||||
public async Task ProcessDetectedCode(Message message)
|
||||
{
|
||||
switch (await _mode.GetValueAsync(Chat.Id))
|
||||
{
|
||||
case "disabled":
|
||||
return;
|
||||
|
||||
case "remove":
|
||||
await RemoveDetectedCode(message);
|
||||
break;
|
||||
|
||||
case "upload":
|
||||
await UploadDetectedCode(message);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UploadDetectedCode(Message message)
|
||||
{
|
||||
var resp = await _haste.PostDocumentAsync(message.Text!);
|
||||
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat.Id,
|
||||
$"{message.From?.ToHtml()}, you posted something long and pre-escaped, so I decided to upload it to our paste service for you.\n" +
|
||||
$"Here is the link: {_haste.GetHasteUri() + resp.Key}",
|
||||
parseMode: ParseMode.Html);
|
||||
}
|
||||
|
||||
public async Task RemoveDetectedCode(Message message)
|
||||
{
|
||||
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
||||
|
||||
Reference in New Issue
Block a user