Compare commits

..

3 Commits

Author SHA1 Message Date
0effb60e58 Fix unbleeper triggering on bot commands 2026-04-05 22:08:41 +03:00
65a5c57465 fix broken tests 2026-04-05 22:08:17 +03:00
377b615207 Update to .net 10 and bump dependencies 2026-04-05 22:07:22 +03:00
7 changed files with 37 additions and 35 deletions

View File

@ -18,7 +18,7 @@ public class DetectTests : BleepTestsBase
[InlineData("еб*ть—колотить", "*ебать")] [InlineData("еб*ть—колотить", "*ебать")]
[InlineData("Получилась полная х**ня: даже не знаю, что и сказать, б**.", "*херня\n**бля")] [InlineData("Получилась полная х**ня: даже не знаю, что и сказать, б**.", "*херня\n**бля")]
[InlineData("Сергей опять вы**нулся своим знанием тонкостей русского языка; в окно еб*шил стылый ноябрьский ветер. ", "*выебнулся\n**ебашил")] [InlineData("Сергей опять вы**нулся своим знанием тонкостей русского языка; в окно еб*шил стылый ноябрьский ветер. ", "*выебнулся\n**ебашил")]
public async void DetectWordsWithPunctuation(string text, string expected) public async Task DetectWordsWithPunctuation(string text, string expected)
{ {
var unbleep = (await ubl.UnbleepSwears(text)).Replace("\r\n", "\n").Trim(); var unbleep = (await ubl.UnbleepSwears(text)).Replace("\r\n", "\n").Trim();
Assert.Equal(expected, unbleep); Assert.Equal(expected, unbleep);

View File

@ -5,20 +5,20 @@ public class FilterTests : BleepTestsBase
[InlineData("*")] [InlineData("*")]
[InlineData("**#")] [InlineData("**#")]
[InlineData("@*#")] [InlineData("@*#")]
public void IgnoreShortGrawlixesWithoutLetters(string text) public async Task IgnoreShortGrawlixesWithoutLetters(string text)
{ {
if (text.Length < DefaultSettings.Unbleeper.MinAmbiguousWordLength) if (text.Length < DefaultSettings.Unbleeper.MinAmbiguousWordLength)
Assert.Null(ubl.UnbleepSwears(text)); Assert.Null(await ubl.UnbleepSwears(text));
} }
[Theory] [Theory]
[InlineData("*")] [InlineData("*")]
[InlineData("*б")] [InlineData("*б")]
[InlineData("х#")] [InlineData("х#")]
public void IgnoreShortWords(string text) public async Task IgnoreShortWords(string text)
{ {
if (text.Length < DefaultSettings.Unbleeper.MinWordLength) if (text.Length < DefaultSettings.Unbleeper.MinWordLength)
Assert.Null(ubl.UnbleepSwears(text)); Assert.Null(await ubl.UnbleepSwears(text));
} }
[Theory] [Theory]
@ -27,11 +27,11 @@ public class FilterTests : BleepTestsBase
[InlineData("@Artyom по поводу")] [InlineData("@Artyom по поводу")]
[InlineData("@Laima прошу блины!")] [InlineData("@Laima прошу блины!")]
[InlineData("эй админ @harry0xfefecaca верни бота")] [InlineData("эй админ @harry0xfefecaca верни бота")]
public void IgnoreMentions(string text) => Assert.Null(ubl.UnbleepSwears(text)); public async Task IgnoreMentions(string text) => Assert.Null(await ubl.UnbleepSwears(text));
[Theory] [Theory]
[InlineData("x - floor(abs(x)) * sign(x) -- вроде такая формула для frac(x)")] [InlineData("x - floor(abs(x)) * sign(x) -- вроде такая формула для frac(x)")]
public void IgnoresWeirdShit(string text) => Assert.Null(ubl.UnbleepSwears(text)); public async Task IgnoresWeirdShit(string text) => Assert.Null(await ubl.UnbleepSwears(text));
[Theory] [Theory]
[InlineData("/poll")] [InlineData("/poll")]
@ -40,7 +40,7 @@ public class FilterTests : BleepTestsBase
[InlineData("/status@MinecraftServerBot")] [InlineData("/status@MinecraftServerBot")]
[InlineData("/broadcast@MinecraftServerBot пи#*ец вы понастроили тут")] [InlineData("/broadcast@MinecraftServerBot пи#*ец вы понастроили тут")]
[InlineData("/ban@MinecraftServerBot @dirty_johnny86")] [InlineData("/ban@MinecraftServerBot @dirty_johnny86")]
public void IgnoreCommands(string text) => Assert.Null(ubl.UnbleepSwears(text)); public async Task IgnoreCommands(string text) => Assert.Null(await ubl.UnbleepSwears(text));
[Theory] [Theory]
[InlineData("#UEeğitimKarazin")] [InlineData("#UEeğitimKarazin")]
@ -61,7 +61,7 @@ public class FilterTests : BleepTestsBase
[InlineData("#ZodiacKillerStrikesAgain")] [InlineData("#ZodiacKillerStrikesAgain")]
[InlineData("#ThanksObama")] [InlineData("#ThanksObama")]
[InlineData("#BalıkBurcuKızıylaEvlenmek")] [InlineData("#BalıkBurcuKızıylaEvlenmek")]
public void IgnoreHashtags(string text) => Assert.Null(ubl.UnbleepSwears(text)); public async Task IgnoreHashtags(string text) => Assert.Null(await ubl.UnbleepSwears(text));
[Theory] [Theory]
[InlineData("ipetrov@mail.ru")] [InlineData("ipetrov@mail.ru")]
@ -69,6 +69,6 @@ public class FilterTests : BleepTestsBase
[InlineData("ипетров@почта.рф")] [InlineData("ипетров@почта.рф")]
[InlineData("admin@local")] [InlineData("admin@local")]
[InlineData("админ@local")] [InlineData("админ@local")]
public void IgnoreEmails(string text) => Assert.Null(ubl.UnbleepSwears(text)); public async Task IgnoreEmails(string text) => Assert.Null(await ubl.UnbleepSwears(text));
} }

View File

@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit" Version="2.4.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.v3" Version="3.2.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages> <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<UseAppHost>False</UseAppHost> <UseAppHost>False</UseAppHost>
</PropertyGroup> </PropertyGroup>
@ -11,25 +11,16 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Telegram.Bot" Version="19.0.0" /> <PackageReference Include="Telegram.Bot" Version="22.9.6" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Update="appsettings.json"> <Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Update="secrets.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="aasb.cfg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="aasb.cfg.secret.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dict\ObsceneDictionaryRu.txt"> <None Update="dict\ObsceneDictionaryRu.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>

View File

@ -52,11 +52,13 @@ public static class StringEx
=> a == b || !char.IsLetterOrDigit(a) || !char.IsLetterOrDigit(b); => a == b || !char.IsLetterOrDigit(a) || !char.IsLetterOrDigit(b);
static readonly Regex MentionRegex = new Regex("^@[a-zA-Z0-9_]+$", RegexOptions.Compiled); static readonly Regex MentionRegex = new Regex("^@[a-zA-Z0-9_]+$", RegexOptions.Compiled);
static readonly Regex TelegramCommandRegex = new Regex("^/[a-zA-Z0-9_]+@?[a-zA-Z0-9_]*", RegexOptions.Compiled);
static readonly Regex EmailPartRegex = new Regex("^\\w+@\\w+$", RegexOptions.Compiled); static readonly Regex EmailPartRegex = new Regex("^\\w+@\\w+$", RegexOptions.Compiled);
static readonly Regex HashTagRegex = new Regex("^#\\w+$", RegexOptions.Compiled); static readonly Regex HashTagRegex = new Regex("^#\\w+$", RegexOptions.Compiled);
public static bool IsTelegramMention(string word) => MentionRegex.IsMatch(word); public static bool IsTelegramMention(string word) => MentionRegex.IsMatch(word);
public static bool IsTelegramCommand(string word) => TelegramCommandRegex.IsMatch(word);
public static bool IsEmailPart(string word) => EmailPartRegex.IsMatch(word); public static bool IsEmailPart(string word) => EmailPartRegex.IsMatch(word);

View File

@ -54,7 +54,7 @@ public class AntiAntiSwearingBot : IHostedService
TelegramBot = new TelegramBotClient(TelegramSettings.ApiKey, httpClient); TelegramBot = new TelegramBotClient(TelegramSettings.ApiKey, httpClient);
Log.LogInformation("Connecting to Telegram..."); Log.LogInformation("Connecting to Telegram...");
Me = await TelegramBot.GetMeAsync(); Me = await TelegramBot.GetMe(cancellationToken);
Log.LogInformation("Connected to Telegram as @{Username}", Me.Username); Log.LogInformation("Connected to Telegram as @{Username}", Me.Username);
Router = new ChatCommandRouter(Me.Username, AccessCfg); Router = new ChatCommandRouter(Me.Username, AccessCfg);
Router.Register(new LearnCommand(Dict), "learn"); Router.Register(new LearnCommand(Dict), "learn");
@ -74,7 +74,7 @@ public class AntiAntiSwearingBot : IHostedService
public async Task StopAsync(CancellationToken cancellationToken) public async Task StopAsync(CancellationToken cancellationToken)
{ {
await TelegramBot.CloseAsync(); await TelegramBot.Close();
} }
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
@ -97,23 +97,26 @@ public class AntiAntiSwearingBot : IHostedService
if (cmdResponse != null) if (cmdResponse != null)
{ {
await TelegramBot.SendTextMessageAsync( await TelegramBot.SendMessage(
msg.Chat.Id, msg.Chat,
cmdResponse, cmdResponse,
replyToMessageId: msg.MessageId, replyParameters: new ReplyParameters { MessageId = msg.MessageId },
parseMode: ParseMode.MarkdownV2, parseMode: ParseMode.MarkdownV2,
disableNotification: true); disableNotification: true,
cancellationToken: cancellationToken);
} }
} }
else else
{ {
var unbleepResponse = await Unbleeper.UnbleepSwears(msg.Text); var unbleepResponse = await Unbleeper.UnbleepSwears(msg.Text);
if (unbleepResponse != null) if (unbleepResponse != null)
await TelegramBot.SendTextMessageAsync( await TelegramBot.SendMessage(
msg.Chat.Id, msg.Chat,
unbleepResponse, unbleepResponse,
replyToMessageId: msg.MessageId, replyParameters: new ReplyParameters { MessageId = msg.MessageId },
disableNotification: true); disableNotification: true,
cancellationToken: cancellationToken);
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -26,6 +26,9 @@ public class Unbleeper
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
return null; return null;
if (StringEx.IsTelegramCommand(text))
return null;
text = text.Trim(); text = text.Trim();