Ejemplo n.º 1
0
    public async Task MemberKickJob(
        long chatId,
        long userId
        )
    {
        var needVerify = await _stepHistoriesService.GetStepHistoryVerifyCore
                         (
            new StepHistory()
        {
            ChatId = chatId,
            UserId = userId
        }
                         );

        if (!needVerify.Any())
        {
            return;
        }

        Log.Information(
            "Starting kick member for UserId {UserId} from {ChatId}",
            userId,
            chatId
            );

        var history = needVerify.FirstOrDefault();

        if (history == null)
        {
            return;
        }

        await _botClient.BanChatMemberAsync(chatId, userId);

        await _botClient.UnbanChatMemberAsync(chatId, userId);

        if (history.LastWarnMessageId != -1)
        {
            await _botClient.DeleteMessageAsync(chatId, history.LastWarnMessageId);
        }

        await UpdateStepHistoryStatus(chatId, userId);

        var chat = await _chatService.GetChatAsync(chatId);

        var sb = new StringBuilder()
                 .Append("<b>Action:</b> #KickChatMember")
                 .AppendLine()
                 .Append("<b>User:</b> ")
                 .AppendFormat("{0}\n", history.UserId.GetNameLink(history.FirstName, history.LastName))
                 .Append("<b>Chat:</b> ")
                 .AppendFormat("{0} \n", chat.GetChatNameLink())
                 .Append("<b>Reason:</b> ")
                 .AppendJoin(",", needVerify.Select(x => $"#{x.Name}"))
                 .AppendLine()
                 .AppendFormat(
            "#U{0} #C{1}",
            userId,
            chatId.ReduceChatId()
            );

        await _botClient.SendTextMessageAsync(
            text : sb.ToTrimmedString(),
            disableWebPagePreview : true,
            chatId : _eventLogConfig.ChannelId,
            parseMode : ParseMode.Html
            );

        Log.Information(
            "UserId {UserId} successfully kicked from {ChatId}",
            userId,
            chatId
            );
    }
Ejemplo n.º 2
0
    public async Task <bool> ExecuteVerifyAsync()
    {
        Log.Information("Executing Verify Callback");

        var callbackData = _callbackQuery.Data;
        var fromId       = _telegramService.FromId;
        var chatId       = _telegramService.ChatId;

        Log.Debug(
            "CallbackData: {CallbackData} from {FromId}",
            callbackData,
            fromId
            );

        var partCallbackData = callbackData.Split(" ");
        var callBackParam1   = partCallbackData.ElementAtOrDefault(1);
        var answer           = "Tombol ini bukan untukmu Bep!";

        Log.Debug("Verify Param1: {Param}", callBackParam1);

        Log.Information("Starting Verify from History for UserId: {UserId}", fromId);
        var needVerifyList = (await _stepHistoriesService.GetStepHistoryVerifyCore(
                                  new StepHistory()
        {
            ChatId = chatId,
            UserId = fromId
        }
                                  )).ToList();

        if (!needVerifyList.Any())
        {
            answer = "Kamu tidak perlu verifikasi!";
        }
        else
        {
            await _userProfilePhotoService.ResetUserProfilePhotoCacheAsync(fromId);

            foreach (var step in needVerifyList)
            {
                var updateHistory = step;
                updateHistory.UpdatedAt = DateTime.Now;

                switch (step.Name)
                {
                case StepHistoryName.ChatMemberUsername:
                    Log.Debug("Verifying Username for UserId {UserId}", fromId);
                    if (_telegramService.HasUsername)
                    {
                        updateHistory.Status = StepHistoryStatus.HasVerify;
                    }
                    break;

                case StepHistoryName.ChatMemberPhoto:
                    Log.Debug("Verifying User Profile Photo for UserId {UserId}", fromId);
                    if (await _userProfilePhotoService.HasUserProfilePhotosAsync(fromId))
                    {
                        updateHistory.Status = StepHistoryStatus.HasVerify;
                    }
                    break;

                case StepHistoryName.ForceSubscription:
                    var chatMember = await _telegramService.ChatService.GetChatMemberAsync(
                        chatId : chatId,
                        userId : fromId,
                        evictAfter : true
                        );

                    if (chatMember.Status != ChatMemberStatus.Left)
                    {
                        updateHistory.Status = StepHistoryStatus.HasVerify;
                    }

                    break;

                case StepHistoryName.HumanVerification:
                    updateHistory.Status = StepHistoryStatus.HasVerify;
                    break;

                default:
                    break;
                }

                await _stepHistoriesService.SaveStepHistory(updateHistory);
            }

            var afterVerify = await _stepHistoriesService.GetStepHistoryVerifyCore(
                new StepHistory()
            {
                ChatId = chatId,
                UserId = fromId
            }
                );

            if (!afterVerify.Any())
            {
                await _telegramService.UnmuteChatMemberAsync(fromId);

                answer = "Terima kasih sudah verifikasi!";
            }
            else
            {
                answer = "Silakan lakukan sesuai instruksi, lalu tekan Verifikasi";
            }
        }

        await _telegramService.AnswerCallbackQueryAsync(answer);

        return(true);
    }