Ejemplo n.º 1
0
    public override async Task HandleAsync(
        IUpdateContext context,
        UpdateDelegate next,
        string[] args
        )
    {
        await _telegramService.AddUpdateContext(context);

        var msg      = _telegramService.Message;
        var chatId   = msg.Chat.Id;
        var msgId    = msg.MessageId;
        var msgText  = msg.Text;
        var dateDate = DateTime.UtcNow.ToString("yyyy-MM-dd", new DateTimeFormatInfo());

        var checkUserPermission = await _telegramService.CheckUserPermission();

        if (!checkUserPermission)
        {
            var send = "Maaf, hanya Admin yang dapat mengekspor daftar RSS";
            await _telegramService.SendTextMessageAsync(send);

            return;
        }

        var rssSettings = await _rssService.GetRssSettingsAsync(chatId);

        Log.Information("RssSettings: {V}", rssSettings.ToJson(true));

        var listRss = new StringBuilder();

        foreach (var rss in rssSettings)
        {
            listRss.AppendLine(rss.UrlFeed);
        }

        Log.Debug("ListUrl: \n{ListRss}", listRss);

        var listRssStr = listRss.ToString().Trim();
        var sendText   = "Daftar RSS ini tidak terenkripsi, dapat di pulihkan di obrolan mana saja. " +
                         "Tambahkan parameter -e agar daftar RSS terenkripsi.";

        if (msgText.Contains("-e", StringComparison.CurrentCulture))
        {
            Log.Information("List RSS will be encrypted.");
            listRssStr = listRssStr.AesEncrypt();
            sendText   = "Daftar RSS ini terenkripsi, hanya dapat di pulihkan di obrolan ini!";
        }

        var filePath = $"{chatId}/rss-feed_{dateDate}_{msgId}.txt";
        await listRssStr.WriteTextAsync(filePath);

        var fileSend = Path.Combine("Storage", "Caches") + $"/{filePath}";
        await _telegramService.SendMediaAsync(
            fileSend,
            MediaType.LocalDocument,
            sendText
            );

        fileSend.DeleteFile();
    }
Ejemplo n.º 2
0
    public override async Task HandleAsync(
        IUpdateContext context,
        UpdateDelegate next,
        string[] args
        )
    {
        await _telegramService.AddUpdateContext(context);

        var chatId = _telegramService.ChatId;

        if (!await _telegramService.CheckUserPermission())
        {
            Log.Warning("This command only for sudo!");
            return;
        }

        await _telegramService.AppendTextAsync("Memulai Job RSS..");

        _rssFeedService.UnRegisterRssFeedByChatId(chatId);

        var rssSettings = await _rssService.GetRssSettingsAsync(chatId);

        var rssCount = rssSettings.Count();

        foreach (var rssSetting in rssSettings)
        {
            var urlFeed = rssSetting.UrlFeed;

            _rssFeedService.RegisterUrlFeed(chatId, urlFeed);
        }

        await _telegramService.AppendTextAsync($"Sebanyak {rssCount} RSS berhasil dimulai");
    }
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            _rssService      = new RssService(_telegramService.Message);
            var msg      = _telegramService.Message;
            var chatId   = msg.Chat.Id;
            var msgId    = msg.MessageId;
            var msgText  = msg.Text;
            var dateDate = DateTime.UtcNow.ToString("yyyy-MM-dd", new DateTimeFormatInfo());

            var isAdminOrPrivate = await _telegramService.IsAdminOrPrivateChat()
                                   .ConfigureAwait(false);

            if (!isAdminOrPrivate)
            {
                var send = "Maaf, hanya Admin yang dapat mengekspor daftar RSS";
                await _telegramService.SendTextAsync(send).ConfigureAwait(false);

                return;
            }

            var rssSettings = await _rssService.GetRssSettingsAsync()
                              .ConfigureAwait(false);

            Log.Information($"RssSettings: {rssSettings.ToJson(true)}");

            var listRss = new StringBuilder();

            foreach (var rss in rssSettings)
            {
                listRss.AppendLine(rss.UrlFeed);
            }

            Log.Information($"ListUrl: \n{listRss}");

            var listRssStr = listRss.ToString().Trim();
            var sendText   = "Daftar RSS ini tidak terenkripsi, dapat di pulihkan di obrolan mana saja. " +
                             "Tambahkan parameter -e agar daftar RSS terenkripsi.";

            if (msgText.Contains("-e", StringComparison.CurrentCulture))
            {
                Log.Information("List RSS will be encrypted.");
                listRssStr = listRssStr.AesEncrypt();
                sendText   = "Daftar RSS ini terenkripsi, hanya dapat di pulihkan di obrolan ini!";
            }

            var filePath = $"{chatId}/rss-feed_{dateDate}_{msgId}.txt";
            await listRssStr.WriteTextAsync(filePath).ConfigureAwait(false);

            var fileSend = BotSettings.PathCache + $"/{filePath}";
            await _telegramService.SendMediaAsync(fileSend, MediaType.LocalDocument, sendText)
            .ConfigureAwait(false);

            fileSend.DeleteFile();
        }
Ejemplo n.º 4
0
    public override async Task HandleAsync(
        IUpdateContext context,
        UpdateDelegate next,
        string[] args
        )
    {
        await _telegramService.AddUpdateContext(context);

        var chatId = _telegramService.ChatId;

        var checkUserPermission = await _telegramService.CheckUserPermission();

        if (!checkUserPermission)
        {
            await _telegramService.SendTextMessageAsync("Kamu bukan admin, atau kamu bisa mengaturnya di japri.");

            return;
        }

        await _telegramService.SendTextMessageAsync("🔄 Sedang meload data..");

        var rssData = await _rssService.GetRssSettingsAsync(chatId);

        var rssCount = rssData.Count();

        var sb = new StringBuilder();

        sb.AppendLine($"📚 <b>List RSS</b>: {rssCount} Items.");
        var num = 1;

        foreach (var rss in rssData)
        {
            sb.AppendLine($"{num++}. {rss.UrlFeed}");
        }

        if (rssCount == 0)
        {
            sb.AppendLine("\nSepertinya kamu belum menambahkan RSS disini. ");
            sb.AppendLine("Gunakan <code>/setrss https://link_rss_nya</code> untuk menambahkan.");
        }

        await _telegramService.EditMessageTextAsync(sb.ToTrimmedString());
    }
Ejemplo n.º 5
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 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 ");
            }
        }
Ejemplo n.º 6
0
    public async Task RegisterRssFeedByChatId(long chatId)
    {
        var op = Operation.Begin("Registering RSS by ChatId: {ChatId}", chatId);

        var rssSettings = await _rssService.GetRssSettingsAsync(chatId);

        var filteredSettings = rssSettings.Where(
            (
                setting,
                index
            ) => setting.IsEnabled
            );

        filteredSettings.ForEach
        (
            setting => {
            RegisterUrlFeed(chatId, setting.UrlFeed);
        }
        );

        op.Complete();
    }
Ejemplo n.º 7
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);
        }
        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);
        }
Ejemplo n.º 9
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.");
        }