public static async Task <int> ExecBroadcasterAsync(string chatId)
        {
            Log.Information("Starting RSS Scheduler.");
            int newRssCount = 0;

            var rssService = new RssService();

            Log.Information("Getting RSS settings..");
            var rssSettings = await rssService.GetRssSettingsAsync(chatId);

            var tasks = rssSettings.Select(async rssSetting =>
            {
                // foreach (RssSetting rssSetting in rssSettings)
                // {
                var rssUrl = rssSetting.UrlFeed;

                // var rssUrl = rssSetting["url_feed"].ToString();
                // var chatId = rssSetting["chat_id"].ToString();

                ConsoleHelper.WriteLine($"Processing {rssUrl} for {chatId}.");
                try
                {
                    var rssFeeds = await FeedReader.ReadAsync(rssUrl);
                    var rssTitle = rssFeeds.Title;

                    var castLimit = 3;
                    var castStep  = 0;

                    foreach (var rssFeed in rssFeeds.Items)
                    {
                        // Prevent flood in first time;
                        if (castLimit == castStep)
                        {
                            Log.Information($"Send stopped due limit {castLimit} for prevent flooding in first time");
                            break;
                        }

                        var titleLink = $"{rssTitle} - {rssFeed.Title}".MkUrl(rssFeed.Link);
                        var category  = rssFeed.Categories.MkJoin(", ");
                        var sendText  = $"{titleLink}" +
                                        $"\nTags: {category}";

                        var where = new Dictionary <string, object>()
                        {
                            { "chat_id", chatId },
                            { "url", rssFeed.Link }
                        };

                        var isExist = await rssService.IsExistInHistory(where);
                        if (!isExist)
                        {
                            Log.Information($"Sending feed to {chatId}");

                            try
                            {
                                await Bot.Client.SendTextMessageAsync(chatId, sendText, ParseMode.Html);

                                var data = new Dictionary <string, object>()
                                {
                                    { "url", rssFeed.Link },
                                    { "rss_source", rssUrl },
                                    { "chat_id", chatId },
                                    { "title", rssFeed.Title },
                                    { "publish_date", rssFeed.PublishingDate.ToString() },
                                    { "author", rssFeed.Author }
                                };

                                Log.Information($"Writing to RSS History");
                                await rssService.SaveRssHistoryAsync(data);

                                castStep++;
                                newRssCount++;
                            }
                            catch (ChatNotFoundException chatNotFoundException)
                            {
                                Log.Information($"May Bot not added in {chatId}.");
                                Log.Error(chatNotFoundException, "Chat Not Found");
                                // ConsoleHelper.WriteLine($"May Bot not added in {chatId}." +
                                // $"\n{chatNotFoundException.Message}");
                            }
                        }
                        else
                        {
                            Log.Information($"This feed has sent to {chatId}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Broadcasting RSS Feed.");
                    Thread.Sleep(4000);

                    // ConsoleHelper.WriteLine(ex.Message);
                    // ConsoleHelper.WriteLine(ex.ToString());
                }
                // }
            });

            await Task.WhenAll(tasks);

            Log.Information($"RSS Scheduler finished. New RSS Count: {newRssCount}");

            return(newRssCount);
        }
Exemple #2
0
        public static void BackgroundWriteCache(this object dataTable, string fileJson)
        {
            var jobId = BackgroundJob.Enqueue(() => WriteCacheAsync(dataTable, fileJson, true));

            ConsoleHelper.WriteLine($"Background Write Cache scheduled with ID: {jobId}");
        }