Fix #3 Subscribe to channels by their ID

This commit is contained in:
Andreas Bielawski 2020-03-31 12:52:45 +02:00
parent 262d77d1e1
commit a44d4b27a3
No known key found for this signature in database
GPG Key ID: D2073645DC2C3DE5
3 changed files with 67 additions and 46 deletions

34
Bot.cs
View File

@ -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<long> subs = new HashSet<long>();
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);
}

View File

@ -42,10 +42,18 @@ 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;
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 {
@ -97,10 +105,18 @@ 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;
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 {
@ -135,11 +151,18 @@ 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);
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 {

View File

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