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:
@@ -1,9 +1,23 @@
|
|||||||
using Kruzya.TelegramBot.Core;
|
using Kruzya.TelegramBot.Core;
|
||||||
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using West.TelegramBot.CodeWatcher.Options;
|
||||||
|
using West.TelegramBot.CodeWatcher.Service;
|
||||||
|
|
||||||
namespace West.TelegramBot.CodeWatcher
|
namespace West.TelegramBot.CodeWatcher
|
||||||
{
|
{
|
||||||
public class CodeWatcher : Module
|
public class CodeWatcher : Module
|
||||||
{
|
{
|
||||||
public CodeWatcher(Core core) : base(core) {}
|
public CodeWatcher(Core core) : base(core) {}
|
||||||
|
|
||||||
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddOption<CodeWatcherMode>();
|
||||||
|
|
||||||
|
services.AddHttpClient<IHasteService, HasteService>(c =>
|
||||||
|
{
|
||||||
|
c.BaseAddress = new Uri(Core.Configuration["HastebinUrl"]!);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,10 @@
|
|||||||
<ProjectReference Include="..\..\Core\Core.csproj" />
|
<ProjectReference Include="..\..\Core\Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Dto\" />
|
||||||
|
<Folder Include="Service\" />
|
||||||
|
<Folder Include="Options\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace West.TelegramBot.CodeWatcher.Dto;
|
||||||
|
|
||||||
|
public class HasteResponse
|
||||||
|
{
|
||||||
|
public HasteResponse(string key)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Key { get; set; }
|
||||||
|
}
|
||||||
@@ -7,16 +7,22 @@ using Kruzya.TelegramBot.Core.Extensions;
|
|||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using West.TelegramBot.CodeWatcher.Options;
|
||||||
|
using West.TelegramBot.CodeWatcher.Service;
|
||||||
|
|
||||||
namespace West.TelegramBot.CodeWatcher;
|
namespace West.TelegramBot.CodeWatcher;
|
||||||
|
|
||||||
public class Handler : BotEventHandler
|
public class Handler : BotEventHandler
|
||||||
{
|
{
|
||||||
private readonly ConcurrentBag<DeleteRequest> _requests;
|
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;
|
_requests = requests;
|
||||||
|
_mode = mode;
|
||||||
|
_haste = haste;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Update(InChat.Public, UpdateFlag.Message | UpdateFlag.EditedMessage)]
|
[Update(InChat.Public, UpdateFlag.Message | UpdateFlag.EditedMessage)]
|
||||||
@@ -35,7 +41,6 @@ public class Handler : BotEventHandler
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!await Bot.CanDeleteMessagesAsync(Chat))
|
if (!await Bot.CanDeleteMessagesAsync(Chat))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -69,14 +74,43 @@ public class Handler : BotEventHandler
|
|||||||
|
|
||||||
if (entityLines.Count >= 20 || entityLines.Any(line => line.Length > 160))
|
if (entityLines.Count >= 20 || entityLines.Any(line => line.Length > 160))
|
||||||
{
|
{
|
||||||
await DeleteAndNotifyAsync(message);
|
await ProcessDetectedCode(message);
|
||||||
return;
|
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);
|
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||||
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
var notifyMsg = await Bot.SendTextMessageAsync(Chat.Id,
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using Kruzya.TelegramBot.Core.Options;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher.Options;
|
||||||
|
|
||||||
|
public class CodeWatcherMode : AbstractOption<CodeWatcherMode, string>
|
||||||
|
{
|
||||||
|
public override string OptionId => "codeWatcherMode";
|
||||||
|
public override string OptionName => "CodeWatcher™ mode";
|
||||||
|
public override OptionType OptionType => OptionType.Select;
|
||||||
|
public override string DefaultValue => "remove";
|
||||||
|
|
||||||
|
public CodeWatcherMode(IOptionProvider optionProvider) : base(optionProvider)
|
||||||
|
{
|
||||||
|
ChoiceList.AddRange(new []
|
||||||
|
{
|
||||||
|
new SelectChoice("disabled", "Disabled", "D"),
|
||||||
|
new SelectChoice("remove", "Remove", "R"),
|
||||||
|
new SelectChoice("upload", "Upload", "U")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System.Net.Http.Json;
|
||||||
|
using West.TelegramBot.CodeWatcher.Dto;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher.Service;
|
||||||
|
|
||||||
|
public class HasteService : IHasteService
|
||||||
|
{
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public HasteService(HttpClient client)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<HasteResponse> PostDocumentAsync(string data)
|
||||||
|
{
|
||||||
|
var response = await (await _client.PostAsync("documents", new StringContent(data)))
|
||||||
|
.Content.ReadFromJsonAsync<HasteResponse>();
|
||||||
|
|
||||||
|
if (response == null)
|
||||||
|
{
|
||||||
|
throw new Exception("Haste returned some shit");
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uri GetHasteUri()
|
||||||
|
{
|
||||||
|
return _client.BaseAddress!;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using West.TelegramBot.CodeWatcher.Dto;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher.Service;
|
||||||
|
|
||||||
|
public interface IHasteService
|
||||||
|
{
|
||||||
|
public Task<HasteResponse> PostDocumentAsync(string data);
|
||||||
|
public Uri GetHasteUri();
|
||||||
|
}
|
||||||
@@ -5,13 +5,10 @@ public static class StringExtension
|
|||||||
// https://stackoverflow.com/a/23408020
|
// https://stackoverflow.com/a/23408020
|
||||||
public static IEnumerable<string> SplitToLines(this string input)
|
public static IEnumerable<string> SplitToLines(this string input)
|
||||||
{
|
{
|
||||||
using (var reader = new StringReader(input))
|
using var reader = new StringReader(input);
|
||||||
{
|
while (reader.ReadLine() is { } line)
|
||||||
string line;
|
|
||||||
while ((line = reader.ReadLine()) != null)
|
|
||||||
{
|
{
|
||||||
yield return line;
|
yield return line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user