Skip to content

Commit

Permalink
merge results
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed May 28, 2024
2 parents 4ad066f + 2359ddb commit 23c63c8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public class EmailSettings
public class SmsSettings
{
public string? FromPhoneNumber { get; set; }
public string? AccountSid { get; set; }
public string? AuthToken { get; set; }

public string? ConnectionString { get; set; }

public bool Configured => string.IsNullOrEmpty(FromPhoneNumber) is false && string.IsNullOrEmpty(ConnectionString) is false;
public bool Configured => string.IsNullOrEmpty(FromPhoneNumber) is false &&
string.IsNullOrEmpty(AccountSid) is false &&
string.IsNullOrEmpty(AuthToken) is false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Network" Version="8.0.1" />
<PackageReference Include="Riok.Mapperly" Version="3.5.1" />
<PackageReference Include="Azure.Communication.Sms" Version="1.0.1" />
<PackageReference Include="Twilio" Version="7.1.0" />

<Using Include="Microsoft.EntityFrameworkCore.Migrations" />
<Using Include="Microsoft.EntityFrameworkCore.Metadata.Builders" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.DataProtection;
using Azure.Communication.Sms;
using Twilio;
//#endif

namespace Boilerplate.Server;
Expand Down Expand Up @@ -145,7 +145,7 @@ private static void ConfigureServices(this WebApplicationBuilder builder)
services.TryAddTransient<SmsService>();
if (appSettings.SmsSettings.Configured)
{
services.TryAddTransient(sp => new SmsClient(appSettings.SmsSettings.ConnectionString));
TwilioClient.Init(appSettings.SmsSettings.AccountSid, appSettings.SmsSettings.AuthToken);
}

//#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
using System.Net;
using Azure.Communication.Sms;
using Twilio.Rest.Api.V2010.Account;

namespace Boilerplate.Server.Services;

public partial class SmsService
{
[AutoInject] private readonly SmsClient? smsClient = null;
[AutoInject] private readonly AppSettings appSettings = default!;
[AutoInject] private readonly ILogger<SmsService> logger = default!;
[AutoInject] private readonly IHostEnvironment hostEnvironment = default!;

public async Task SendSms(string message, string phoneNumber, CancellationToken cancellationToken)
public async Task SendSms(string messageText, string phoneNumber, CancellationToken cancellationToken)
{
if (hostEnvironment.IsDevelopment())
{
LogSendSms(logger, message, phoneNumber);
LogSendSms(logger, messageText, phoneNumber);
}

if (smsClient is null) return;
if (appSettings.SmsSettings.Configured is false) return;

SmsSendResult sendResult = smsClient.Send(
from: appSettings.SmsSettings.FromPhoneNumber,
to: phoneNumber,
message: message,
options: new(enableDeliveryReport: true),
cancellationToken
);
var messageOptions = new CreateMessageOptions(new(phoneNumber))
{
From = new(appSettings.SmsSettings.FromPhoneNumber),
Body = messageText
};

var smsMessage = MessageResource.Create(messageOptions);

if (sendResult.Successful) return;
if (smsMessage.ErrorCode is null) return;

LogSendSmsFailed(logger, sendResult.To, sendResult.MessageId, (HttpStatusCode)sendResult.HttpStatusCode, sendResult.ErrorMessage);
LogSendSmsFailed(logger, phoneNumber, smsMessage.ErrorCode, smsMessage.ErrorMessage);
}

[LoggerMessage(Level = LogLevel.Information, Message = "SMS: {message} to {phoneNumber}.")]
private static partial void LogSendSms(ILogger logger, string message, string phoneNumber);

[LoggerMessage(Level = LogLevel.Error, Message = "Failed to send Sms to {phoneNumber}. Id: {id}, Status code: {statusCode}, Error message: {errorMessage}")]
private static partial void LogSendSmsFailed(ILogger logger, string phoneNumber, string id, HttpStatusCode statusCode, string errorMessage);
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to send Sms to {phoneNumber}. Code: {errorCode}, Error message: {errorMessage}")]
private static partial void LogSendSmsFailed(ILogger logger, string phoneNumber, int? errorCode, string errorMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"SmsSettings": {
"FromPhoneNumber": null,
"ConnectionString": null /* Azure communication service connection string */
"AccountSid": null, /* Twilio */
"AutoToken": null
},
"HealthCheckSettings": {
"EnableHealthChecks": false
Expand Down

0 comments on commit 23c63c8

Please sign in to comment.