-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
25 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 15 additions & 17 deletions
32
src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Server/Services/SmsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters