initial commit
All checks were successful
Build and Publish / Build Yale Access Backend (push) Successful in 28s
Build and Publish / Build Yale Access Frontend (push) Successful in 47s
Build and Publish / Push Yale Access Backend Docker Image (push) Successful in 9s
Build and Publish / Push Yale Access Frontend Docker Image (push) Successful in 10s
All checks were successful
Build and Publish / Build Yale Access Backend (push) Successful in 28s
Build and Publish / Build Yale Access Frontend (push) Successful in 47s
Build and Publish / Push Yale Access Backend Docker Image (push) Successful in 9s
Build and Publish / Push Yale Access Frontend Docker Image (push) Successful in 10s
This commit is contained in:
35
packages/backend/Services/SMSService.cs
Normal file
35
packages/backend/Services/SMSService.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using Twilio;
|
||||
using Twilio.Rest.Api.V2010.Account;
|
||||
using YaleAccess.Models.Options;
|
||||
|
||||
namespace YaleAccess.Services
|
||||
{
|
||||
public class SMSService
|
||||
{
|
||||
private readonly TwiloOptions _twiloOptions;
|
||||
|
||||
public SMSService(IOptions<TwiloOptions> twiloOptions)
|
||||
{
|
||||
_twiloOptions = twiloOptions.Value;
|
||||
}
|
||||
|
||||
public async Task SendCodeViaSMSAsync(string code, string phoneNumber)
|
||||
{
|
||||
// Create a Twilio client
|
||||
TwilioClient.Init(_twiloOptions.AccountSid, _twiloOptions.AuthToken);
|
||||
|
||||
// Send the message
|
||||
var message = await MessageResource.CreateAsync(
|
||||
body: $"{_twiloOptions.Message} {code}",
|
||||
from: new Twilio.Types.PhoneNumber(_twiloOptions.FromNumber),
|
||||
to: new Twilio.Types.PhoneNumber(phoneNumber)
|
||||
);
|
||||
|
||||
// Log the message
|
||||
Log.Logger.Information("SMS sent to {PhoneNumber} with message SID {MessageSid}.", phoneNumber, message.Sid);
|
||||
Log.Logger.Debug("SMS message: {MessageBody}", message.Body);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user