public void GetFeeds_Can_Successfully_Retrieve_Values_From_Rss()
        {
            var url = ConfigurationManager.AppSettings["rss.sergejus"];
            var rssService = new RssService();

            var feeds = rssService.GetFeeds(url, this.date);

            Assert.That(feeds.Count(), Is.GreaterThan(0));
        }
        public void GetFeeds_Returns_Empty_List_When_Internal_Exception_Happens()
        {
            var empty = 0;
            var url = "http://sergejus.blogas.lt/tag/fakeltnet/atom";
            var rssService = new RssService();

            var feeds = rssService.GetFeeds(url, this.date);

            Assert.AreEqual(empty, feeds.Count());
        }
        public void Given_Last_Feed_Date_GetFeeds_Can_Successfully_Retrieve_Latest_Values_From_Rss()
        {
            var url = ConfigurationManager.AppSettings["rss.sergejus"];
            var rssService = new RssService();

            var feeds = rssService.GetFeeds(url, this.date).ToList();
            if (feeds.Count() > 1)
            {
                var fromDate = feeds.Last().Published;
                var latestFeeds = rssService.GetFeeds(url, fromDate);

                Assert.That(feeds.Count(), Is.GreaterThan(latestFeeds.Count()));
            }
        }
        public Task ExecuteGroupAsync(CommandContext ctx,
                                      [Description("Subreddit.")] string sub = "all")
        {
            string url = RssService.GetFeedURLForSubreddit(sub, out string rsub);

            if (url == null)
            {
                throw new CommandFailedException("That subreddit doesn't exist.");
            }

            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults(url);

            if (res == null)
            {
                throw new CommandFailedException($"Failed to get the data from that subreddit ({Formatter.Bold(rsub)}).");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
        private async Task GetLatestNews()
        {
            LoadingIndicator.IsVisible = true;
            LoadingIndicator.IsRunning = true;

            RssService             service = new RssService();
            IEnumerable <FeedItem> items   = await service.GetNews("http://www.indaco.coop/feed/");

            var itemsWithHtml = items.Select(i => new
            {
                i.Title,
                i.PublishDate,
                HtmlSource = new HtmlWebViewSource {
                    Html = i.Description
                },
            });

            News.ItemsSource           = itemsWithHtml;
            LoadingIndicator.IsRunning = false;
        }
        public void CanParseRssFeed(string feed)
        {
            using (var context = new BlogifierDbContext(_options))
            {
                var storage = new BlogStorage("test");
                var path = Path.Combine(GetRoot(), feed);

                var uow = new UnitOfWork(context);

                var profile = uow.Profiles.Single(p => p.IdentityName == "test");

                if(profile == null)
                {
                    profile = new Profile();
                    profile.IdentityName = "test";
                    profile.AuthorName = "test";
                    profile.AuthorEmail = "*****@*****.**";
                    profile.Title = "test";
                    profile.Slug = "test";
                    profile.Description = "the test";
                    profile.BlogTheme = "Standard";

                    uow.Profiles.Add(profile);
                    uow.Complete();
                }

                Assert.True(context.Profiles.ToList().Count > 0);

                var service = new RssService(uow, null);

                var model = new RssImportModel();
                model.FeedUrl = path;
                model.ProfileId = profile.Id;

                var result = service.Import(model);

                Assert.True(context.BlogPosts.ToList().Count > 1);
            }

            Assert.NotNull(feed);
        }
Exemple #7
0
        public static async Task <bool> TryAddSubscriptionAsync(this DBService db, ulong cid, string url, string qname = null)
        {
            var newest = RssService.GetFeedResults(url)?.FirstOrDefault();

            if (newest == null)
            {
                return(false);
            }

            int?sid = null;
            await db.ExecuteTransactionAsync(async (con, tsem) => {
                int?id = null;
                using (var cmd = con.CreateCommand()) {
                    cmd.CommandText = "INSERT INTO gf.feeds VALUES (DEFAULT, @url, @savedurl) ON CONFLICT (url) DO UPDATE SET url = EXCLUDED.url RETURNING id;";
                    cmd.Parameters.AddWithValue("url", NpgsqlDbType.Text, url);
                    cmd.Parameters.AddWithValue("savedurl", NpgsqlDbType.Text, newest.Links[0].Uri.ToString());

                    object res = await cmd.ExecuteScalarAsync().ConfigureAwait(false);
                    if (res != null && !(res is DBNull))
                    {
                        id = (int)res;
                    }
                }

                using (var cmd = con.CreateCommand()) {
                    cmd.CommandText = "INSERT INTO gf.subscriptions VALUES (@id, @cid, @qname) ON CONFLICT DO NOTHING RETURNING id;";
                    cmd.Parameters.Add(new NpgsqlParameter <int>("id", id.Value));
                    cmd.Parameters.Add(new NpgsqlParameter <long>("cid", (long)cid));
                    cmd.Parameters.Add(new NpgsqlParameter <string>("qname", qname));

                    object res = await cmd.ExecuteScalarAsync().ConfigureAwait(false);
                    if (res != null && !(res is DBNull))
                    {
                        sid = (int)res;
                    }
                }
            });

            return(sid.HasValue);
        }
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            _rssService      = new RssService(context.Update.Message);

            var chatId  = _telegramService.Message.Chat.Id;
            var isAdmin = await _telegramService.IsAdminGroup();

            if (isAdmin || _telegramService.IsPrivateChat())
            {
                await _telegramService.SendTextAsync("🔄 Sedang meload data..");

                var rssData = await _rssService.GetRssSettingsAsync(chatId);

                var rssCount = rssData.Count();

                var sendText = $"📚 <b>List RSS</b>: {rssCount} Items.";
                int num      = 1;
                foreach (RssSetting rss in rssData)
                {
                    // var urlFeed = rss["url_feed"].ToString();
                    sendText += $"\n{num++}. {rss.UrlFeed}";
                }

                if (rssCount == 0)
                {
                    sendText += "\n\nSepertinya kamu belum menambahkan RSS disini. " +
                                "Kamu dapat menambahkan RSS dengan jumlah tidak terbatas!" +
                                "\nGunakan <code>/setrss https://link_rss_nya</code> untuk menambahkan.";
                }

                await _telegramService.EditAsync(sendText);
            }
            else
            {
                await _telegramService.SendTextAsync("Kamu bukan admin, atau kamu bisa mengaturnya di japri ");
            }
        }
Exemple #9
0
        public async Task RssService_FetchesFeedAsync_FeedItemsListHasEntries()
        {
            var expectedNumberOfItems = 2;
            var service = new RssService(_rssHttpClientFactory);

            var document = new XDocument();
            var xElement = new XElement("channel");

            document.Add(xElement);
            xElement.Add(
                CreateRssEntry("RSSTitle", "*****@*****.**", "a useful description", "Fri, 06 Apr 2018 12:51:01 +0000"),
                CreateRssEntry("RSSTitle", "*****@*****.**", "a useful description", "Fri, 06 Apr 2018 12:51:01 +0000")
                );

            _httpClient.GetStringAsync(Arg.Any <string>()).Returns(document.ToString());
            _rssHttpClientFactory.CreateHttpClient().Returns(_httpClient);

            var feedItems = await service.FetchFeedAsync(Urls.RssFeedUrl);

            Assert.IsNotEmpty(feedItems);
            Assert.AreEqual(expectedNumberOfItems, feedItems.Count);
        }
Exemple #10
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            ChatHelper.Init(context);
            _rssService = new RssService(context.Update.Message);

            var chatId = ChatHelper.Message.Chat.Id;

            if (ChatHelper.Message.From.Id.IsSudoer())
            {
                ConsoleHelper.WriteLine("Test started..");
                await "Sedang mengetes sesuatu".SendTextAsync();

                var data = await new Query("rss_history")
                           .Where("chat_id", chatId)
                           .ExecForMysql()
                           .GetAsync();

                var rssHistories = data
                                   .ToJson()
                                   .MapObject <List <RssHistory> >();

                ConsoleHelper.WriteLine(data.GetType());
                ConsoleHelper.WriteLine(data.ToJson(true));

                ConsoleHelper.WriteLine(rssHistories.GetType());
                ConsoleHelper.WriteLine(rssHistories.ToJson(true));

                ConsoleHelper.WriteLine("Test completed..");
                await "Selesai ngetest".EditAsync();
            }
            else
            {
                await "Unauthorized.".SendTextAsync();
            }

            ChatHelper.Close();
        }
Exemple #11
0
        public static async Task <int> ExecBroadcasterAsync(long chatId)
        {
            Log.Information("Starting RSS Scheduler.");
            int newRssCount = 0;

            var rssService = new RssService();

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

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

                Log.Information($"Processing {rssUrl} for {chatId}.");
                try
                {
                    await ExecuteUrlAsync(chatId, rssUrl)
                    .ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Broadcasting RSS Feed.");
                    Thread.Sleep(4000);
                }

                // }
            });

            await Task.WhenAll(tasks).ConfigureAwait(false);

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

            return(newRssCount);
        }
Exemple #12
0
            public async Task It_calls_the_endpoint()
            {
                var called = false;

                var uri = GetUri(_name);

                var handler = _mockHandlerBuilder
                              .When(uri)
                              .Return(HttpStatusCode.OK, _rss)
                              .WhenCalled(_ => called = true)
                              .Build();

                var client = new HttpClient(handler)
                {
                    BaseAddress = new Uri(_baseAddress)
                };

                var sut = new RssService(client);

                await sut.GetChannel(_name);

                Assert.True(called);
            }
        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 #14
0
 public RssItemsViewController(IntPtr handle) : base(handle)
 {
     this.Channel    = new rssChannel();
     this.RssService = new RssService();
 }
Exemple #15
0
 public RssModule(RssService service, Random color)
 {
     _service      = service;
     _timers       = new ConcurrentBag <Timer>();
     _random_color = color;
 }
Exemple #16
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args, CancellationToken cancellationToken)
        {
            ChatHelper.Init(context);
            _rssService = new RssService(ChatHelper.Message);

            var url = ChatHelper.Message.Text.GetTextWithoutCmd();

            if (url != null)
            {
                await $"Sedang memeriksa {url}".AppendTextAsync();
                if (url.CheckUrlValid())
                {
                    await $"Sedang memvalidasi {url}".AppendTextAsync();
                    var isValid = await url.IsValidUrlFeed();

                    if (!isValid)
                    {
                        await "Sedang mencari kemungkinan RSS yang valid".AppendTextAsync();
                        var foundUrl = await url.GetBaseUrl().FindUrlFeed();

                        ConsoleHelper.WriteLine($"Found URL Feed: {foundUrl}");

                        if (foundUrl != "")
                        {
                            url = foundUrl;
                        }
                        else
                        {
                            var notfoundRss = $"Kami tidak dapat memvalidasi {url} adalah Link RSS yang valid, " +
                                              $"dan mencoba mencari di {url.GetBaseUrl()} tetap tidak dapat menemukan.";

                            await notfoundRss.EditAsync();

                            ChatHelper.Close();
                            return;
                        }
                    }

                    var isFeedExist = await _rssService.IsExistRssAsync(url);

                    ConsoleHelper.WriteLine($"Is Url Exist: {isFeedExist}");

                    if (!isFeedExist)
                    {
                        await $"Sedang menyimpan..".SendTextAsync();

                        var data = new Dictionary <string, object>()
                        {
                            { "url_feed", url },
                            { "chat_id", ChatHelper.Message.Chat.Id },
                            { "from_id", ChatHelper.Message.From.Id }
                        };

                        await _rssService.SaveRssSettingAsync(data);

                        await $"Url: {url} berhasil di simpan".EditAsync();
                    }
                    else
                    {
                        await $"Url: {url} sudah di simpan".SendTextAsync();
                    }
                }
                else
                {
                    await "Url tersebut sepertinya tidak valid".SendTextAsync();
                }
            }
            else
            {
                await "Apa url Feednya?".SendTextAsync();
            }

            ChatHelper.Close();
        }
Exemple #17
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            _rssService      = new RssService(_telegramService.Message);
            var chatId = _telegramService.Message.Chat.Id;

            var url = _telegramService.Message.Text.GetTextWithoutCmd();

            if (url != null)
            {
                await _telegramService.AppendTextAsync($"URL: {url}");

                if (url.CheckUrlValid())
                {
                    await _telegramService.AppendTextAsync($"Sedang mengecek apakah berisi RSS");

                    var isValid = await url.IsValidUrlFeed();

                    if (!isValid)
                    {
                        await _telegramService.AppendTextAsync("Sedang mencari kemungkinan tautan RSS yang valid");

                        var foundUrl = await url.GetBaseUrl().FindUrlFeed();

                        Log.Information($"Found URL Feed: {foundUrl}");

                        if (foundUrl != "")
                        {
                            url = foundUrl;
                        }
                        else
                        {
                            var notfoundRss = $"Kami tidak dapat memvalidasi {url} adalah Link RSS yang valid, " +
                                              $"dan mencoba mencari di {url.GetBaseUrl()} tetap tidak dapat menemukan.";

                            await _telegramService.EditAsync(notfoundRss);

                            return;
                        }
                    }

                    var isFeedExist = await _rssService.IsExistRssAsync(url);

                    Log.Information($"Is Url Exist: {isFeedExist}");

                    if (!isFeedExist)
                    {
                        await _telegramService.AppendTextAsync($"Sedang menyimpan..");

                        var data = new Dictionary <string, object>()
                        {
                            { "url_feed", url },
                            { "chat_id", _telegramService.Message.Chat.Id },
                            { "from_id", _telegramService.Message.From.Id }
                        };

                        await _rssService.SaveRssSettingAsync(data);

                        await _telegramService.AppendTextAsync("Memastikan Scheduler sudah berjalan");

                        chatId.RegisterScheduler();

                        await _telegramService.AppendTextAsync($"Tautan berhasil di simpan");
                    }
                    else
                    {
                        await _telegramService.AppendTextAsync($"Tautan sudah di simpan");
                    }
                }
                else
                {
                    await _telegramService.AppendTextAsync("Url tersebut sepertinya tidak valid");
                }
            }
            else
            {
                await _telegramService.SendTextAsync("Apa url Feednya?");
            }
        }
Exemple #18
0
 public RssController(ILogger <RssController> logger, RssService rssService)
 {
     _logger     = logger;
     _rssService = rssService;
 }
Exemple #19
0
 public RssController(RssService rssService) => _rssService = rssService;
Exemple #20
0
 public async Task RefreshAsync()
 {
     await RssService.SyncFeedAsync();
 }
Exemple #21
0
        /// <inheritdoc/>
        public async Task InitializeAsync()
        {
            await RssService.GetSubscriptionsAsync();

            await RssService.GetFeedAsync();
        }
Exemple #22
0
        public static async Task ExecuteUrlAsync(long chatId, string rssUrl)
        {
            int newRssCount = 0;
            var rssService  = new RssService();

            var rssFeeds = await FeedReader.ReadAsync(rssUrl)
                           .ConfigureAwait(false);

            var rssTitle = rssFeeds.Title;

            // var castLimit = 10;
            // 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 whereHistory = new Dictionary <string, object>()
                {
                    ["chat_id"]    = chatId,
                    ["rss_source"] = rssUrl
                };

                var rssHistory = await rssService.GetRssHistory(whereHistory)
                                 .ConfigureAwait(false);

                var lastRssHistory = rssHistory.LastOrDefault();

                if (!rssHistory.Any())
                {
                    break;
                }

                var lastArticleDate    = DateTime.Parse(lastRssHistory.PublishDate);
                var currentArticleDate = rssFeed.PublishingDate.Value;

                if (currentArticleDate < lastArticleDate)
                {
                    Log.Information($"Current article is older than last article. Stopped.");
                    break;
                }

                Log.Information($"LastArticleDate: {lastArticleDate}");
                Log.Information($"CurrentArticleDate: {currentArticleDate}");


                Log.Information("Prepare sending article.");

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

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

                var isExist = await rssService.IsExistInHistory(where)
                              .ConfigureAwait(false);

                if (isExist)
                {
                    Log.Information($"This feed has sent to {chatId}");
                    break;
                }

                Log.Information($"Sending feed to {chatId}");

                try
                {
                    await BotSettings.Client.SendTextMessageAsync(chatId, sendText, ParseMode.Html)
                    .ConfigureAwait(false);

                    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 },
                        { "created_at", DateTime.Now.ToString(CultureInfo.InvariantCulture) }
                    };

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

                    // castStep++;
                    newRssCount++;
                }
                catch (ChatNotFoundException chatNotFoundException)
                {
                    Log.Information($"May Bot not added in {chatId}.");
                    Log.Error(chatNotFoundException, "Chat Not Found");
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "RSS Broadcaster error");
                }
            }
        }
Exemple #23
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            _rssService      = new RssService(context.Update.Message);

            var chatId = _telegramService.Message.Chat.Id;
            var fromId = _telegramService.Message.From.Id;
            var msg    = _telegramService.Message;

            if (fromId.IsSudoer())
            {
                Log.Information("Test started..");
                await _telegramService.SendTextAsync("Sedang mengetes sesuatu")
                .ConfigureAwait(false);

                // var data = await new Query("rss_history")
                //     .Where("chat_id", chatId)
                //     .ExecForMysql()
                //     .GetAsync();
                //
                // var rssHistories = data
                //     .ToJson()
                //     .MapObject<List<RssHistory>>();
                //
                // ConsoleHelper.WriteLine(data.GetType());
                // // ConsoleHelper.WriteLine(data.ToJson(true));
                //
                // ConsoleHelper.WriteLine(rssHistories.GetType());
                // // ConsoleHelper.WriteLine(rssHistories.ToJson(true));
                //
                // ConsoleHelper.WriteLine("Test completed..");

                // await "This test".LogToChannel();

                // await RssHelper.SyncRssHistoryToCloud();
                // await BotHelper.ClearLog();

                // await SyncHelper.SyncGBanToLocalAsync();
                // var greet = TimeHelper.GetTimeGreet();

                var inlineKeyboard = new InlineKeyboardMarkup(new[]
                {
                    // new[]
                    // {
                    // InlineKeyboardButton.WithCallbackData("Warn Username Limit", "info warn-username-limit"),
                    // InlineKeyboardButton.WithCallbackData("-", "callback-set warn_username_limit 3"),
                    // InlineKeyboardButton.WithCallbackData("4", "info setelah"),
                    // InlineKeyboardButton.WithCallbackData("+", "callback-set warn_username_limit 5")
                    // },
                    new[]
                    {
                        // InlineKeyboardButton.WithCallbackData("Warn Username Limit", "info warn-username-limit"),
                        InlineKeyboardButton.WithCallbackData("-", "callback-set warn_username_limit 3"),
                        InlineKeyboardButton.WithCallbackData("4", "info setelah"),
                        InlineKeyboardButton.WithCallbackData("+", "callback-set warn_username_limit 5")
                    }
                });

                // await _telegramService.EditAsync("Warn Username Limit", inlineKeyboard);

                // LearningHelper.Setup2();
                // LearningHelper.Predict();


                if (msg.ReplyToMessage != null)
                {
                    var repMsg     = msg.ReplyToMessage;
                    var repMsgText = repMsg.Text;

                    Log.Information("Predicting message");
                    var isSpam = MachineLearning.PredictMessage(repMsgText);
                    await _telegramService.EditAsync($"IsSpam: {isSpam}")
                    .ConfigureAwait(false);

                    return;
                }

                await _telegramService.EditAsync("Complete")
                .ConfigureAwait(false);
            }

            // else
            // {
            //     await _requestProvider.SendTextAsync("Unauthorized.");
            // }
        }
Exemple #24
0
        public static async Task ExecScheduler(string chatId)
        {
            ConsoleHelper.WriteLine("Starting RSS Scheduler.");

            var rssService = new RssService();

            ConsoleHelper.WriteLine("Getting RSS settings..");
            var rssSettings = await rssService.GetRssSettingsAsync(chatId);

            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}.");
                var rssFeeds = await FeedReader.ReadAsync(rssUrl);

                var rssTitle = rssFeeds.Title;

                var rssLimit = 3;
                var rssStep  = 1;

                foreach (var rssFeed in rssFeeds.Items.Take(rssLimit))
                {
                    // if (rssStep == rssLimit)
                    // {
                    // ConsoleHelper.WriteLine($"RSS Limit to {rssLimit} 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)
                    {
                        ConsoleHelper.WriteLine($"Sending feed to {chatId}");

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

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

                            ConsoleHelper.WriteLine($"Writing to RSS History");
                            await rssService.SaveRssHistoryAsync(data);

                            rssStep++;
                        }
                        catch (ChatNotFoundException chatNotFoundException)
                        {
                            ConsoleHelper.WriteLine($"May Bot not added in {chatId}." +
                                                    $"\nMessage: {chatNotFoundException.Message}");
                        }
                    }
                    else
                    {
                        ConsoleHelper.WriteLine($"This feed has sent to {chatId}");
                    }
                }
            }

            ConsoleHelper.WriteLine("RSS Scheduler finished.");
        }