2020-03-13 14:29:07 +01:00
|
|
|
using System;
|
2020-01-30 20:57:41 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-11-05 19:23:42 +01:00
|
|
|
using System.Net.Http;
|
2020-03-07 01:11:58 +01:00
|
|
|
using System.Text;
|
2020-01-30 20:57:41 +01:00
|
|
|
using System.Text.RegularExpressions;
|
2020-11-05 19:23:42 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-01-30 20:57:41 +01:00
|
|
|
using System.Web;
|
|
|
|
using CodeHollow.FeedReader;
|
|
|
|
using NLog;
|
|
|
|
using Telegram.Bot.Exceptions;
|
|
|
|
using Telegram.Bot.Types;
|
|
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
|
|
|
|
namespace RSSBot {
|
|
|
|
public static class Commands {
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Welcome(Message message, GroupCollection matches)
|
|
|
|
{
|
2020-01-30 20:57:41 +01:00
|
|
|
await Bot.BotClient.SendTextMessageAsync(
|
|
|
|
message.Chat,
|
2020-03-13 14:29:07 +01:00
|
|
|
"<b>Willkommen bei RikoBot!</b>\nSende /help, um zu starten.",
|
2020-01-30 20:57:41 +01:00
|
|
|
ParseMode.Html
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Help(Message message, GroupCollection matches)
|
|
|
|
{
|
2020-01-30 20:57:41 +01:00
|
|
|
await Bot.BotClient.SendTextMessageAsync(
|
|
|
|
message.Chat,
|
|
|
|
"<b>/rss</b> <i>[Chat]</i>: Abonnierte Feeds anzeigen\n" +
|
|
|
|
"<b>/sub</b> <i>Feed-URL</i> <i>[Chat]</i>: Feed abonnieren\n" +
|
|
|
|
"<b>/del</b> <i>Feed-URL</i> <i>[Chat]</i>: Feed löschen\n" +
|
|
|
|
"<b>/show</b> <i>Feed-URL</i> <i>[Chat]</i>: Feeds auf dieser Seite anzeigen\n" +
|
|
|
|
"<i>[Chat]</i> ist ein optionales Argument mit dem @Kanalnamen.",
|
|
|
|
ParseMode.Html
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Subscribe(Message message, GroupCollection args)
|
|
|
|
{
|
2020-03-31 11:20:12 +02:00
|
|
|
string url = args[1].Value;
|
|
|
|
long chatId = message.Chat.Id;
|
|
|
|
RssBotFeed feed = new RssBotFeed(url);
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
|
|
|
|
|
|
|
|
if (args.Count > 2) {
|
|
|
|
Chat chatInfo;
|
2020-03-31 12:52:45 +02:00
|
|
|
string chatName = args[2].Value;
|
|
|
|
bool isId = long.TryParse(chatName, out chatId);
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
if (isId) {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatId);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
} else {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatName);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
chatId = chatInfo.Id;
|
|
|
|
|
|
|
|
if (!await Utils.IsBotAdmin(chatId)) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat,
|
|
|
|
"❌ Du musst den Bot als Administrator zu diesem Kanal hinzufügen.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await feed.Check();
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(
|
|
|
|
message.Chat,
|
|
|
|
"❌ Kein gültiger RSS-Feed."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have the feed
|
|
|
|
RssBotFeed existingFeed = Bot.RssBotFeeds
|
|
|
|
.FirstOrDefault(x => x.Url.ToLower().Equals(feed.Url.ToLower()));
|
2020-11-05 19:34:45 +01:00
|
|
|
if (existingFeed == null) {
|
2020-01-30 20:57:41 +01:00
|
|
|
Bot.RssBotFeeds.Add(feed);
|
2020-11-05 19:34:45 +01:00
|
|
|
} else {
|
2020-01-30 20:57:41 +01:00
|
|
|
feed = existingFeed;
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
// Check if chat already subscribed
|
|
|
|
if (feed.Subs.Contains(chatId)) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "✅ Dieser Feed wurde bereits abonniert.");
|
|
|
|
} else {
|
|
|
|
feed.Subs.Add(chatId);
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "✅ Feed abonniert!");
|
|
|
|
Bot.Save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Unsubscribe(Message message, GroupCollection args)
|
|
|
|
{
|
2020-03-31 11:20:12 +02:00
|
|
|
string url = args[1].Value;
|
|
|
|
long chatId = message.Chat.Id;
|
2020-01-30 20:57:41 +01:00
|
|
|
RssBotFeed feed = Bot.RssBotFeeds
|
|
|
|
.FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower()));
|
|
|
|
|
|
|
|
if (args.Count > 2) {
|
|
|
|
Chat chatInfo;
|
2020-03-31 12:52:45 +02:00
|
|
|
string chatName = args[2].Value;
|
|
|
|
bool isId = long.TryParse(chatName, out chatId);
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
if (isId) {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatId);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
} else {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatName);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
chatId = chatInfo.Id;
|
|
|
|
|
|
|
|
if (!await Utils.IsBotAdmin(chatId)) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat,
|
|
|
|
"❌ Du musst den Bot als Administrator zu diesem Kanal hinzufügen.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feed == null || !feed.Subs.Contains(chatId)) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Feed wurde nicht abonniert.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
feed.Cleanup(chatId);
|
2020-11-05 19:34:45 +01:00
|
|
|
if (feed.Subs.Count == 0) {
|
|
|
|
Bot.RssBotFeeds.Remove(feed);
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "✅ Feed deabonniert!");
|
|
|
|
Bot.Save();
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Show(Message message, GroupCollection args)
|
|
|
|
{
|
2020-03-31 11:20:12 +02:00
|
|
|
long chatId = message.Chat.Id;
|
|
|
|
string chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title;
|
2020-01-30 20:57:41 +01:00
|
|
|
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
|
2020-03-07 01:11:58 +01:00
|
|
|
|
2020-01-30 20:57:41 +01:00
|
|
|
if (args.Count > 1) {
|
|
|
|
Chat chatInfo;
|
2020-03-31 12:52:45 +02:00
|
|
|
string chatName = args[1].Value;
|
|
|
|
bool isId = long.TryParse(chatName, out chatId);
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
if (isId) {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatId);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
} else {
|
2020-03-31 12:52:45 +02:00
|
|
|
try {
|
|
|
|
chatInfo = await Bot.BotClient.GetChatAsync(chatName);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Dieser Kanal existiert nicht.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
chatId = chatInfo.Id;
|
|
|
|
chatTitle = chatInfo.Title;
|
|
|
|
|
|
|
|
if (!await Utils.IsBotAdmin(chatId)) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat,
|
|
|
|
"❌ Du musst den Bot als Administrator zu diesem Kanal hinzufügen.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
List<RssBotFeed> feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList();
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
StringBuilder text = new StringBuilder();
|
2020-01-30 20:57:41 +01:00
|
|
|
if (feeds.Count < 1) {
|
2020-03-07 01:11:58 +01:00
|
|
|
text.Append("❌ Keine Feeds abonniert.");
|
2020-01-30 20:57:41 +01:00
|
|
|
} else {
|
2020-03-07 01:11:58 +01:00
|
|
|
text.Append($"<strong>{HttpUtility.HtmlEncode(chatTitle)}</strong> hat abonniert:\n");
|
2020-11-05 19:34:45 +01:00
|
|
|
for (int i = 0; i < feeds.Count; i++) {
|
|
|
|
text.Append($"<strong>{i + 1}</strong>) {feeds[i].Url}\n");
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
}
|
2020-03-07 01:11:58 +01:00
|
|
|
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, text.ToString(), ParseMode.Html, true);
|
2020-01-30 20:57:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void Sync()
|
|
|
|
{
|
2020-03-07 01:11:58 +01:00
|
|
|
Logger.Info("================================");
|
2020-03-31 11:20:12 +02:00
|
|
|
bool hadEntries = false;
|
2020-01-30 20:57:41 +01:00
|
|
|
foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) {
|
|
|
|
Logger.Info(feed.Url);
|
|
|
|
try {
|
|
|
|
await feed.Check();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Logger.Warn($"FEHLER: {e.Message}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feed.NewEntries.Count == 0) {
|
2020-03-13 14:29:07 +01:00
|
|
|
/* Logger.Info("Keine neuen Beiträge."); */
|
2020-01-30 20:57:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hadEntries = true;
|
|
|
|
Logger.Info(feed.NewEntries.Count == 1
|
|
|
|
? "1 neuer Beitrag"
|
|
|
|
: $"{feed.NewEntries.Count} neue Beiträge");
|
|
|
|
|
|
|
|
foreach (FeedItem entry in feed.NewEntries) {
|
2020-03-31 11:20:12 +02:00
|
|
|
string postTitle = "Kein Titel";
|
2020-11-05 19:34:45 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(entry.Title)) {
|
|
|
|
postTitle = Utils.StripHtml(entry.Title);
|
2020-12-20 23:43:17 +01:00
|
|
|
postTitle = Utils.EscapeHtml(postTitle);
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
string postLink = feed.MainLink;
|
|
|
|
string linkName = postLink;
|
2020-01-30 20:57:41 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(entry.Link)) {
|
|
|
|
postLink = entry.Link;
|
|
|
|
// FeedProxy URLs
|
|
|
|
GroupCollection feedProxy =
|
2020-03-13 22:40:42 +01:00
|
|
|
Utils.ReturnMatches(postLink, "^https?://feedproxy.google.com/~r/(.+?)/.*");
|
2020-01-30 20:57:41 +01:00
|
|
|
linkName = feedProxy.Count > 1 ? feedProxy[1].Value : new Uri(postLink).Host;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "www."
|
2020-03-31 11:20:12 +02:00
|
|
|
int index = linkName.IndexOf("www.", StringComparison.Ordinal);
|
2020-11-05 19:34:45 +01:00
|
|
|
if (index > -1) {
|
|
|
|
linkName = linkName.Remove(index, 4);
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
string content = "";
|
2020-11-05 19:34:45 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(entry.Content)) {
|
2020-03-13 13:57:10 +01:00
|
|
|
content = Utils.ProcessContent(entry.Content);
|
2020-11-05 19:34:45 +01:00
|
|
|
} else if (!string.IsNullOrWhiteSpace(entry.Description)) {
|
2020-01-30 20:57:41 +01:00
|
|
|
content = Utils.ProcessContent(entry.Description);
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-04-01 14:52:17 +02:00
|
|
|
string text = $"<b>[#RSS] {postTitle}</b>\n{content}";
|
2020-03-13 14:29:07 +01:00
|
|
|
text += $"\n<a href=\"{postLink}\">Auf {linkName} ansehen.</a>";
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
// Send
|
2020-11-05 19:34:45 +01:00
|
|
|
foreach (long chatId in feed.Subs.ToList()) {
|
2020-11-05 19:23:42 +01:00
|
|
|
await SendFinishedMessage(chatId, text, feed);
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:29:07 +01:00
|
|
|
/* Logger.Info("Nächster Check in 60 Sekunden"); */
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
if (hadEntries) {
|
|
|
|
Bot.Save();
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
Bot.JobQueue.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(-1));
|
|
|
|
}
|
2020-11-05 19:23:42 +01:00
|
|
|
|
|
|
|
private static async Task SendFinishedMessage(long chatId, string text, RssBotFeed feed, int waitTime = 10)
|
|
|
|
{
|
2020-11-05 19:34:45 +01:00
|
|
|
try {
|
2020-11-05 19:23:42 +01:00
|
|
|
await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true);
|
|
|
|
Thread.Sleep(1000);
|
2020-11-05 19:34:45 +01:00
|
|
|
} catch (ApiRequestException e) {
|
|
|
|
if (e.ErrorCode.Equals(403)) {
|
2020-11-05 19:23:42 +01:00
|
|
|
Logger.Warn(e.Message);
|
|
|
|
feed.Cleanup(chatId);
|
|
|
|
if (feed.Subs.Count == 0) // was last subscriber
|
2020-11-05 19:34:45 +01:00
|
|
|
{
|
2020-11-05 19:23:42 +01:00
|
|
|
Bot.RssBotFeeds.Remove(feed);
|
2020-11-05 19:34:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-05 19:23:42 +01:00
|
|
|
Logger.Error($"{e.ErrorCode}: {e.Message}");
|
|
|
|
}
|
2020-11-05 19:34:45 +01:00
|
|
|
} catch (HttpRequestException) // likely 429
|
2020-11-05 19:23:42 +01:00
|
|
|
{
|
2020-11-05 19:34:45 +01:00
|
|
|
Logger.Warn($"Rate-Limit erreicht, warte {waitTime} Sekunden...");
|
2020-11-05 19:23:42 +01:00
|
|
|
Thread.Sleep(waitTime * 1000);
|
2020-11-05 19:34:45 +01:00
|
|
|
await SendFinishedMessage(chatId, text, feed, waitTime * 2);
|
2020-11-05 19:23:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 20:57:41 +01:00
|
|
|
|
2020-11-05 19:34:45 +01:00
|
|
|
public static async void ShowAvailableFeeds(Message message, GroupCollection args)
|
|
|
|
{
|
2020-03-31 11:20:12 +02:00
|
|
|
string url = args[1].Value;
|
2020-01-30 20:57:41 +01:00
|
|
|
IEnumerable<HtmlFeedLink> feeds;
|
|
|
|
try {
|
|
|
|
feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url);
|
|
|
|
} catch {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Sete konnte nicht erreicht werden.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
List<HtmlFeedLink> htmlFeedLinks = feeds.ToList();
|
2020-01-30 20:57:41 +01:00
|
|
|
if (htmlFeedLinks.Count == 0) {
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:20:12 +02:00
|
|
|
string text = htmlFeedLinks.Aggregate("Feeds gefunden:\n",
|
2020-03-07 01:11:58 +01:00
|
|
|
(current, feedLink) =>
|
|
|
|
current + $"* <a href=\"{feedLink.Url}\">{Utils.StripHtml(feedLink.Title)}</a>\n");
|
2020-01-30 20:57:41 +01:00
|
|
|
|
|
|
|
await Bot.BotClient.SendTextMessageAsync(message.Chat, text, ParseMode.Html, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|