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 }); } }
public async Task SendTelegramNotifications() { // Get pending notifications var pendingNotifications = await _mainDbContext.Notifications .Where(NotificationExpression.PendingNotification()) .ToListAsync(); await SendTelegramNotifications(pendingNotifications); }
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 }); }
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); } }