示例#1
0
        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 msgId    = msg.MessageId;
            var chatId   = msg.Chat.Id;
            var msgText  = msg.Text;
            var dateDate = DateTime.UtcNow.ToString("yyyy-MM-dd");

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

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

                return;
            }

            await _telegramService.AppendTextAsync("Sedang mempersiapkan")
            .ConfigureAwait(false);

            var filePath = $"{chatId}/rss-feed_{dateDate}_{msgId}.txt";

            filePath = await _telegramService.DownloadFileAsync(filePath)
                       .ConfigureAwait(false);

            await _telegramService.AppendTextAsync("Sedang membuka berkas")
            .ConfigureAwait(false);

            var rssLists = await File.ReadAllLinesAsync(filePath, cancellationToken)
                           .ConfigureAwait(false);

            foreach (var rssList in rssLists)
            {
                Log.Information($"Importing {rssList}");
                var data = new Dictionary <string, object>()
                {
                    { "url_feed", rssList },
                    { "chat_id", _telegramService.Message.Chat.Id },
                    { "from_id", _telegramService.Message.From.Id }
                };

                await _rssService.SaveRssSettingAsync(data)
                .ConfigureAwait(false);
            }

            await _telegramService.AppendTextAsync($"Memeriksa RSS duplikat")
            .ConfigureAwait(false);

            await _rssService.DeleteDuplicateAsync()
            .ConfigureAwait(false);

            await _telegramService.AppendTextAsync($"{rssLists.Length} RSS berhasil di import")
            .ConfigureAwait(false);
        }
示例#2
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?");
            }
        }
示例#3
0
    public override async Task HandleAsync(
        IUpdateContext context,
        UpdateDelegate next,
        string[] args
        )
    {
        await _telegramService.AddUpdateContext(context);

        var msg      = _telegramService.Message;
        var msgId    = msg.MessageId;
        var chatId   = _telegramService.ChatId;
        var fromId   = _telegramService.FromId;
        var dateDate = DateTime.UtcNow.ToString("yyyy-MM-dd");

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

            return;
        }

        await _telegramService.AppendTextAsync("Sedang mempersiapkan");

        var filePath = $"{chatId}/rss-feed_{dateDate}_{msgId}";

        filePath = await _telegramService.DownloadFileAsync(filePath);

        await _telegramService.AppendTextAsync("Sedang membuka berkas");

        var rssLists = await File.ReadAllLinesAsync(filePath);

        foreach (var rssList in rssLists)
        {
            Log.Information("Importing {RssList}", rssList);

            var data = new Dictionary <string, object>()
            {
                { "url_feed", rssList },
                { "chat_id", chatId },
                { "from_id", fromId }
            };

            await _rssService.SaveRssSettingAsync(data);
        }

        await _telegramService.AppendTextAsync($"Memeriksa RSS duplikat");

        var dedupe = await _rssService.DeleteDuplicateAsync();

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

        _rssFeedService.ReRegisterRssFeedByChatId(chatId);

        var importCount = rssLists.Length;

        if (dedupe != importCount)
        {
            var diff = importCount - dedupe;
            await _telegramService.AppendTextAsync($"{diff} RSS berhasil di import");
        }
        else
        {
            await _telegramService.AppendTextAsync($"RSS telah di import");
        }
    }
示例#4
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();
        }