Merge pull request #34 from Bubuni-Team/feature/codewatcher/options-and-upload

[code-watcher] Options and auto-upload
This commit is contained in:
Andriy
2023-07-28 18:40:16 +03:00
committed by GitHub
9 changed files with 135 additions and 11 deletions
+1 -1
View File
@@ -184,7 +184,7 @@ public class SettingsHandler : CommonHandler
var keyboardLayout = _options.ToAsyncEnumerable()
.SelectAwait(async x => await GetKeyboardButton(x))
.ToEnumerable()
.Chunk(3);
.Chunk(2);
return new InlineKeyboardMarkup(keyboardLayout);
}
+14
View File
@@ -1,9 +1,23 @@
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
{
public class CodeWatcher : Module
{
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"]!);
});
}
}
}
+6
View File
@@ -12,4 +12,10 @@
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Dto\" />
<Folder Include="Service\" />
<Folder Include="Options\" />
</ItemGroup>
</Project>
+11
View File
@@ -0,0 +1,11 @@
namespace West.TelegramBot.CodeWatcher.Dto;
public class HasteResponse
{
public HasteResponse(string key)
{
Key = key;
}
public string Key { get; set; }
}
+38 -4
View File
@@ -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,
@@ -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();
}
+2 -5
View File
@@ -5,13 +5,10 @@ public static class StringExtension
// https://stackoverflow.com/a/23408020
public static IEnumerable<string> SplitToLines(this string input)
{
using (var reader = new StringReader(input))
{
string line;
while ((line = reader.ReadLine()) != null)
using var reader = new StringReader(input);
while (reader.ReadLine() is { } line)
{
yield return line;
}
}
}
}