2020-01-30 20:57:41 +01:00
|
|
|
|
#nullable enable
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using Telegram.Bot;
|
|
|
|
|
using Telegram.Bot.Args;
|
|
|
|
|
using Telegram.Bot.Types;
|
|
|
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
|
|
|
|
|
|
namespace RSSBot {
|
|
|
|
|
public static class Bot {
|
|
|
|
|
public static ITelegramBotClient BotClient;
|
|
|
|
|
public static User BotInfo;
|
|
|
|
|
public static readonly List<RssBotFeed> RssBotFeeds = new List<RssBotFeed>();
|
|
|
|
|
public static Timer JobQueue;
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
private static HashSet<RegexHandler> Handlers;
|
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
private static void Main(string[] args)
|
|
|
|
|
{
|
2020-01-30 20:57:41 +01:00
|
|
|
|
Configuration.Parse();
|
|
|
|
|
BotClient = new TelegramBotClient(Configuration.BotToken);
|
|
|
|
|
try {
|
|
|
|
|
BotInfo = BotClient.GetMeAsync().Result;
|
|
|
|
|
} catch (AggregateException) {
|
|
|
|
|
Logger.Fatal("Login fehlgeschlagen, Bot-Token prüfen!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
|
|
|
|
|
Console.CancelKeyPress += delegate { Save(); };
|
|
|
|
|
|
|
|
|
|
// Read subscribed feeds from Redis
|
|
|
|
|
ReadAllFeeds();
|
|
|
|
|
|
|
|
|
|
// Add handlers
|
|
|
|
|
Handlers = new HashSet<RegexHandler> {
|
|
|
|
|
new RegexHandler($"^/start(?:@{BotInfo.Username})?$", Commands.Welcome),
|
|
|
|
|
new RegexHandler($"^/help(?:@{BotInfo.Username})?$", Commands.Help),
|
|
|
|
|
new RegexHandler($"^/rss(?:@{BotInfo.Username})?$", Commands.Show),
|
2020-03-31 12:52:45 +02:00
|
|
|
|
new RegexHandler($"^/rss(?:@{BotInfo.Username})? (@[A-z0-9_]+)$", Commands.Show),
|
|
|
|
|
new RegexHandler($@"^/rss(?:@{BotInfo.Username})? (-\d+)$", Commands.Show),
|
2020-01-30 20:57:41 +01:00
|
|
|
|
new RegexHandler(
|
|
|
|
|
$"^/show(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
|
|
|
|
|
Commands.ShowAvailableFeeds),
|
|
|
|
|
new RegexHandler(
|
|
|
|
|
$"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
|
|
|
|
|
Commands.Subscribe),
|
|
|
|
|
new RegexHandler(
|
2020-03-31 12:52:45 +02:00
|
|
|
|
$"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (@[A-z0-9_]+)$",
|
|
|
|
|
Commands.Subscribe),
|
|
|
|
|
new RegexHandler(
|
|
|
|
|
$@"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (-\d+)$",
|
2020-01-30 20:57:41 +01:00
|
|
|
|
Commands.Subscribe),
|
|
|
|
|
new RegexHandler(
|
|
|
|
|
$"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
|
|
|
|
|
Commands.Unsubscribe),
|
|
|
|
|
new RegexHandler(
|
2020-03-31 12:52:45 +02:00
|
|
|
|
$"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (@[A-z0-9_]+)$",
|
2020-01-30 20:57:41 +01:00
|
|
|
|
Commands.Unsubscribe),
|
2020-03-31 12:52:45 +02:00
|
|
|
|
new RegexHandler(
|
|
|
|
|
$@"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (-\d+)$",
|
|
|
|
|
Commands.Unsubscribe)
|
2020-01-30 20:57:41 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
JobQueue = new Timer(e => { Commands.Sync(); }, null, TimeSpan.FromSeconds(5),
|
|
|
|
|
TimeSpan.FromMilliseconds(-1));
|
|
|
|
|
|
|
|
|
|
Logger.Info($"Bot gestartet: {BotInfo.FirstName}, AKA {BotInfo.Username} ({BotInfo.Id}).");
|
|
|
|
|
|
|
|
|
|
BotClient.OnMessage += Bot_OnMessage;
|
|
|
|
|
BotClient.StartReceiving();
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
BotClient.StopReceiving();
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
private static void ReadAllFeeds()
|
|
|
|
|
{
|
2020-01-30 20:57:41 +01:00
|
|
|
|
RedisValue[] allFeedUrls = Configuration.Database.SetMembers($"{Configuration.RedisHash}:feeds");
|
|
|
|
|
foreach (RedisValue feedUrl in allFeedUrls) {
|
|
|
|
|
HashSet<long> subs = new HashSet<long>();
|
|
|
|
|
RedisValue[] allSubs = Configuration.Database.SetMembers($"{Configuration.RedisHash}:{feedUrl}:subs");
|
2020-11-05 19:34:45 +01:00
|
|
|
|
foreach (RedisValue sub in allSubs) {
|
|
|
|
|
subs.Add(Convert.ToInt64(sub));
|
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
|
|
string lastEntry = Configuration.Database.HashGet($"{Configuration.RedisHash}:{feedUrl}", "last_entry");
|
|
|
|
|
|
|
|
|
|
RssBotFeed feed = new RssBotFeed(feedUrl, lastEntry, subs);
|
|
|
|
|
RssBotFeeds.Add(feed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
private static void OnProcessExit(object? sender, EventArgs e)
|
|
|
|
|
{
|
2020-01-30 20:57:41 +01:00
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
private static void Bot_OnMessage(object? sender, MessageEventArgs messageEventArgs)
|
|
|
|
|
{
|
2020-03-31 12:52:45 +02:00
|
|
|
|
Message message = messageEventArgs.Message;
|
2020-11-05 19:34:45 +01:00
|
|
|
|
if (message == null || message.Type != MessageType.Text) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
if (!Configuration.Admins.Contains(message.From.Id)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (RegexHandler handler in Handlers.Where(handler => handler.HandleUpdate(message))) {
|
2020-01-30 20:57:41 +01:00
|
|
|
|
handler.ProcessUpdate(message);
|
2020-11-05 19:34:45 +01:00
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
|
public static async void Save()
|
|
|
|
|
{
|
|
|
|
|
if (RssBotFeeds.Count > 0) {
|
2020-12-21 16:21:49 +01:00
|
|
|
|
/* Logger.Info("Speichere Daten..."); */
|
2020-11-05 19:34:45 +01:00
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
|
|
foreach (RssBotFeed feed in RssBotFeeds) {
|
|
|
|
|
string feedKey = $"{Configuration.RedisHash}:{feed.Url}";
|
2020-11-05 19:34:45 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(feed.LastEntry)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
|
|
await Configuration.Database.HashSetAsync(feedKey, "last_entry", feed.LastEntry);
|
2020-11-05 19:34:45 +01:00
|
|
|
|
foreach (long chatId in feed.Subs) {
|
|
|
|
|
await Configuration.Database.SetAddAsync($"{feedKey}:subs", chatId);
|
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
|
|
await Configuration.Database.SetAddAsync($"{Configuration.RedisHash}:feeds", feed.Url);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 14:29:07 +01:00
|
|
|
|
/* Logger.Info("Gespeichert!"); */
|
2020-01-30 20:57:41 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|