Ejemplo n.º 1
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)
        {
            Log.Warning("Delete RSS only for admin or private chat!");
            await _telegramService.DeleteAsync();

            return;
        }

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

        await _telegramService.SendTextMessageAsync($"Sedang menghapus {urlFeed}");

        var delete = await _rssService.DeleteRssAsync(chatId, urlFeed);

        var success = delete.ToBool()
            ? "berhasil."
            : "gagal. Mungkin RSS tersebut sudah di hapus atau belum di tambahkan";

        await _telegramService.EditMessageTextAsync($"Hapus {urlFeed} {success}");

        await _rssFeedService.ReRegisterRssFeedByChatId(chatId);
    }
Ejemplo n.º 2
0
    public async Task <bool> ExecuteAsync()
    {
        var       chatId            = _telegramService.ChatId;
        var       chatTitle         = _telegramService.ChatTitle;
        var       messageId         = _telegramService.CallBackMessageId;
        var       answerHeader      = $"RSS Control for {chatTitle}";
        var       answerDescription = string.Empty;
        var       part  = _telegramService.CallbackQuery.Data?.Split(" ");
        var       rssId = part !.ElementAtOrDefault(2);
        var       page  = 0;
        const int take  = 5;

        if (!await _telegramService.CheckUserPermission())
        {
            await _telegramService.AnswerCallbackQueryAsync("Anda tidak mempunyai akses", true);

            return(false);
        }

        var rssFind = new RssSettingFindDto()
        {
            ChatId = chatId
        };

        var updateValue = new Dictionary <string, object>()
        {
            { "updated_at", DateTime.UtcNow }
        };

        switch (part.ElementAtOrDefault(1))
        {
        case "stop-all":
            updateValue.Add("is_enabled", false);
            answerDescription = $"Semua service berhasil dimatikan";
            break;

        case "start-all":
            updateValue.Add("is_enabled", true);
            answerDescription = "Semua service berhasil diaktifkan";
            break;

        case "start":
            rssFind.Id = rssId.ToInt64();
            updateValue.Add("is_enabled", true);
            answerDescription = "Service berhasil di aktifkan";
            break;

        case "stop":
            rssFind.Id = rssId.ToInt64();
            updateValue.Add("is_enabled", false);
            answerDescription = "Service berhasil dimatikan";
            break;

        case "attachment-off":
            rssFind.Id = rssId.ToInt64();
            updateValue.Add("include_attachment", false);
            answerDescription = "Attachment tidak akan ditambahkan";
            break;

        case "attachment-on":
            rssFind.Id = rssId.ToInt64();
            updateValue.Add("include_attachment", true);
            answerDescription = "Berhasil mengaktifkan attachment";
            break;

        case "delete":
            await _rssService.DeleteRssAsync(
                chatId : chatId,
                columnName : "id",
                columnValue : rssId
                );

            answerDescription = "Service berhasil dihapus";
            break;

        case "navigate-page":
            var toPage = part.ElementAtOrDefault(2).ToInt();
            page = toPage;
            answerDescription = "Halaman " + (toPage + 1);
            break;
        }

        await _rssService.UpdateRssSettingAsync(rssFind, updateValue);

        await _rssFeedService.ReRegisterRssFeedByChatId(chatId);

        var answerCombined = answerHeader + Environment.NewLine + answerDescription;

        var btnMarkupCtl = await _rssService.GetButtonMarkup(
            chatId : chatId,
            page : page,
            take : take
            );

        if (answerDescription.IsNotNullOrEmpty())
        {
            await _telegramService.EditMessageCallback(answerCombined, btnMarkupCtl);

            if (part.ElementAtOrDefault(1)?.Contains("all") ?? false)
            {
                await _telegramService.AnswerCallbackQueryAsync(answerCombined, true);
            }
        }

        await _messageHistoryService.UpdateDeleteAtAsync(
            new MessageHistoryFindDto()
        {
            ChatId    = chatId,
            MessageId = messageId
        },
            DateTime.UtcNow.AddMinutes(10)
            );

        return(true);
    }
Ejemplo n.º 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");
        }
    }