Skip to content

Commit 0e85b9d

Browse files
committed
feat: support email notification
fixes #1
1 parent b929eba commit 0e85b9d

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

OpenReservation.Notification/NotificationRequest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ public sealed class NotificationRequest
66
{
77
public string? MsgId { get; set; }
88
public string? Signature { get; set; }
9+
910
[Required]
1011
public required string Text { get; set; }
1112

13+
public string? Subject { get; set; }
14+
1215
public string? TimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";
1316
public string? To { get; set; }
1417

OpenReservation.Notification/NotificationType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public enum NotificationType
44
{
55
DingBot = 0,
66
WeChatCorpApp = 1,
7+
Email = 2,
78
}

OpenReservation.Notification/OpenReservation.Notification.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
13+
<PackageReference Include="SendGrid" Version="9.29.1" />
1314
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
1415
<PackageReference Include="WeihanLi.Common" Version="1.0.61" />
1516
<PackageReference Include="WeihanLi.Web.Extensions" Version="1.7.0" />

OpenReservation.Notification/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OpenReservation.Notification;
2+
using SendGrid;
23
using WeihanLi.Web.Authentication;
34

45
var builder = WebApplication.CreateSlimBuilder(args);
@@ -9,10 +10,13 @@
910
builder.Services.AddSwaggerGen();
1011

1112
builder.Services.AddMemoryCache();
13+
1214
builder.Services.AddHttpClient<DingBotNotification>();
1315
builder.Services.AddKeyedSingleton<INotification, DingBotNotification>(NotificationType.DingBot);
1416
builder.Services.AddHttpClient<WeChatCorpAppNotification>();
1517
builder.Services.AddKeyedSingleton<INotification, WeChatCorpAppNotification>(NotificationType.WeChatCorpApp);
18+
builder.Services.AddSingleton<ISendGridClient>(new SendGridClient(builder.Configuration.GetRequiredAppSetting("SendGridApiKey")));
19+
builder.Services.AddKeyedSingleton<INotification, SendGridEmailNotification>(NotificationType.Email);
1620

1721
builder.Services.AddAuthentication()
1822
.AddApiKey(options => options.ApiKey = builder.Configuration.GetRequiredAppSetting("AuthApiKey"));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using SendGrid;
2+
using WeihanLi.Common;
3+
4+
namespace OpenReservation.Notification;
5+
6+
public sealed class SendGridEmailNotification
7+
(IConfiguration configuration, ISendGridClient client, ILogger<SendGridEmailNotification> logger)
8+
: INotification
9+
{
10+
private readonly ILogger<SendGridEmailNotification> _logger = logger;
11+
private readonly SendGridEmailConfiguration _emailConfiguration = Guard.NotNull(configuration.GetRequiredSection("SendGrid")
12+
.Get<SendGridEmailConfiguration>());
13+
14+
public async Task SendAsync(NotificationRequest request)
15+
{
16+
var msg = SendGrid.Helpers.Mail.MailHelper.CreateSingleEmail(
17+
new SendGrid.Helpers.Mail.EmailAddress(_emailConfiguration.SourceEmail, _emailConfiguration.SourceName),
18+
new SendGrid.Helpers.Mail.EmailAddress(request.To),
19+
request.Subject ?? "Email Notification",
20+
null,
21+
request.Text
22+
);
23+
24+
var response = await client.SendEmailAsync(msg);
25+
26+
if (response.StatusCode is System.Net.HttpStatusCode.OK
27+
or System.Net.HttpStatusCode.Created
28+
or System.Net.HttpStatusCode.Accepted
29+
)
30+
{
31+
_logger.LogInformation("Email Sent for {To}, message: {Message} successfully sent",
32+
request.To, request.Text);
33+
}
34+
else
35+
{
36+
var errorMessage = await response.Body.ReadAsStringAsync();
37+
_logger.LogError("Send email response with code {StatusCode} and body {ErrorMessage}, subject: {Subject}",
38+
response.StatusCode, errorMessage, request.Subject);
39+
}
40+
}
41+
}
42+
43+
public sealed class SendGridEmailConfiguration
44+
{
45+
public string SourceEmail { get; set; } = "[email protected]";
46+
public string SourceName { get; set; } = "Notification Service";
47+
}

OpenReservation.Notification/appsettings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
"AllowedHosts": "*",
99
"AppSettings": {
1010
"AuthApiKey": "",
11-
"DingBotWebhookUrl": ""
11+
"DingBotWebhookUrl": "",
12+
"SendGridApiKey": ""
1213
},
1314
"WeChatCorpApp": {
1415
"CorpId": "ww3508de6cad12eebd",
1516
"AppId": "1000003",
1617
"AppSecret": ""
18+
},
19+
"SendGrid": {
20+
"SourceEmail": "[email protected]",
21+
"SourceName": "Notification Service"
1722
}
1823
}

0 commit comments

Comments
 (0)