Continue to modularize even more components
This commit is contained in:
40
TwilioSMSReceiver.Common/MessageHandlers/BaseHandler.cs
Normal file
40
TwilioSMSReceiver.Common/MessageHandlers/BaseHandler.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
#nullable disable
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwilioSMSReceiver.Common.Interfaces;
|
||||
using TwilioSMSReceiver.Data;
|
||||
using TwilioSMSReceiver.Data.Models;
|
||||
|
||||
namespace TwilioSMSReceiver.Common.MessageHandlers
|
||||
{
|
||||
public abstract class BaseHandler : IMessageHandler
|
||||
{
|
||||
protected readonly SMSDbCtx sMSDbCtx;
|
||||
protected readonly ILogger<BaseHandler> _logger;
|
||||
protected readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public BaseHandler(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
sMSDbCtx = _serviceProvider.CreateScope().ServiceProvider.GetService<SMSDbCtx>();
|
||||
_logger = _serviceProvider.CreateScope().ServiceProvider.GetService<ILogger<BaseHandler>>();
|
||||
}
|
||||
public virtual async Task<bool> RelaySms(SMSModel model)
|
||||
{
|
||||
if (sMSDbCtx.SMSMessages.Any(b => b.Id == model.Id))
|
||||
{
|
||||
var message =
|
||||
sMSDbCtx.SMSMessages.Where(b => b.Id == model.Id).First();
|
||||
if (!message.IsForwardedYet)
|
||||
{
|
||||
_logger.LogInformation($"Marking message with id {message.Id} as forwarded");
|
||||
message.IsForwardedYet = true;
|
||||
sMSDbCtx.SMSMessages.Update(message);
|
||||
await sMSDbCtx.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
TwilioSMSReceiver.Common/MessageHandlers/MSTeamsHandler.cs
Normal file
53
TwilioSMSReceiver.Common/MessageHandlers/MSTeamsHandler.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using TwilioSMSReceiver.Data.Models;
|
||||
using TeamsHook.NET;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwilioSMSReceiver.Common.MessageHandlers;
|
||||
|
||||
namespace TwilioSMSReceiver.Common.Handlers
|
||||
{
|
||||
public class MSTeamsHandler : BaseHandler
|
||||
{
|
||||
public MSTeamsHandler(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<bool> RelaySms(SMSModel model)
|
||||
{
|
||||
var teams = sMSDbCtx.MSTeamsWebHooks;
|
||||
if (!teams.Any())
|
||||
{
|
||||
_logger.LogWarning("Teams Webhook list is empty!");
|
||||
return false;
|
||||
}
|
||||
List<Task> teamsPostRequests = new List<Task>();
|
||||
foreach (var teamHook in teams)
|
||||
{
|
||||
try
|
||||
{
|
||||
var teamsClient = new TeamsHookClient();
|
||||
var card = new MessageCard
|
||||
{
|
||||
Title = $"Incoming SMS from {model.SenderNumber}",
|
||||
Summary = $"Sent to {model.ReceivedNumber}",
|
||||
Text = $"{model.MessageContents}"
|
||||
};
|
||||
|
||||
teamsPostRequests.Add(teamsClient.PostAsync(teamHook.WebHookUri, card));
|
||||
} catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Webhook with URL {teamHook.WebHookUri} failed to post. Exception here {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Task.WhenAll(teamsPostRequests);
|
||||
} catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Webhook failure {ex}");
|
||||
}
|
||||
|
||||
return await base.RelaySms(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
TwilioSMSReceiver.Common/MessageHandlers/SMTPHandler.cs
Normal file
19
TwilioSMSReceiver.Common/MessageHandlers/SMTPHandler.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwilioSMSReceiver.Common.MessageHandlers;
|
||||
using TwilioSMSReceiver.Data.Models;
|
||||
|
||||
namespace TwilioSMSReceiver.Common.Handlers
|
||||
{
|
||||
public class SMTPHandler : BaseHandler
|
||||
{
|
||||
public SMTPHandler(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<bool> RelaySms(SMSModel model)
|
||||
{
|
||||
_logger.LogWarning("SMTP isn't implemented yet");
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user