-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Got outbound texts, generated from email responses, working in suppor…
…t of #464.
- Loading branch information
1 parent
8fe97d5
commit 2076bb0
Showing
2 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using MailKit.Net.Pop3; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
|
||
using Models; | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
namespace Messaging | ||
{ | ||
public class TimedHostedService(ILogger<TimedHostedService> logger, AppSettings appSettings) : BackgroundService | ||
{ | ||
private readonly ILogger<TimedHostedService> _logger = logger; | ||
private int _executionCount; | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_logger.LogInformation("Timed Hosted Service running."); | ||
|
||
// When the timer should have no due-time, then do the work once now. | ||
await DoWork(appSettings, stoppingToken); | ||
|
||
// Most email clients refresh every 10 seconds per a brief googling. | ||
using PeriodicTimer timer = new(TimeSpan.FromSeconds(10)); | ||
|
||
try | ||
{ | ||
while (await timer.WaitForNextTickAsync(stoppingToken)) | ||
{ | ||
await DoWork(appSettings, stoppingToken); | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
_logger.LogInformation("Timed Hosted Service is stopping."); | ||
} | ||
} | ||
|
||
// Could also be a async method, that can be awaited in ExecuteAsync above | ||
private async Task DoWork(AppSettings appSettings, CancellationToken cls) | ||
{ | ||
int count = Interlocked.Increment(ref _executionCount); | ||
|
||
_logger.LogInformation("Timed Hosted Service is working. Count: {Count}", count); | ||
|
||
var contextOptions = new DbContextOptionsBuilder<MessagingContext>() | ||
.UseSqlite() | ||
.Options; | ||
using var dbContext = new MessagingContext(contextOptions); | ||
|
||
// Check for emails | ||
var emails = await EmailMessage.GetEmailsAsync(appSettings.ConnectionStrings.EmailUsername, appSettings.ConnectionStrings.EmailPassword, cls); | ||
// Transform each email to a text message | ||
foreach (var email in MemoryMarshal.ToEnumerable(emails)) | ||
{ | ||
await EmailToForwardedMessageAsync(email, dbContext); | ||
_logger.LogInformation("To {to} From {from}", email.To, email.From); | ||
} | ||
// Send the messages outbound | ||
_logger.LogInformation("Timed Hosted Service has completed. Count: {Count}", count); | ||
} | ||
|
||
private async Task EmailToForwardedMessageAsync(EmailMessage.InboundEmail email, MessagingContext messagingContext) | ||
{ | ||
var message = new SendMessageRequest() | ||
{ | ||
// Reverse from and to here, as we are responding. | ||
To = email.From, | ||
MSISDN = email.To, | ||
MediaURLs = [], | ||
Message = email.Content | ||
}; | ||
|
||
_ = await Endpoints.SendMessageAsync(message, false, appSettings, messagingContext); | ||
} | ||
} | ||
} |