mirror of
https://github.com/Jetsparrow/antiantiswearingbot.git
synced 2026-01-20 23:16:08 +03:00
128 lines
3.7 KiB
C#
128 lines
3.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using AntiAntiSwearingBot.Commands;
|
|
using Telegram.Bot;
|
|
using Telegram.Bot.Args;
|
|
using Telegram.Bot.Types;
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
namespace AntiAntiSwearingBot
|
|
{
|
|
public class AntiAntiSwearingBot : IDisposable
|
|
{
|
|
Config Config { get; }
|
|
SearchDictionary Dict { get; }
|
|
|
|
public AntiAntiSwearingBot(Config cfg, SearchDictionary dict)
|
|
{
|
|
Config = cfg;
|
|
Dict = dict;
|
|
BleepedSwearsRegex = new Regex(cfg.BleepedSwearsRegex, RegexOptions.Compiled);
|
|
NonWordRegex = new Regex("\\W", RegexOptions.Compiled);
|
|
MentionRegex = new Regex("@[a-zA-Z0-9_]+", RegexOptions.Compiled);
|
|
}
|
|
|
|
TelegramBotClient Client { get; set; }
|
|
ChatCommandRouter Router { get; set; }
|
|
User Me { get; set; }
|
|
|
|
public async Task Init()
|
|
{
|
|
var httpProxy = new WebProxy($"{Config.Proxy.Url}:{Config.Proxy.Port}")
|
|
{
|
|
Credentials = new NetworkCredential(Config.Proxy.Login, Config.Proxy.Password)
|
|
};
|
|
|
|
Client = new TelegramBotClient(Config.ApiKey, httpProxy);
|
|
Me = await Client.GetMeAsync();
|
|
Router = new ChatCommandRouter(Me.Username);
|
|
Router.Add(new LearnCommand(Dict), "learn");
|
|
Router.Add(new UnlearnCommand(Dict), "unlearn");
|
|
|
|
Client.OnMessage += BotOnMessageReceived;
|
|
Client.StartReceiving();
|
|
}
|
|
|
|
public async Task Stop()
|
|
{
|
|
Dict.Save();
|
|
Dispose();
|
|
}
|
|
|
|
#region service
|
|
|
|
Regex BleepedSwearsRegex { get; }
|
|
Regex NonWordRegex { get; }
|
|
Regex MentionRegex { get; }
|
|
|
|
string UnbleepSwears(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return null;
|
|
|
|
var words = BleepedSwearsRegex.Matches(text)
|
|
.Select(m => m.Value)
|
|
.Where(m => NonWordRegex.IsMatch(m))
|
|
.Where(m => !MentionRegex.IsMatch(m))
|
|
.ToArray();
|
|
|
|
if (words.Any())
|
|
{
|
|
var response = new StringBuilder();
|
|
for (int i = 0; i < words.Length; ++i)
|
|
{
|
|
var m = Dict.Match(words[i]);
|
|
response.AppendLine(new string('*', i + 1) + m.Word + new string('?', m.Distance));
|
|
}
|
|
return response.ToString();
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
void BotOnMessageReceived(object sender, MessageEventArgs args)
|
|
{
|
|
var msg = args.Message;
|
|
if (msg == null || msg.Type != MessageType.Text)
|
|
return;
|
|
|
|
string commandResponse = null;
|
|
|
|
try { commandResponse = Router.Execute(sender, args); }
|
|
catch { }
|
|
|
|
if (commandResponse != null)
|
|
{
|
|
Client.SendTextMessageAsync(
|
|
args.Message.Chat.Id,
|
|
commandResponse,
|
|
replyToMessageId: args.Message.MessageId);
|
|
}
|
|
else
|
|
{
|
|
var unbleepResponse = UnbleepSwears(msg.Text);
|
|
if (unbleepResponse != null)
|
|
Client.SendTextMessageAsync(
|
|
args.Message.Chat.Id,
|
|
unbleepResponse,
|
|
replyToMessageId: args.Message.MessageId);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
|
|
public void Dispose()
|
|
{
|
|
Client.StopReceiving();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|