Beispiel #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);
        }
    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");
        }
    }