Exemple #1
0
        public async Task SendWhatsappNotifications()
        {
            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get pending notifications
            var pendingNotifications =
                await _notificationRepository.GetAll(NotificationExpression.PendingNotification());

            // If there are pending notifications
            if (pendingNotifications.Count > 0)
            {
                // Connect
                TwilioClient.Init(
                    Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"),
                    Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN")
                    );

                // For each notification
                var count       = 0;
                var failedCount = 0;
                foreach (var pendingNotification in pendingNotifications)
                {
                    try
                    {
                        // Send whatsapp
                        MessageResource.Create(
                            from: new PhoneNumber("whatsapp:" + pendingNotification.PhoneNumber),
                            to: new PhoneNumber("whatsapp:" + "+34666666666"),
                            body: pendingNotification.Message
                            );
                        pendingNotification.MarkAsSent();
                        count++;
                    }
                    catch (Exception ex)
                    {
                        // Log into Splunk
                        _logger.LogSplunkError(ex);
                        failedCount++;
                    }
                }

                // Save
                await _dbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkInformation("SendWhatsappNotifications", new
                {
                    Count         = count,
                    FailedCount   = failedCount,
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });
            }
        }
Exemple #2
0
        public async Task SendTelegramNotifications()
        {
            // Get pending notifications
            var pendingNotifications = await _mainDbContext.Notifications
                                       .Where(NotificationExpression.PendingNotification())
                                       .ToListAsync();

            await SendTelegramNotifications(pendingNotifications);
        }
Exemple #3
0
        public async Task SendTelegramNotifications()
        {
            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get pending notifications
            var pendingNotifications = await _notificationRepository.GetAll(NotificationExpression.PendingNotification());

            // If there are pending notifications
            if (pendingNotifications.Count == 0)
            {
                return;
            }

            // Connect
            var apiToken = _configuration["AppSettings:TelegramApiToken"];
            var bot      = new TelegramBotClient(apiToken);

            // For each notification
            var count       = 0;
            var failedCount = 0;

            foreach (var pendingNotification in pendingNotifications)
            {
                try
                {
                    // Send whatsapp
                    await bot.SendTextMessageAsync("@crypto_watcher_official", pendingNotification.Message);

                    pendingNotification.MarkAsSent();
                    count++;
                }
                catch (Exception ex)
                {
                    // Log into Splunk
                    _logger.LogSplunkError(ex);
                    failedCount++;
                }
            }

            // Save
            await _dbContext.SaveChangesAsync();

            // Stop watch
            stopwatch.Stop();

            // Log into Splunk
            _logger.LogSplunkInformation("SendTelegramNotifications", new
            {
                Count         = count,
                FailedCount   = failedCount,
                ExecutionTime = stopwatch.Elapsed.TotalSeconds
            });
        }
Exemple #4
0
        public async Task SendWhatsappNotifications()
        {
            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get pending notifications
            var pendingNotifications = await _mainDbContext.Notifications.Where(NotificationExpression.PendingNotification()).ToListAsync();

            // If there are pending notifications
            if (pendingNotifications.Count > 0)
            {
                // Connect
                TwilioClient.Init(
                    Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"),
                    Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN")
                    );

                // For each notification
                var count       = 0;
                var failedCount = 0;
                foreach (var pendingNotification in pendingNotifications)
                {
                    try
                    {
                        // Send whatsapp
                        MessageResource.Create(
                            from: new PhoneNumber("whatsapp:" + pendingNotification.PhoneNumber),
                            to: new PhoneNumber("whatsapp:" + "+34666666666"),
                            body: pendingNotification.Message
                            );
                        pendingNotification.MarkAsSent();
                        count++;
                    }
                    catch (Exception ex)
                    {
                        // Log
                        _logger.LogError(ex, ex.Message);
                        failedCount++;
                    }
                }

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log
                _logger.LogInformation("{@Event}, {@Count}, {@FailedCount}, {@ExecutionTime}", "WhatsappNotificationsSent", count, failedCount, stopwatch.Elapsed.TotalSeconds);
            }
        }
Exemple #5
0
        public async Task <List <Responses.Notification> > GetAllNotifications(string userId)
        {
            // Get user
            var user = await _userRepository.GetSingle(userId);

            // Check if it exists
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get all notifications
            var notifications = await _notificationRepository.GetAll(NotificationExpression.NotificationFilter(userId));

            // Response
            var response = _mapper.Map <List <Responses.Notification> >(notifications);

            // Return
            return(response);
        }