mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Use file-scoped namespaces
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
using BotFramework;
|
||||
using Kruzya.TelegramBot.RichSiteSummary.Data;
|
||||
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler
|
||||
{
|
||||
public abstract class AbstractHandler : BotEventHandler
|
||||
{
|
||||
protected readonly RichSiteSummaryContext _dbContext;
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler;
|
||||
|
||||
protected AbstractHandler(RichSiteSummaryContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
public abstract class AbstractHandler : BotEventHandler
|
||||
{
|
||||
protected readonly RichSiteSummaryContext _dbContext;
|
||||
|
||||
protected AbstractHandler(RichSiteSummaryContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
}
|
||||
@@ -13,299 +13,298 @@ using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler;
|
||||
|
||||
public class FeedList : AbstractHandler
|
||||
{
|
||||
public class FeedList : AbstractHandler
|
||||
public FeedList(RichSiteSummaryContext dbContext) : base(dbContext)
|
||||
{
|
||||
public FeedList(RichSiteSummaryContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[ParametrizedCommand("rss_show")]
|
||||
public async Task ShowFeed(Feed feed) => await ShowFeed(feed, Chat, From);
|
||||
[ParametrizedCommand("rss_show")]
|
||||
public async Task ShowFeed(Feed feed) => await ShowFeed(feed, Chat, From);
|
||||
|
||||
public async Task ShowFeed(Guid feedId, Chat chat, User from)
|
||||
public async Task ShowFeed(Guid feedId, Chat chat, User from)
|
||||
{
|
||||
var feed = await _dbContext.FindAsync<Feed>(feedId);
|
||||
if (feed == null)
|
||||
{
|
||||
var feed = await _dbContext.FindAsync<Feed>(feedId);
|
||||
if (feed == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await ShowFeed(feed, chat, from);
|
||||
return;
|
||||
}
|
||||
|
||||
public async Task ShowFeed(Feed feed, Chat chat, User from, Message message = null)
|
||||
await ShowFeed(feed, chat, from);
|
||||
}
|
||||
|
||||
public async Task ShowFeed(Feed feed, Chat chat, User from, Message message = null)
|
||||
{
|
||||
var textBuilder = new StringBuilder();
|
||||
textBuilder.Append(feed.Representation(ParseMode.Html));
|
||||
textBuilder.Append("\n");
|
||||
textBuilder.Append($"<b>Posts at this moment</b>: {feed.Posts.Count}\n");
|
||||
textBuilder.Append($"<b>RSS feed</b>: <code>{feed.Url}</code>\n");
|
||||
textBuilder.Append($"<b>Home page</b>: <code>{feed.HomePage}</code>\n");
|
||||
textBuilder.Append($"<b>Last synchronization</b>: <code>{feed.UpdatedAt}</code>");
|
||||
|
||||
var text = textBuilder.ToString();
|
||||
|
||||
var buttons = new List<InlineKeyboardButton>();
|
||||
var callbackData = $"feed|{from.Id}|do|{feed.FeedId}";
|
||||
if (await _dbContext.Subscriptions.HasSubscription(chat, feed))
|
||||
{
|
||||
var textBuilder = new StringBuilder();
|
||||
textBuilder.Append(feed.Representation(ParseMode.Html));
|
||||
textBuilder.Append("\n");
|
||||
textBuilder.Append($"<b>Posts at this moment</b>: {feed.Posts.Count}\n");
|
||||
textBuilder.Append($"<b>RSS feed</b>: <code>{feed.Url}</code>\n");
|
||||
textBuilder.Append($"<b>Home page</b>: <code>{feed.HomePage}</code>\n");
|
||||
textBuilder.Append($"<b>Last synchronization</b>: <code>{feed.UpdatedAt}</code>");
|
||||
|
||||
var text = textBuilder.ToString();
|
||||
|
||||
var buttons = new List<InlineKeyboardButton>();
|
||||
var callbackData = $"feed|{from.Id}|do|{feed.FeedId}";
|
||||
if (await _dbContext.Subscriptions.HasSubscription(chat, feed))
|
||||
buttons.Add(new InlineKeyboardButton("Unsubscribe")
|
||||
{
|
||||
buttons.Add(new InlineKeyboardButton("Unsubscribe")
|
||||
{
|
||||
CallbackData = callbackData,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
buttons.Add(new InlineKeyboardButton("Subscribe")
|
||||
{
|
||||
CallbackData = callbackData,
|
||||
});
|
||||
}
|
||||
buttons.Add(new InlineKeyboardButton("Last 10 posts")
|
||||
{
|
||||
CallbackData = $"feed|{from.Id}|last|{feed.FeedId}",
|
||||
CallbackData = callbackData,
|
||||
});
|
||||
|
||||
var inlineButtons = new InlineKeyboardMarkup(buttons);
|
||||
if (message == null)
|
||||
{
|
||||
await Bot.SendTextMessageAsync(new ChatId(chat.Id), text, 0, ParseMode.Html, null, true, true, false, 0, replyMarkup: inlineButtons);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Bot.EditMessageTextAsync(chat, message.MessageId, text, ParseMode.Html, null, true, replyMarkup: inlineButtons);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buttons.Add(new InlineKeyboardButton("Subscribe")
|
||||
{
|
||||
CallbackData = callbackData,
|
||||
});
|
||||
}
|
||||
buttons.Add(new InlineKeyboardButton("Last 10 posts")
|
||||
{
|
||||
CallbackData = $"feed|{from.Id}|last|{feed.FeedId}",
|
||||
});
|
||||
|
||||
var inlineButtons = new InlineKeyboardMarkup(buttons);
|
||||
if (message == null)
|
||||
{
|
||||
await Bot.SendTextMessageAsync(new ChatId(chat.Id), text, 0, ParseMode.Html, null, true, true, false, 0, replyMarkup: inlineButtons);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Bot.EditMessageTextAsync(chat, message.MessageId, text, ParseMode.Html, null, true, replyMarkup: inlineButtons);
|
||||
}
|
||||
}
|
||||
|
||||
[ParametrizedCommand("rss_list")]
|
||||
public async Task List() => await GenerateMenu();
|
||||
[ParametrizedCommand("rss_list")]
|
||||
public async Task List() => await GenerateMenu();
|
||||
|
||||
[ParametrizedCommand("rss_search")]
|
||||
public async Task Search(string query)
|
||||
[ParametrizedCommand("rss_search")]
|
||||
public async Task Search(string query)
|
||||
{
|
||||
if (!(await GenerateMenu(query)))
|
||||
{
|
||||
if (!(await GenerateMenu(query)))
|
||||
{
|
||||
await Bot.SendTextMessageAsync(Chat, $"No one feed match by search pattern (<code>{query}</code>).", 0, ParseMode.Html, null, true);
|
||||
}
|
||||
await Bot.SendTextMessageAsync(Chat, $"No one feed match by search pattern (<code>{query}</code>).", 0, ParseMode.Html, null, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("rss_subscriptions")]
|
||||
public async Task Subscriptions()
|
||||
[Command("rss_subscriptions")]
|
||||
public async Task Subscriptions()
|
||||
{
|
||||
if (!(await GenerateMenu(":sub")))
|
||||
{
|
||||
if (!(await GenerateMenu(":sub")))
|
||||
{
|
||||
await Bot.SendTextMessageAsync(Chat,
|
||||
"There are no one subscription. Add new with /feed_list or /feed_search.");
|
||||
}
|
||||
await Bot.SendTextMessageAsync(Chat,
|
||||
"There are no one subscription. Add new with /feed_list or /feed_search.");
|
||||
}
|
||||
}
|
||||
|
||||
#region Message generator
|
||||
#region Message generator
|
||||
|
||||
protected async Task<bool> GenerateMenu() =>
|
||||
await GenerateMenu(string.Empty);
|
||||
protected async Task<bool> GenerateMenu() =>
|
||||
await GenerateMenu(string.Empty);
|
||||
|
||||
protected async Task<bool> GenerateMenu(string query) =>
|
||||
await GenerateMenu(query, 0);
|
||||
protected async Task<bool> GenerateMenu(string query) =>
|
||||
await GenerateMenu(query, 0);
|
||||
|
||||
protected async Task<bool> GenerateMenu(string query, UInt16 page, Message editableMessage = null)
|
||||
protected async Task<bool> GenerateMenu(string query, UInt16 page, Message editableMessage = null)
|
||||
{
|
||||
var chat = editableMessage != null ? (editableMessage.Chat) : Chat;
|
||||
var elementsOnPage = 9;
|
||||
var startIndex = page * elementsOnPage;
|
||||
var dbQuery = _dbContext.Feeds.AsQueryable()
|
||||
.Where(f => !f.IsPrivate);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(query))
|
||||
{
|
||||
var chat = editableMessage != null ? (editableMessage.Chat) : Chat;
|
||||
var elementsOnPage = 9;
|
||||
var startIndex = page * elementsOnPage;
|
||||
var dbQuery = _dbContext.Feeds.AsQueryable()
|
||||
.Where(f => !f.IsPrivate);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(query))
|
||||
var searchQuery = query.ToLower();
|
||||
if (query.EndsWith(":sub"))
|
||||
{
|
||||
var searchQuery = query.ToLower();
|
||||
if (query.EndsWith(":sub"))
|
||||
{
|
||||
var chatList = await _dbContext.Subscriptions.AllSubscriptions(chat).Select(chat => chat.Feed.FeedId).ToArrayAsync();
|
||||
dbQuery = dbQuery.Where(sub => chatList.Contains(sub.FeedId));
|
||||
var chatList = await _dbContext.Subscriptions.AllSubscriptions(chat).Select(chat => chat.Feed.FeedId).ToArrayAsync();
|
||||
dbQuery = dbQuery.Where(sub => chatList.Contains(sub.FeedId));
|
||||
|
||||
searchQuery = searchQuery.Remove(searchQuery.Length - 4, 4);
|
||||
}
|
||||
searchQuery = searchQuery.Remove(searchQuery.Length - 4, 4);
|
||||
}
|
||||
|
||||
dbQuery = dbQuery.Where(feed => feed.Title.ToLower().Contains(searchQuery) || feed.Url.ToLower().Contains(searchQuery) || feed.HomePage.ToLower().Contains(searchQuery));
|
||||
}
|
||||
dbQuery = dbQuery.Where(feed => feed.Title.ToLower().Contains(searchQuery) || feed.Url.ToLower().Contains(searchQuery) || feed.HomePage.ToLower().Contains(searchQuery));
|
||||
}
|
||||
|
||||
var totalCount = await dbQuery.CountAsync();
|
||||
if (totalCount == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (totalCount == 1)
|
||||
{
|
||||
await ShowFeed((await dbQuery.FirstOrDefaultAsync()), Chat, From);
|
||||
return true;
|
||||
}
|
||||
|
||||
var result = await dbQuery.Take(elementsOnPage * (page + 1)).Skip(startIndex)
|
||||
.ToArrayAsync();
|
||||
|
||||
var backButton = (page > 0);
|
||||
var nextButton = totalCount >= (startIndex + elementsOnPage);
|
||||
|
||||
var buttons = new List<List<InlineKeyboardButton>>();
|
||||
List<InlineKeyboardButton> row;
|
||||
foreach (var feed in result)
|
||||
{
|
||||
row = new List<InlineKeyboardButton>();
|
||||
row.Add(new InlineKeyboardButton(feed.Title)
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|show|{feed.FeedId.ToString()}",
|
||||
});
|
||||
|
||||
buttons.Add(row);
|
||||
}
|
||||
|
||||
if (backButton || nextButton)
|
||||
{
|
||||
row = new List<InlineKeyboardButton>();
|
||||
if (backButton)
|
||||
{
|
||||
row.Add(new InlineKeyboardButton("⬅️")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|page|{query}|{page - 1}",
|
||||
});
|
||||
}
|
||||
|
||||
row.Add(new InlineKeyboardButton($"{page + 1}/{(totalCount / elementsOnPage) + 1}")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|donothing",
|
||||
});
|
||||
|
||||
if (nextButton)
|
||||
{
|
||||
row.Add(new InlineKeyboardButton("➡️")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|page|{query}|{page + 1}",
|
||||
});
|
||||
}
|
||||
|
||||
buttons.Add(row);
|
||||
}
|
||||
|
||||
if (editableMessage == null)
|
||||
{
|
||||
await Bot.SendTextMessageAsync(Chat, "Select the feed for viewing details", 0, null,
|
||||
null, true, true, false, 0, replyMarkup: new InlineKeyboardMarkup(buttons));
|
||||
}
|
||||
else
|
||||
{
|
||||
await Bot.EditMessageTextAsync(editableMessage.Chat, editableMessage.MessageId, "Select the feed for viewing details", null, null, false,
|
||||
new InlineKeyboardMarkup(buttons));
|
||||
}
|
||||
var totalCount = await dbQuery.CountAsync();
|
||||
if (totalCount == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (totalCount == 1)
|
||||
{
|
||||
await ShowFeed((await dbQuery.FirstOrDefaultAsync()), Chat, From);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Message handler
|
||||
|
||||
[Update(UpdateFlag.CallbackQuery)]
|
||||
public async Task HandleAction()
|
||||
{
|
||||
if (!RawUpdate.CallbackQuery.Data.StartsWith("feed|"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var data = RawUpdate.CallbackQuery.Data.Split('|');
|
||||
var fromId = Int64.Parse(data[1]);
|
||||
if (fromId != From.Id)
|
||||
{
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id,
|
||||
"You can't perform this action: command called by another user.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check action.
|
||||
switch (data[2])
|
||||
{
|
||||
case "page":
|
||||
await GenerateMenu(data[3], UInt16.Parse(data[4]), RawUpdate.CallbackQuery.Message);
|
||||
break;
|
||||
|
||||
case "show":
|
||||
await ShowFeed(Guid.Parse(data[3]), RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From);
|
||||
break;
|
||||
|
||||
case "do":
|
||||
if (RawUpdate.CallbackQuery.From.Id != RawUpdate.CallbackQuery.Message.Chat.Id)
|
||||
{
|
||||
var userStatus = (await Bot.GetChatMemberAsync(RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From.Id)).Status;
|
||||
var allowedUserStatuses = new ChatMemberStatus[]
|
||||
{ChatMemberStatus.Administrator, ChatMemberStatus.Creator};
|
||||
|
||||
if (!allowedUserStatuses.Contains(userStatus))
|
||||
{
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id,
|
||||
"You can't perform this action: you don't have administrator permissions.", true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await ChangeFeedSubscription(Guid.Parse(data[3]), RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From, RawUpdate.CallbackQuery.Message);
|
||||
break;
|
||||
|
||||
case "last":
|
||||
var feed = await _dbContext.FindAsync<Feed>(Guid.Parse(data[3]));
|
||||
var posts = feed.Posts.OrderByDescending(p => p.ReceivedAt).Take(10).ToList();
|
||||
|
||||
var messageTextBuilder = new StringBuilder();
|
||||
messageTextBuilder.Append($"Last 10 posts from {feed.Representation(ParseMode.Html)}\n");
|
||||
messageTextBuilder.Append("\n");
|
||||
|
||||
var idx = 1;
|
||||
foreach (var post in posts)
|
||||
{
|
||||
messageTextBuilder.Append($"{idx}. {post}\n");
|
||||
idx++;
|
||||
}
|
||||
|
||||
await Bot.SendTextMessageAsync(RawUpdate.CallbackQuery.Message.Chat, messageTextBuilder.ToString(),
|
||||
0, ParseMode.Html, null, true);
|
||||
break;
|
||||
}
|
||||
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id);
|
||||
}
|
||||
var result = await dbQuery.Take(elementsOnPage * (page + 1)).Skip(startIndex)
|
||||
.ToArrayAsync();
|
||||
|
||||
var backButton = (page > 0);
|
||||
var nextButton = totalCount >= (startIndex + elementsOnPage);
|
||||
|
||||
protected async Task ChangeFeedSubscription(Guid feedId, Chat chat, User from, Message message)
|
||||
var buttons = new List<List<InlineKeyboardButton>>();
|
||||
List<InlineKeyboardButton> row;
|
||||
foreach (var feed in result)
|
||||
{
|
||||
var feed = await _dbContext.FindAsync<Feed>(feedId);
|
||||
if (feed == null)
|
||||
row = new List<InlineKeyboardButton>();
|
||||
row.Add(new InlineKeyboardButton(feed.Title)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CallbackData = $"feed|{From.Id}|show|{feed.FeedId.ToString()}",
|
||||
});
|
||||
|
||||
var subscriptionState = await _dbContext.Subscriptions.HasSubscription(chat, feed);
|
||||
if (subscriptionState)
|
||||
{
|
||||
var subId = await _dbContext.Subscriptions
|
||||
.Where(subscriber => subscriber.Feed == feed && subscriber.SubscriberId == chat.Id)
|
||||
.Select(subscriber => subscriber.SubscriptionId).FirstAsync();
|
||||
|
||||
_dbContext.Remove(_dbContext.Find<Subscriber>(subId));
|
||||
}
|
||||
else
|
||||
{
|
||||
var subscription = _dbContext.Subscriptions.Create();
|
||||
subscription.Feed = feed;
|
||||
subscription.SubscriberId = chat.Id;
|
||||
|
||||
_dbContext.Add(subscription);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
await ShowFeed(feed, chat, from, message);
|
||||
buttons.Add(row);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
if (backButton || nextButton)
|
||||
{
|
||||
row = new List<InlineKeyboardButton>();
|
||||
if (backButton)
|
||||
{
|
||||
row.Add(new InlineKeyboardButton("⬅️")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|page|{query}|{page - 1}",
|
||||
});
|
||||
}
|
||||
|
||||
row.Add(new InlineKeyboardButton($"{page + 1}/{(totalCount / elementsOnPage) + 1}")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|donothing",
|
||||
});
|
||||
|
||||
if (nextButton)
|
||||
{
|
||||
row.Add(new InlineKeyboardButton("➡️")
|
||||
{
|
||||
CallbackData = $"feed|{From.Id}|page|{query}|{page + 1}",
|
||||
});
|
||||
}
|
||||
|
||||
buttons.Add(row);
|
||||
}
|
||||
|
||||
if (editableMessage == null)
|
||||
{
|
||||
await Bot.SendTextMessageAsync(Chat, "Select the feed for viewing details", 0, null,
|
||||
null, true, true, false, 0, replyMarkup: new InlineKeyboardMarkup(buttons));
|
||||
}
|
||||
else
|
||||
{
|
||||
await Bot.EditMessageTextAsync(editableMessage.Chat, editableMessage.MessageId, "Select the feed for viewing details", null, null, false,
|
||||
new InlineKeyboardMarkup(buttons));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Message handler
|
||||
|
||||
[Update(UpdateFlag.CallbackQuery)]
|
||||
public async Task HandleAction()
|
||||
{
|
||||
if (!RawUpdate.CallbackQuery.Data.StartsWith("feed|"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var data = RawUpdate.CallbackQuery.Data.Split('|');
|
||||
var fromId = Int64.Parse(data[1]);
|
||||
if (fromId != From.Id)
|
||||
{
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id,
|
||||
"You can't perform this action: command called by another user.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check action.
|
||||
switch (data[2])
|
||||
{
|
||||
case "page":
|
||||
await GenerateMenu(data[3], UInt16.Parse(data[4]), RawUpdate.CallbackQuery.Message);
|
||||
break;
|
||||
|
||||
case "show":
|
||||
await ShowFeed(Guid.Parse(data[3]), RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From);
|
||||
break;
|
||||
|
||||
case "do":
|
||||
if (RawUpdate.CallbackQuery.From.Id != RawUpdate.CallbackQuery.Message.Chat.Id)
|
||||
{
|
||||
var userStatus = (await Bot.GetChatMemberAsync(RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From.Id)).Status;
|
||||
var allowedUserStatuses = new ChatMemberStatus[]
|
||||
{ChatMemberStatus.Administrator, ChatMemberStatus.Creator};
|
||||
|
||||
if (!allowedUserStatuses.Contains(userStatus))
|
||||
{
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id,
|
||||
"You can't perform this action: you don't have administrator permissions.", true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await ChangeFeedSubscription(Guid.Parse(data[3]), RawUpdate.CallbackQuery.Message.Chat, RawUpdate.CallbackQuery.From, RawUpdate.CallbackQuery.Message);
|
||||
break;
|
||||
|
||||
case "last":
|
||||
var feed = await _dbContext.FindAsync<Feed>(Guid.Parse(data[3]));
|
||||
var posts = feed.Posts.OrderByDescending(p => p.ReceivedAt).Take(10).ToList();
|
||||
|
||||
var messageTextBuilder = new StringBuilder();
|
||||
messageTextBuilder.Append($"Last 10 posts from {feed.Representation(ParseMode.Html)}\n");
|
||||
messageTextBuilder.Append("\n");
|
||||
|
||||
var idx = 1;
|
||||
foreach (var post in posts)
|
||||
{
|
||||
messageTextBuilder.Append($"{idx}. {post}\n");
|
||||
idx++;
|
||||
}
|
||||
|
||||
await Bot.SendTextMessageAsync(RawUpdate.CallbackQuery.Message.Chat, messageTextBuilder.ToString(),
|
||||
0, ParseMode.Html, null, true);
|
||||
break;
|
||||
}
|
||||
|
||||
await Bot.AnswerCallbackQueryAsync(RawUpdate.CallbackQuery.Id);
|
||||
}
|
||||
|
||||
protected async Task ChangeFeedSubscription(Guid feedId, Chat chat, User from, Message message)
|
||||
{
|
||||
var feed = await _dbContext.FindAsync<Feed>(feedId);
|
||||
if (feed == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var subscriptionState = await _dbContext.Subscriptions.HasSubscription(chat, feed);
|
||||
if (subscriptionState)
|
||||
{
|
||||
var subId = await _dbContext.Subscriptions
|
||||
.Where(subscriber => subscriber.Feed == feed && subscriber.SubscriberId == chat.Id)
|
||||
.Select(subscriber => subscriber.SubscriptionId).FirstAsync();
|
||||
|
||||
_dbContext.Remove(_dbContext.Find<Subscriber>(subId));
|
||||
}
|
||||
else
|
||||
{
|
||||
var subscription = _dbContext.Subscriptions.Create();
|
||||
subscription.Feed = feed;
|
||||
subscription.SubscriberId = chat.Id;
|
||||
|
||||
_dbContext.Add(subscription);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
await ShowFeed(feed, chat, from, message);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -7,31 +7,30 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler
|
||||
{
|
||||
public class StatsHandler : AbstractHandler
|
||||
{
|
||||
public StatsHandler(RichSiteSummaryContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
[Command("rss_stats")]
|
||||
public async Task Execute()
|
||||
{
|
||||
var feedCount = await _dbContext.Feeds.CountAsync();
|
||||
var postsCount = await _dbContext.Posts.CountAsync();
|
||||
var subscriptionsCount = await _dbContext.Subscriptions.CountAsync();
|
||||
var uniqueSubscribers =
|
||||
await _dbContext.Subscriptions.Select(sub => sub.SubscriberId).Distinct().CountAsync();
|
||||
|
||||
var textMessage = new StringBuilder();
|
||||
textMessage.Append("ℹ️ In database on this moment:\n");
|
||||
textMessage.Append($"- registered <b>{feedCount} feeds</b>\n");
|
||||
textMessage.Append($"- saved <b>{postsCount} posts</b> for prevent re-sending\n");
|
||||
textMessage.Append(
|
||||
$"- exists <b>{subscriptionsCount} subscriptions on feeds</b> (<b>unique subscribers: {uniqueSubscribers}</b>)");
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Handler;
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat, textMessage.ToString(), parseMode: ParseMode.Html);
|
||||
}
|
||||
public class StatsHandler : AbstractHandler
|
||||
{
|
||||
public StatsHandler(RichSiteSummaryContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
[Command("rss_stats")]
|
||||
public async Task Execute()
|
||||
{
|
||||
var feedCount = await _dbContext.Feeds.CountAsync();
|
||||
var postsCount = await _dbContext.Posts.CountAsync();
|
||||
var subscriptionsCount = await _dbContext.Subscriptions.CountAsync();
|
||||
var uniqueSubscribers =
|
||||
await _dbContext.Subscriptions.Select(sub => sub.SubscriberId).Distinct().CountAsync();
|
||||
|
||||
var textMessage = new StringBuilder();
|
||||
textMessage.Append("ℹ️ In database on this moment:\n");
|
||||
textMessage.Append($"- registered <b>{feedCount} feeds</b>\n");
|
||||
textMessage.Append($"- saved <b>{postsCount} posts</b> for prevent re-sending\n");
|
||||
textMessage.Append(
|
||||
$"- exists <b>{subscriptionsCount} subscriptions on feeds</b> (<b>unique subscribers: {uniqueSubscribers}</b>)");
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat, textMessage.ToString(), parseMode: ParseMode.Html);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user