yale-user-access/packages/backend/Services/SMSService.cs
Liam Pietralla f577617b4d
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
initial commit
2025-01-10 08:37:18 +11:00

36 lines
1.1 KiB
C#

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);
}
}
}