🚧 Anti-spam system based on entities

This commit is contained in:
2020-03-03 14:11:51 +04:00
parent 1441c345b2
commit 8f8898e901
9 changed files with 148 additions and 3 deletions
+1 -1
View File
@@ -165,7 +165,7 @@ namespace Kruzya.TelegramBot.Core
appDomain.AssemblyResolve += delegate(object? sender, ResolveEventArgs args) appDomain.AssemblyResolve += delegate(object? sender, ResolveEventArgs args)
{ {
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyPath = Path.Combine(basePath, new AssemblyName(args.Name).Name + ".dll"); var assemblyPath = Path.Combine(basePath, new AssemblyName(args.Name).Name + ".dll");
if (!File.Exists(assemblyPath)) if (!File.Exists(assemblyPath))
{ {
return null; return null;
+4
View File
@@ -1,4 +1,5 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Kruzya.TelegramBot.Core.Cache;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Kruzya.TelegramBot.Core.Extensions namespace Kruzya.TelegramBot.Core.Extensions
@@ -7,5 +8,8 @@ namespace Kruzya.TelegramBot.Core.Extensions
{ {
public static IServiceCollection AddQueue<T>(this IServiceCollection collection) public static IServiceCollection AddQueue<T>(this IServiceCollection collection)
=> collection.AddSingleton<ConcurrentQueue<T>>(); => collection.AddSingleton<ConcurrentQueue<T>>();
public static IServiceCollection AddMemoryCache<TKey, TValue>(this IServiceCollection collection)
=> collection.AddSingleton<MemoryCache<TKey, TValue>>();
} }
} }
+1 -1
View File
@@ -8,7 +8,7 @@ namespace Kruzya.TelegramBot.Core.Extensions
public static string ToHtml(this User user) public static string ToHtml(this User user)
{ {
var text = new StringBuilder(); var text = new StringBuilder();
text.Append("<a href=\"tg://user?id={member.Id}\">"); text.Append($"<a href=\"tg://user?id={user.Id}\">");
if (!string.IsNullOrWhiteSpace(user.Username)) if (!string.IsNullOrWhiteSpace(user.Username))
text.Append(user.Username); text.Append(user.Username);
else else
+7
View File
@@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RichSiteSummary", "modules\
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests.csproj", "{5C62CA70-F270-4029-953E-458623C31A3B}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests.csproj", "{5C62CA70-F270-4029-953E-458623C31A3B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UrlLimitations", "modules\UrlLimitations\UrlLimitations.csproj", "{B82E3339-3B34-4600-8C59-C5A3640B1982}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -32,9 +34,14 @@ Global
{5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.Build.0 = Release|Any CPU {5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.Build.0 = Release|Any CPU
{B82E3339-3B34-4600-8C59-C5A3640B1982}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B82E3339-3B34-4600-8C59-C5A3640B1982}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B82E3339-3B34-4600-8C59-C5A3640B1982}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B82E3339-3B34-4600-8C59-C5A3640B1982}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{C50D64C3-204B-4B58-927B-C8D759787252} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {C50D64C3-204B-4B58-927B-C8D759787252} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{B82E3339-3B34-4600-8C59-C5A3640B1982} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal
+19
View File
@@ -0,0 +1,19 @@
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.UrlLimitations
{
public struct ChatMember
{
public long Chat;
public long User;
public static ChatMember From(long chatId, long userId)
{
return new ChatMember()
{
Chat = chatId,
User = userId
};
}
}
}
+58
View File
@@ -0,0 +1,58 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace Kruzya.TelegramBot.UrlLimitations
{
public class MessageHandler : BotEventHandler
{
protected ILogger Logger;
protected MemoryCache<ChatMember, UInt16> ChatMembersCache;
public MessageHandler(ILogger<MessageHandler> logger, MemoryCache<ChatMember, UInt16> chatMembersCache)
{
Logger = logger;
ChatMembersCache = chatMembersCache;
}
[TextMessage(InChat.Public)]
public async Task OnMessageReceived()
{
Logger.LogDebug($"Message received from {From.Id} in {Chat.Id}");
UInt16 messageCount;
var chatMember = ChatMember.From(Chat.Id, From.Id);
if (!ChatMembersCache.TryGetValue(chatMember, out messageCount))
{
return;
}
if (messageCount > 10)
{
ChatMembersCache.SetValue(chatMember, 0, 0);
return;
}
messageCount++;
ChatMembersCache.SetValue(chatMember, messageCount);
var message = RawUpdate.Message;
if (message.Entities.Count(e => e.Type == MessageEntityType.Url || e.Type == MessageEntityType.TextLink) >
0)
{
var targetChat = new ChatId(Chat.Id);
await Bot.KickChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1));
await Bot.DeleteMessageAsync(targetChat, message.MessageId);
await Bot.SendTextMessageAsync(targetChat,
$"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html);
}
}
}
}
+19
View File
@@ -0,0 +1,19 @@
using System;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;
namespace Kruzya.TelegramBot.UrlLimitations
{
public class UrlLimitations : Module
{
public UrlLimitations(Core.Core core) : base(core)
{
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache<ChatMember, UInt16>();
}
}
}
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>TelegramBot.UrlLimitations</AssemblyName>
<RootNamespace>Kruzya.TelegramBot.UrlLimitations</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
</Project>
+25
View File
@@ -0,0 +1,25 @@
using System;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Cache;
using Telegram.Bot.Types.Enums;
namespace Kruzya.TelegramBot.UrlLimitations
{
public class UserJoinHandler : BotEventHandler
{
protected MemoryCache<ChatMember, UInt16> ChatMembersCache;
public UserJoinHandler(MemoryCache<ChatMember, UInt16> chatMembersCache)
{
ChatMembersCache = chatMembersCache;
}
[Message(MessageType.ChatMembersAdded, InChat.Public)]
public void OnChatMembersAdded()
{
ChatMembersCache.SetValue(ChatMember.From(Chat.Id, From.Id), 0);
}
}
}