mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
|
|
using BotFramework;
|
||
|
|
using BotFramework.Attributes;
|
||
|
|
using BotFramework.Enums;
|
||
|
|
using BotFramework.Utils;
|
||
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
||
|
|
using Telegram.Bot;
|
||
|
|
using Telegram.Bot.Types;
|
||
|
|
using Telegram.Bot.Types.Enums;
|
||
|
|
|
||
|
|
namespace West.TelegramBot.CodeWatcher;
|
||
|
|
|
||
|
|
public class Handler : BotEventHandler
|
||
|
|
{
|
||
|
|
private static readonly string ResponseMessage = new HtmlString()
|
||
|
|
.Text(", не стоит публиковать столь большой код прямо в чат. Вместо этого загрузите его на ")
|
||
|
|
.Url("https://pastebin.com", "https://pastebin.com")
|
||
|
|
.Text(" и отправьте в виде ссылки.")
|
||
|
|
.ToString();
|
||
|
|
|
||
|
|
|
||
|
|
[Message(InChat.Public, MessageFlag.HasText)]
|
||
|
|
public async Task HandleMessage()
|
||
|
|
{
|
||
|
|
var message = RawUpdate.Message!;
|
||
|
|
|
||
|
|
if (message.Entities == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!await Bot.CanDeleteMessagesAsync(Chat))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (var entity in message.Entities)
|
||
|
|
{
|
||
|
|
if (entity.Type is MessageEntityType.Code or MessageEntityType.Pre)
|
||
|
|
{
|
||
|
|
var entityLines = message.Text!
|
||
|
|
.Substring(entity.Offset, entity.Length)
|
||
|
|
.SplitToLines()
|
||
|
|
.ToList();
|
||
|
|
|
||
|
|
if (entityLines.Count >= 10)
|
||
|
|
{
|
||
|
|
await DeleteAndNotifyAsync(message);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (entityLines.Any(line => line.Length > 80))
|
||
|
|
{
|
||
|
|
await DeleteAndNotifyAsync(message);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task DeleteAndNotifyAsync(Message message)
|
||
|
|
{
|
||
|
|
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||
|
|
await Bot.SendTextMessageAsync(Chat.Id,
|
||
|
|
(message.From?.ToHtml() ?? "") + ResponseMessage,
|
||
|
|
ParseMode.Html);
|
||
|
|
}
|
||
|
|
}
|