From 24ee7f9052cf728e9406fdc3d9858d959b128df4 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Fri, 13 Mar 2020 22:43:40 +0100 Subject: [PATCH 1/3] More replacements --- Utils.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Utils.cs b/Utils.cs index 1bd4159..8f3645a 100644 --- a/Utils.cs +++ b/Utils.cs @@ -15,7 +15,9 @@ namespace RSSBot { private static string CleanRss(string input) { string[] replacements = { + "[←]", "[…]", + "[...]", "[bilder]", "[boerse]", "[mehr]", @@ -41,6 +43,8 @@ namespace RSSBot { "(more…)", "View On WordPress", "Continue reading →", + "» weiterlesen", + "(Feed generated with FetchRSS)", "(RSS generated with FetchRss)", "-- Delivered by Feed43 service", "Meldung bei www.tagesschau.de lesen" From 262d77d1e13b8f1240669aaac8fc144be4903a13 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 31 Mar 2020 11:20:12 +0200 Subject: [PATCH 2/3] Replace "var" with their types --- Commands.cs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Commands.cs b/Commands.cs index 1e84c89..c970f5f 100644 --- a/Commands.cs +++ b/Commands.cs @@ -35,14 +35,14 @@ namespace RSSBot { } public static async void Subscribe(Message message, GroupCollection args) { - var url = args[1].Value; - var chatId = message.Chat.Id; - var feed = new RssBotFeed(url); + string url = args[1].Value; + long chatId = message.Chat.Id; + RssBotFeed feed = new RssBotFeed(url); await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); if (args.Count > 2) { - var chatName = args[2].Value; + string chatName = args[2].Value; if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; Chat chatInfo; @@ -91,13 +91,13 @@ namespace RSSBot { } public static async void Unsubscribe(Message message, GroupCollection args) { - var url = args[1].Value; - var chatId = message.Chat.Id; + string url = args[1].Value; + long chatId = message.Chat.Id; RssBotFeed feed = Bot.RssBotFeeds .FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower())); if (args.Count > 2) { - var chatName = args[2].Value; + string chatName = args[2].Value; if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; Chat chatInfo; @@ -130,12 +130,12 @@ namespace RSSBot { } public static async void Show(Message message, GroupCollection args) { - var chatId = message.Chat.Id; - var chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title; + long chatId = message.Chat.Id; + string chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title; await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); if (args.Count > 1) { - var chatName = args[1].Value; + string chatName = args[1].Value; if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; Chat chatInfo; @@ -157,14 +157,14 @@ namespace RSSBot { } } - var feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList(); + List feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList(); - var text = new StringBuilder(); + StringBuilder text = new StringBuilder(); if (feeds.Count < 1) { text.Append("❌ Keine Feeds abonniert."); } else { text.Append($"{HttpUtility.HtmlEncode(chatTitle)} hat abonniert:\n"); - for (var i = 0; i < feeds.Count; i++) text.Append($"{i + 1}) {feeds[i].Url}\n"); + for (int i = 0; i < feeds.Count; i++) text.Append($"{i + 1}) {feeds[i].Url}\n"); } await Bot.BotClient.SendTextMessageAsync(message.Chat, text.ToString(), ParseMode.Html, true); @@ -172,7 +172,7 @@ namespace RSSBot { public static async void Sync() { Logger.Info("================================"); - var hadEntries = false; + bool hadEntries = false; foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) { Logger.Info(feed.Url); try { @@ -193,11 +193,11 @@ namespace RSSBot { : $"{feed.NewEntries.Count} neue Beiträge"); foreach (FeedItem entry in feed.NewEntries) { - var postTitle = "Kein Titel"; + string postTitle = "Kein Titel"; if (!string.IsNullOrWhiteSpace(entry.Title)) postTitle = Utils.StripHtml(entry.Title); - var postLink = feed.MainLink; - var linkName = postLink; + string postLink = feed.MainLink; + string linkName = postLink; if (!string.IsNullOrWhiteSpace(entry.Link)) { postLink = entry.Link; // FeedProxy URLs @@ -207,20 +207,20 @@ namespace RSSBot { } // Remove "www." - var index = linkName.IndexOf("www.", StringComparison.Ordinal); + int index = linkName.IndexOf("www.", StringComparison.Ordinal); if (index > -1) linkName = linkName.Remove(index, 4); - var content = ""; + string content = ""; if (!string.IsNullOrWhiteSpace(entry.Content)) content = Utils.ProcessContent(entry.Content); else if (!string.IsNullOrWhiteSpace(entry.Description)) content = Utils.ProcessContent(entry.Description); - var text = $"{postTitle}\n{feed.Title}\n{content}"; + string text = $"{postTitle}\n{feed.Title}\n{content}"; text += $"\nWeiterlesen auf {linkName}"; // Send - foreach (var chatId in feed.Subs.ToList()) + foreach (long chatId in feed.Subs.ToList()) try { await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true); } catch (ApiRequestException e) { @@ -244,7 +244,7 @@ namespace RSSBot { } public static async void ShowAvailableFeeds(Message message, GroupCollection args) { - var url = args[1].Value; + string url = args[1].Value; IEnumerable feeds; try { feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url); @@ -253,13 +253,13 @@ namespace RSSBot { return; } - var htmlFeedLinks = feeds.ToList(); + List htmlFeedLinks = feeds.ToList(); if (htmlFeedLinks.Count == 0) { await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden."); return; } - var text = htmlFeedLinks.Aggregate("Feeds gefunden:\n", + string text = htmlFeedLinks.Aggregate("Feeds gefunden:\n", (current, feedLink) => current + $"* {Utils.StripHtml(feedLink.Title)}\n"); From a44d4b27a3d4ebaff4fa2b97ed68bfbd252b3395 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 31 Mar 2020 12:52:45 +0200 Subject: [PATCH 3/3] Fix #3 Subscribe to channels by their ID --- Bot.cs | 34 +++++++++++------------ Commands.cs | 77 +++++++++++++++++++++++++++++++++------------------ RSSBot.csproj | 2 +- 3 files changed, 67 insertions(+), 46 deletions(-) diff --git a/Bot.cs b/Bot.cs index 1168d2e..0e837c0 100644 --- a/Bot.cs +++ b/Bot.cs @@ -40,7 +40,8 @@ namespace RSSBot { new RegexHandler($"^/start(?:@{BotInfo.Username})?$", Commands.Welcome), new RegexHandler($"^/help(?:@{BotInfo.Username})?$", Commands.Help), new RegexHandler($"^/rss(?:@{BotInfo.Username})?$", Commands.Show), - new RegexHandler($"^/rss(?:@{BotInfo.Username})? (@?[A-z0-9_]+)$", Commands.Show), + new RegexHandler($"^/rss(?:@{BotInfo.Username})? (@[A-z0-9_]+)$", Commands.Show), + new RegexHandler($@"^/rss(?:@{BotInfo.Username})? (-\d+)$", Commands.Show), new RegexHandler( $"^/show(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$", Commands.ShowAvailableFeeds), @@ -48,14 +49,20 @@ namespace RSSBot { $"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$", Commands.Subscribe), new RegexHandler( - $"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (@?[A-z0-9_]+)$$", + $"^/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+)$", 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( - $"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (@?[A-z0-9_]+)$$", + $"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (@[A-z0-9_]+)$", Commands.Unsubscribe), + new RegexHandler( + $@"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+) (-\d+)$", + Commands.Unsubscribe) }; JobQueue = new Timer(e => { Commands.Sync(); }, null, TimeSpan.FromSeconds(5), @@ -75,9 +82,7 @@ namespace RSSBot { foreach (RedisValue feedUrl in allFeedUrls) { HashSet subs = new HashSet(); RedisValue[] allSubs = Configuration.Database.SetMembers($"{Configuration.RedisHash}:{feedUrl}:subs"); - foreach (RedisValue sub in allSubs) { - subs.Add(Convert.ToInt64(sub)); - } + foreach (RedisValue sub in allSubs) subs.Add(Convert.ToInt64(sub)); string lastEntry = Configuration.Database.HashGet($"{Configuration.RedisHash}:{feedUrl}", "last_entry"); @@ -91,30 +96,23 @@ namespace RSSBot { } private static void Bot_OnMessage(object? sender, MessageEventArgs messageEventArgs) { - var message = messageEventArgs.Message; + Message message = messageEventArgs.Message; if (message == null || message.Type != MessageType.Text) return; - if (!Configuration.Admins.Contains(message.From.Id)) { - return; - } + if (!Configuration.Admins.Contains(message.From.Id)) return; - foreach (RegexHandler handler in Handlers.Where(handler => handler.HandleUpdate(message))) { + foreach (RegexHandler handler in Handlers.Where(handler => handler.HandleUpdate(message))) handler.ProcessUpdate(message); - } } public static async void Save() { - if (RssBotFeeds.Count > 0) { - Logger.Info("Speichere Daten..."); - } + if (RssBotFeeds.Count > 0) Logger.Info("Speichere Daten..."); foreach (RssBotFeed feed in RssBotFeeds) { string feedKey = $"{Configuration.RedisHash}:{feed.Url}"; if (string.IsNullOrWhiteSpace(feed.LastEntry)) continue; await Configuration.Database.HashSetAsync(feedKey, "last_entry", feed.LastEntry); - foreach (long chatId in feed.Subs) { - await Configuration.Database.SetAddAsync($"{feedKey}:subs", chatId); - } + foreach (long chatId in feed.Subs) await Configuration.Database.SetAddAsync($"{feedKey}:subs", chatId); await Configuration.Database.SetAddAsync($"{Configuration.RedisHash}:feeds", feed.Url); } diff --git a/Commands.cs b/Commands.cs index c970f5f..d355c60 100644 --- a/Commands.cs +++ b/Commands.cs @@ -42,16 +42,24 @@ namespace RSSBot { await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); if (args.Count > 2) { - string chatName = args[2].Value; - if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; - Chat chatInfo; - try { - chatInfo = await Bot.BotClient.GetChatAsync(chatName); - } catch { - await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); - return; - } + string chatName = args[2].Value; + bool isId = long.TryParse(chatName, out chatId); + + if (isId) + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatId); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } + else + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatName); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } chatId = chatInfo.Id; @@ -97,16 +105,24 @@ namespace RSSBot { .FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower())); if (args.Count > 2) { - string chatName = args[2].Value; - if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; - Chat chatInfo; - try { - chatInfo = await Bot.BotClient.GetChatAsync(chatName); - } catch { - await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); - return; - } + string chatName = args[2].Value; + bool isId = long.TryParse(chatName, out chatId); + + if (isId) + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatId); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } + else + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatName); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } chatId = chatInfo.Id; @@ -135,17 +151,24 @@ namespace RSSBot { await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); if (args.Count > 1) { - string chatName = args[1].Value; - if (!chatName.StartsWith("@")) chatName = $"@{chatName}"; - Chat chatInfo; + string chatName = args[1].Value; + bool isId = long.TryParse(chatName, out chatId); - try { - chatInfo = await Bot.BotClient.GetChatAsync(chatName); - } catch { - await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); - return; - } + if (isId) + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatId); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } + else + try { + chatInfo = await Bot.BotClient.GetChatAsync(chatName); + } catch { + await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht."); + return; + } chatId = chatInfo.Id; chatTitle = chatInfo.Title; diff --git a/RSSBot.csproj b/RSSBot.csproj index cb2138e..0b71896 100644 --- a/RSSBot.csproj +++ b/RSSBot.csproj @@ -4,7 +4,7 @@ Exe netcoreapp3.1 win-x64;osx-x64;linux-x64;linux-arm - 1.0.0 + 1.0.1 false