public async Task StartSending(long chatId)
        {
            var user = _dbContext.Users.Where(x => x.ChatId == chatId.ToString()).SingleOrDefault();
            var clearedPhoneNumber = user != null?_phoneHelper.Clear(user.PhoneNumber) : null;

            if (clearedPhoneNumber != null && _configService.Config.Admins.Contains(clearedPhoneNumber))
            {
                var state = _dbContext.States.First();

                if (state.IsEnabled == 1)
                {
                    await Bot.Api.SendTextMessageAsync
                        (chatId,
                        $"Рассылка уже включена");

                    await _logger.LogIncoming(LogConst.EnableSendingButItYetEnabled, clearedPhoneNumber);

                    await _newLogger.LogByType(MessageTypes.System, LogConst.EnableSendingButItYetEnabled, clearedPhoneNumber);

                    return;
                }

                state.IsEnabled = 1;
                _dbContext.States.Update(state);
                _dbContext.SaveChanges();

                await Bot.Api.SendTextMessageAsync
                    (chatId,
                    $"Рассылка возобновлена");

                await _logger.LogIncoming(LogConst.SendingEnabled, clearedPhoneNumber);

                await _newLogger.LogByType(MessageTypes.System, LogConst.SendingEnabled, clearedPhoneNumber);

                return;
            }

            await Bot.Api.SendTextMessageAsync
                (chatId,
                $":) ");

            await _logger.LogIncoming(LogConst.EnableSendingButPermissionDenied, clearedPhoneNumber);

            await _newLogger.LogByType(MessageTypes.System, LogConst.EnableSendingButPermissionDenied, clearedPhoneNumber);
        }
Example #2
0
        public async Task ReceivedContact(long chatId, string username, string phone)
        {
            var clearedPhoneNumber = _phoneHelper.Clear(phone);

            if (_dbContext.Users.Any(x => x.ChatId == chatId.ToString()))
            {
                await Bot.Api.SendTextMessageAsync(chatId,
                                                   "Вы уже подписаны на рассылку");

                await _logger.LogAuth(LogConst.UserAlreadySubscribetButSendContact, _phoneHelper.Format(clearedPhoneNumber));

                await _newLogger.LogByType(MessageTypes.Incoming, LogConst.UserAlreadySubscribetButSendContact, clearedPhoneNumber);

                return;
            }

            if (_dbContext.UnauthorizedUsers.Any(x => x.ChatId == chatId.ToString()))
            {
                await Bot.Api.SendTextMessageAsync(chatId,
                                                   "На ваш телефон отправлен код подтверждения, отправьте его сюда",
                                                   replyMarkup : ReplyMarkupResetCode);

                await _logger.LogAuth(LogConst.UserNotSubscribedAndGetCodeButSendContact,
                                      username);

                await _newLogger.LogByType(MessageTypes.Auth, LogConst.UserNotSubscribedAndGetCodeButSendContact, username);

                return;
            }

            var code = await _phoneNumberVerifier.SendVerifyRequest(clearedPhoneNumber);

            _dbContext.UnauthorizedUsers.Add(new UnauthorizedUser
            {
                ChatId      = chatId.ToString(),
                Username    = username,
                PhoneNumber = clearedPhoneNumber,
                Code        = code.ToString()
            });
            _dbContext.SaveChanges();

            await Bot.Api.SendTextMessageAsync(chatId,
                                               "На ваш телефон отправлен код подтверждения, отправьте его сюда",
                                               replyMarkup : ReplyMarkupResetCode);

            await _logger.LogAuth(LogConst.CodeSendedToUser, _phoneHelper.Format(clearedPhoneNumber));

            await _newLogger.LogByType(MessageTypes.Auth, LogConst.CodeSendedToUser, clearedPhoneNumber);
        }