private void Menu_Callbacks(object sender, CallbackQueryEventArgs e) { Task.Run(() => { string callbackData = e.CallbackQuery.Data; string[] callbackDataSplitted = callbackData.Split(" "); var rssUser = _userService.Get(x => x.TelegramId == e.CallbackQuery.From.Id); if (callbackData == TelegramBotCommands.CALLBACK_SUBSCRIBE) { //will be rearanged with mongodb aggregation in order to query one time. var userRssList = _userRssService.GetList(x => x.UserId == rssUser.Id); var userGroupCount = _telegramGroupService.GetCollection().Count(x => x.UserId == rssUser.Id); var userSubscribeList = _userSubscribeService.GetList(x => x.UserId == rssUser.Id); userRssList = userRssList.Where(x => (userSubscribeList.Count(y => y.RssId == x.Id) < userGroupCount)).ToList(); var customRssMenu = userRssList.ToMenu(TelegramBotCommands.KEY_RSS_SUBSCRIBE); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.SUBSCRIBE_INFORMATION, replyMarkup: customRssMenu); } else if (callbackData == TelegramBotCommands.CALLBACK_UNSUBSCRIBE) { //will be rearanged with mongodb aggregation in order to query one time. var userRssList = _userRssService.GetList(x => x.UserId == rssUser.Id); var userSubscribeList = _userSubscribeService.GetList(x => x.UserId == rssUser.Id); userRssList = userRssList.Where(x => userSubscribeList.Any(y => y.RssId == x.Id)).ToList(); var customRssMenu = userRssList.ToMenu(TelegramBotCommands.KEY_RSS_UNSUBSCRIBE); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.UNSUBSCRIBE_INFORMATION, replyMarkup: customRssMenu); } else if (callbackData == TelegramBotCommands.CALLBACK_HOME) { _telegramBotClient.SendStartMenu(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId); } else if (callbackDataSplitted.CallbackIsEqualTo(TelegramBotCommands.KEY_RSS_SUBSCRIBE)) { //will be rearanged with mongodb aggregation in order to query one time. var userGroupList = _telegramGroupService.GetList(x => x.UserId == rssUser.Id); var userSubscribeList = _userSubscribeService.GetList(x => x.UserId == rssUser.Id); string rssId = callbackData.RemoveFirstChar().RemoveFirstChar(); userGroupList = userGroupList.Where(x => !userSubscribeList.Any(y => y.GroupId == x.Id && y.RssId == rssId)).ToList(); var customGroupMenu = userGroupList.ToMenu(string.Concat(TelegramBotCommands.KEY_RSS_SUBSCRIBE_GROUP, callbackData.RemoveFirstChar())); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.SUBSCRIBE_GROUP_INFORMATION, replyMarkup: customGroupMenu, parseMode: ParseMode.Html); } else if (callbackDataSplitted.CallbackIsEqualTo(TelegramBotCommands.KEY_RSS_UNSUBSCRIBE)) { //will be rearanged with mongodb aggregation in order to query one time. var userGroupList = _telegramGroupService.GetList(x => x.UserId == rssUser.Id); var userSubscribeList = _userSubscribeService.GetList(x => x.UserId == rssUser.Id); string rssId = callbackData.RemoveFirstChar().RemoveFirstChar(); userGroupList = userGroupList.Where(x => userSubscribeList.Any(y => y.GroupId == x.Id && y.RssId == rssId)).ToList(); var customGroupMenu = userGroupList.ToMenu(string.Concat(TelegramBotCommands.KEY_RSS_UNSUBSCRIBE_GROUP, " ", callbackData.RemoveFirstChar())); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.UNSUBSCRIBE_GROUP_INFORMATION, replyMarkup: customGroupMenu); } else if (callbackDataSplitted.CallbackIsEqualTo(TelegramBotCommands.KEY_RSS_SUBSCRIBE_GROUP)) { string[] Ids = callbackData.RemoveFirstChar().Split(" ", StringSplitOptions.RemoveEmptyEntries); //Index 0: RSS //Index 1: GROUP _userSubscribeService.Save(new dal.entities.UserSubscribe { UserId = rssUser.Id, GroupId = Ids[1], RssId = Ids[0], }); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.USER_SUBSCRIBE_INFORMATION , replyMarkup: TelegramHelper.CreateMenu(TelegramMenu.BUTTONS_SUBSCRIBE_UNSUBSCRIBE)); } else if (callbackDataSplitted.CallbackIsEqualTo(TelegramBotCommands.KEY_RSS_UNSUBSCRIBE_GROUP)) { string[] Ids = callbackData.RemoveFirstChar().Split(" ", StringSplitOptions.RemoveEmptyEntries); //Index 0: RSS //Index 1: GROUP _userSubscribeService.Delete(x => x.RssId == Ids[0] && x.UserId == rssUser.Id && x.GroupId == Ids[1]); _telegramBotClient.EditMessageTextAsync(e.CallbackQuery.From.Id, e.CallbackQuery.Message.MessageId, TelegramBotCommands.USER_UNSUBSCRIBE_INFORMATION , replyMarkup: TelegramHelper.CreateMenu(TelegramMenu.BUTTONS_SUBSCRIBE_UNSUBSCRIBE)); } }); }