137 lines
6.1 KiB
C#
137 lines
6.1 KiB
C#
|
|
using System.Text.Json;
|
|||
|
|
using Amazon;
|
|||
|
|
using Amazon.SQS;
|
|||
|
|
using Amazon.SQS.Model;
|
|||
|
|
using Kurs.Sender.Mail.AmazonSes;
|
|||
|
|
using Kurs.MailQueue.Domain.Entities;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Volo.Abp.Domain.Repositories;
|
|||
|
|
using Volo.Abp.Domain.Services;
|
|||
|
|
|
|||
|
|
namespace Kurs.MailQueue.Domain.MailTracking;
|
|||
|
|
|
|||
|
|
public class MailTrackingManager : DomainService
|
|||
|
|
{
|
|||
|
|
private readonly IRepository<BackgroundWorker_MailQueueEvents> repository;
|
|||
|
|
private readonly IConfiguration configuration;
|
|||
|
|
|
|||
|
|
public MailTrackingManager(
|
|||
|
|
IRepository<BackgroundWorker_MailQueueEvents> repository,
|
|||
|
|
IConfiguration configuration
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
this.repository = repository;
|
|||
|
|
this.configuration = configuration;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// AWS SQS Kuyrugundaki mesajlari alip isleyerek
|
|||
|
|
/// MailQueue_Events tablosuna kaydeder
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task StartAsync()
|
|||
|
|
{
|
|||
|
|
var accessKey = configuration.GetValue<string>(AmazonSesEmailSettingNames.AccessKey);
|
|||
|
|
var accessKeyId = configuration.GetValue<string>(AmazonSesEmailSettingNames.AccessKeyId);
|
|||
|
|
var region = configuration.GetValue<string>(AmazonSesEmailSettingNames.Region);
|
|||
|
|
var regionEndPoint = RegionEndpoint.GetBySystemName(region);
|
|||
|
|
var client = new AmazonSQSClient(accessKeyId, accessKey, regionEndPoint);
|
|||
|
|
|
|||
|
|
var queueUrl = await client.GetQueueUrlAsync("mail-delivery-tracking-queue");
|
|||
|
|
if (queueUrl.HttpStatusCode != System.Net.HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
throw new System.Exception(queueUrl.HttpStatusCode.ToString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var messages = await client.ReceiveMessageAsync(new ReceiveMessageRequest
|
|||
|
|
{
|
|||
|
|
QueueUrl = queueUrl.QueueUrl,
|
|||
|
|
MaxNumberOfMessages = 10,
|
|||
|
|
//WaitTimeSeconds = 5,
|
|||
|
|
});
|
|||
|
|
if (messages.HttpStatusCode != System.Net.HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
throw new System.Exception(messages.HttpStatusCode.ToString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Event examples: https://docs.aws.amazon.com/ses/latest/dg/event-publishing-retrieving-sns-examples.html#event-publishing-retrieving-sns-bounce
|
|||
|
|
foreach (var message in messages.Messages)
|
|||
|
|
{
|
|||
|
|
using JsonDocument doc = JsonDocument.Parse(message.Body);
|
|||
|
|
JsonElement root = doc.RootElement;
|
|||
|
|
using JsonDocument doc2 = JsonDocument.Parse(root.GetProperty("Message").ToString());
|
|||
|
|
JsonElement ev = doc2.RootElement;
|
|||
|
|
|
|||
|
|
var mail = ev.GetProperty("mail");
|
|||
|
|
var eventType = ev.GetProperty("eventType").ToString();
|
|||
|
|
|
|||
|
|
var item = new BackgroundWorker_MailQueueEvents
|
|||
|
|
{
|
|||
|
|
AwsMessageId = mail.GetProperty("messageId").ToString(),
|
|||
|
|
Event = eventType,
|
|||
|
|
EventDate = mail.GetProperty("timestamp").GetDateTime(),
|
|||
|
|
MailAddress = string.Join(",", mail.GetProperty("destination").EnumerateArray())
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
switch (eventType)
|
|||
|
|
{
|
|||
|
|
case "Bounce":
|
|||
|
|
var bounce = ev.GetProperty("bounce");
|
|||
|
|
item.EventDate = bounce.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.MailAddress = string.Join(",", (bounce.GetProperty("bouncedRecipients").EnumerateArray().Select(a => a.GetProperty("emailAddress").ToString()).ToList()));
|
|||
|
|
item.ResponseDescription = bounce.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Complaint":
|
|||
|
|
var complaint = ev.GetProperty("complaint");
|
|||
|
|
item.EventDate = complaint.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.MailAddress = string.Join(",", (complaint.GetProperty("complainedRecipients").EnumerateArray().Select(a => a.GetProperty("emailAddress").ToString()).ToList()));
|
|||
|
|
item.ResponseDescription = complaint.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Delivery":
|
|||
|
|
var delivery = ev.GetProperty("delivery");
|
|||
|
|
item.EventDate = delivery.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.MailAddress = string.Join(",", delivery.GetProperty("recipients").EnumerateArray());
|
|||
|
|
item.ResponseDescription = delivery.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Reject":
|
|||
|
|
item.ResponseDescription = ev.GetProperty("reject").ToString();
|
|||
|
|
break;
|
|||
|
|
case "Open":
|
|||
|
|
var open = ev.GetProperty("open");
|
|||
|
|
item.EventDate = open.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.ResponseDescription = open.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Click":
|
|||
|
|
var click = ev.GetProperty("click");
|
|||
|
|
item.EventDate = click.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.ResponseDescription = click.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Rendering Failure":
|
|||
|
|
item.ResponseDescription = ev.GetProperty("failure").ToString();
|
|||
|
|
break;
|
|||
|
|
case "DeliveryDelay":
|
|||
|
|
var deliveryDelay = ev.GetProperty("deliveryDelay");
|
|||
|
|
item.EventDate = deliveryDelay.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.MailAddress = string.Join(",", (deliveryDelay.GetProperty("delayedRecipients").EnumerateArray().Select(a => a.GetProperty("emailAddress").ToString()).ToList()));
|
|||
|
|
item.ResponseDescription = deliveryDelay.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Subscription":
|
|||
|
|
var subscription = ev.GetProperty("subscription");
|
|||
|
|
item.EventDate = subscription.GetProperty("timestamp").GetDateTime();
|
|||
|
|
item.ResponseDescription = subscription.ToString();
|
|||
|
|
break;
|
|||
|
|
case "Send":
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Mesaji DBye ekle
|
|||
|
|
await repository.InsertAsync(item, autoSave: true);
|
|||
|
|
|
|||
|
|
// Mesaji kuyruktan sil
|
|||
|
|
await client.DeleteMessageAsync(queueUrl.QueueUrl, message.ReceiptHandle);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|