Merge upstream

This commit is contained in:
Akamaru 2020-04-01 14:52:17 +02:00
commit c728f62946
3 changed files with 88 additions and 67 deletions

34
Bot.cs
View File

@ -40,7 +40,8 @@ namespace RSSBot {
new RegexHandler($"^/start(?:@{BotInfo.Username})?$", Commands.Welcome), new RegexHandler($"^/start(?:@{BotInfo.Username})?$", Commands.Welcome),
new RegexHandler($"^/help(?:@{BotInfo.Username})?$", Commands.Help), new RegexHandler($"^/help(?:@{BotInfo.Username})?$", Commands.Help),
new RegexHandler($"^/rss(?:@{BotInfo.Username})?$", Commands.Show), 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( new RegexHandler(
$"^/show(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$", $"^/show(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
Commands.ShowAvailableFeeds), 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]))+)$", $"^/sub(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
Commands.Subscribe), Commands.Subscribe),
new RegexHandler( 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), Commands.Subscribe),
new RegexHandler( new RegexHandler(
$"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$", $"^/del(?:@{BotInfo.Username})? (http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$",
Commands.Unsubscribe), Commands.Unsubscribe),
new RegexHandler( 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), 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), JobQueue = new Timer(e => { Commands.Sync(); }, null, TimeSpan.FromSeconds(5),
@ -75,9 +82,7 @@ namespace RSSBot {
foreach (RedisValue feedUrl in allFeedUrls) { foreach (RedisValue feedUrl in allFeedUrls) {
HashSet<long> subs = new HashSet<long>(); HashSet<long> subs = new HashSet<long>();
RedisValue[] allSubs = Configuration.Database.SetMembers($"{Configuration.RedisHash}:{feedUrl}:subs"); RedisValue[] allSubs = Configuration.Database.SetMembers($"{Configuration.RedisHash}:{feedUrl}:subs");
foreach (RedisValue sub in allSubs) { foreach (RedisValue sub in allSubs) subs.Add(Convert.ToInt64(sub));
subs.Add(Convert.ToInt64(sub));
}
string lastEntry = Configuration.Database.HashGet($"{Configuration.RedisHash}:{feedUrl}", "last_entry"); 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) { 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 (message == null || message.Type != MessageType.Text) return;
if (!Configuration.Admins.Contains(message.From.Id)) { if (!Configuration.Admins.Contains(message.From.Id)) return;
return;
}
foreach (RegexHandler handler in Handlers.Where(handler => handler.HandleUpdate(message))) { foreach (RegexHandler handler in Handlers.Where(handler => handler.HandleUpdate(message)))
handler.ProcessUpdate(message); handler.ProcessUpdate(message);
} }
}
public static async void Save() { public static async void Save() {
if (RssBotFeeds.Count > 0) { if (RssBotFeeds.Count > 0) Logger.Info(/* "Speichere Daten..." */);
/* Logger.Info("Speichere Daten..."); */
}
foreach (RssBotFeed feed in RssBotFeeds) { foreach (RssBotFeed feed in RssBotFeeds) {
string feedKey = $"{Configuration.RedisHash}:{feed.Url}"; string feedKey = $"{Configuration.RedisHash}:{feed.Url}";
if (string.IsNullOrWhiteSpace(feed.LastEntry)) continue; if (string.IsNullOrWhiteSpace(feed.LastEntry)) continue;
await Configuration.Database.HashSetAsync(feedKey, "last_entry", feed.LastEntry); await Configuration.Database.HashSetAsync(feedKey, "last_entry", feed.LastEntry);
foreach (long chatId in feed.Subs) { foreach (long chatId in feed.Subs) await Configuration.Database.SetAddAsync($"{feedKey}:subs", chatId);
await Configuration.Database.SetAddAsync($"{feedKey}:subs", chatId);
}
await Configuration.Database.SetAddAsync($"{Configuration.RedisHash}:feeds", feed.Url); await Configuration.Database.SetAddAsync($"{Configuration.RedisHash}:feeds", feed.Url);
} }

View File

@ -35,17 +35,25 @@ namespace RSSBot {
} }
public static async void Subscribe(Message message, GroupCollection args) { public static async void Subscribe(Message message, GroupCollection args) {
var url = args[1].Value; string url = args[1].Value;
var chatId = message.Chat.Id; long chatId = message.Chat.Id;
var feed = new RssBotFeed(url); RssBotFeed feed = new RssBotFeed(url);
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
if (args.Count > 2) { if (args.Count > 2) {
var chatName = args[2].Value;
if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
Chat chatInfo; Chat chatInfo;
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 { try {
chatInfo = await Bot.BotClient.GetChatAsync(chatName); chatInfo = await Bot.BotClient.GetChatAsync(chatName);
} catch { } catch {
@ -91,16 +99,24 @@ namespace RSSBot {
} }
public static async void Unsubscribe(Message message, GroupCollection args) { public static async void Unsubscribe(Message message, GroupCollection args) {
var url = args[1].Value; string url = args[1].Value;
var chatId = message.Chat.Id; long chatId = message.Chat.Id;
RssBotFeed feed = Bot.RssBotFeeds RssBotFeed feed = Bot.RssBotFeeds
.FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower())); .FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower()));
if (args.Count > 2) { if (args.Count > 2) {
var chatName = args[2].Value;
if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
Chat chatInfo; Chat chatInfo;
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 { try {
chatInfo = await Bot.BotClient.GetChatAsync(chatName); chatInfo = await Bot.BotClient.GetChatAsync(chatName);
} catch { } catch {
@ -130,16 +146,23 @@ namespace RSSBot {
} }
public static async void Show(Message message, GroupCollection args) { public static async void Show(Message message, GroupCollection args) {
var chatId = message.Chat.Id; long chatId = message.Chat.Id;
var chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title; string chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title;
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
if (args.Count > 1) { if (args.Count > 1) {
var chatName = args[1].Value;
if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
Chat chatInfo; Chat chatInfo;
string chatName = args[1].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 { try {
chatInfo = await Bot.BotClient.GetChatAsync(chatName); chatInfo = await Bot.BotClient.GetChatAsync(chatName);
} catch { } catch {
@ -157,14 +180,14 @@ namespace RSSBot {
} }
} }
var feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList(); List<RssBotFeed> feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList();
var text = new StringBuilder(); StringBuilder text = new StringBuilder();
if (feeds.Count < 1) { if (feeds.Count < 1) {
text.Append("❌ Keine Feeds abonniert."); text.Append("❌ Keine Feeds abonniert.");
} else { } else {
text.Append($"<strong>{HttpUtility.HtmlEncode(chatTitle)}</strong> hat abonniert:\n"); text.Append($"<strong>{HttpUtility.HtmlEncode(chatTitle)}</strong> hat abonniert:\n");
for (var i = 0; i < feeds.Count; i++) text.Append($"<strong>{i + 1}</strong>) {feeds[i].Url}\n"); for (int i = 0; i < feeds.Count; i++) text.Append($"<strong>{i + 1}</strong>) {feeds[i].Url}\n");
} }
await Bot.BotClient.SendTextMessageAsync(message.Chat, text.ToString(), ParseMode.Html, true); await Bot.BotClient.SendTextMessageAsync(message.Chat, text.ToString(), ParseMode.Html, true);
@ -172,7 +195,7 @@ namespace RSSBot {
public static async void Sync() { public static async void Sync() {
Logger.Info("================================"); Logger.Info("================================");
var hadEntries = false; bool hadEntries = false;
foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) { foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) {
Logger.Info(feed.Url); Logger.Info(feed.Url);
try { try {
@ -193,11 +216,11 @@ namespace RSSBot {
: $"{feed.NewEntries.Count} neue Beiträge"); : $"{feed.NewEntries.Count} neue Beiträge");
foreach (FeedItem entry in feed.NewEntries) { foreach (FeedItem entry in feed.NewEntries) {
var postTitle = "Kein Titel"; string postTitle = "Kein Titel";
if (!string.IsNullOrWhiteSpace(entry.Title)) postTitle = Utils.StripHtml(entry.Title); if (!string.IsNullOrWhiteSpace(entry.Title)) postTitle = Utils.StripHtml(entry.Title);
var postLink = feed.MainLink; string postLink = feed.MainLink;
var linkName = postLink; string linkName = postLink;
if (!string.IsNullOrWhiteSpace(entry.Link)) { if (!string.IsNullOrWhiteSpace(entry.Link)) {
postLink = entry.Link; postLink = entry.Link;
// FeedProxy URLs // FeedProxy URLs
@ -207,20 +230,20 @@ namespace RSSBot {
} }
// Remove "www." // Remove "www."
var index = linkName.IndexOf("www.", StringComparison.Ordinal); int index = linkName.IndexOf("www.", StringComparison.Ordinal);
if (index > -1) linkName = linkName.Remove(index, 4); if (index > -1) linkName = linkName.Remove(index, 4);
var content = ""; string content = "";
if (!string.IsNullOrWhiteSpace(entry.Content)) if (!string.IsNullOrWhiteSpace(entry.Content))
content = Utils.ProcessContent(entry.Content); content = Utils.ProcessContent(entry.Content);
else if (!string.IsNullOrWhiteSpace(entry.Description)) else if (!string.IsNullOrWhiteSpace(entry.Description))
content = Utils.ProcessContent(entry.Description); content = Utils.ProcessContent(entry.Description);
var text = $"<b>[#RSS] {postTitle}</b>\n{content}"; string text = $"<b>[#RSS] {postTitle}</b>\n{content}";
text += $"\n<a href=\"{postLink}\">Auf {linkName} ansehen.</a>"; text += $"\n<a href=\"{postLink}\">Auf {linkName} ansehen.</a>";
// Send // Send
foreach (var chatId in feed.Subs.ToList()) foreach (long chatId in feed.Subs.ToList())
try { try {
await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true); await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true);
} catch (ApiRequestException e) { } catch (ApiRequestException e) {
@ -244,7 +267,7 @@ namespace RSSBot {
} }
public static async void ShowAvailableFeeds(Message message, GroupCollection args) { public static async void ShowAvailableFeeds(Message message, GroupCollection args) {
var url = args[1].Value; string url = args[1].Value;
IEnumerable<HtmlFeedLink> feeds; IEnumerable<HtmlFeedLink> feeds;
try { try {
feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url); feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url);
@ -253,13 +276,13 @@ namespace RSSBot {
return; return;
} }
var htmlFeedLinks = feeds.ToList(); List<HtmlFeedLink> htmlFeedLinks = feeds.ToList();
if (htmlFeedLinks.Count == 0) { if (htmlFeedLinks.Count == 0) {
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden."); await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden.");
return; return;
} }
var text = htmlFeedLinks.Aggregate("Feeds gefunden:\n", string text = htmlFeedLinks.Aggregate("Feeds gefunden:\n",
(current, feedLink) => (current, feedLink) =>
current + $"* <a href=\"{feedLink.Url}\">{Utils.StripHtml(feedLink.Title)}</a>\n"); current + $"* <a href=\"{feedLink.Url}\">{Utils.StripHtml(feedLink.Title)}</a>\n");

View File

@ -4,7 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64;linux-arm</RuntimeIdentifiers> <RuntimeIdentifiers>win-x64;osx-x64;linux-x64;linux-arm</RuntimeIdentifiers>
<Version>1.0.0</Version> <Version>1.0.1</Version>
<!-- <PublishReadyToRun>true</PublishReadyToRun>--> <!-- <PublishReadyToRun>true</PublishReadyToRun>-->
<!-- <PublishSingleFile>true</PublishSingleFile>--> <!-- <PublishSingleFile>true</PublishSingleFile>-->
<SelfContained>false</SelfContained> <SelfContained>false</SelfContained>