-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bellatrix-visual-regression-tracker-integr…
…ation
- Loading branch information
Showing
6 changed files
with
229 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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Twilio" Version="7.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Bellatrix.Core\Bellatrix.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,29 @@ | ||
// <copyright file="TwilioMessageEventArgs.cs" company="Automate The Planet Ltd."> | ||
// Copyright 2024 Automate The Planet Ltd. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
// <author>Miriam Kyoseva</author> | ||
// <site>https://bellatrix.solutions/</site> | ||
|
||
using Twilio.Rest.Api.V2010.Account; | ||
|
||
namespace Bellatrix.SMS; | ||
|
||
public class SmsEventArgs : EventArgs | ||
{ | ||
public SmsListener SmsListener { get; } | ||
public MessageResource Message { get; } | ||
|
||
public SmsEventArgs(SmsListener smsListener, MessageResource message) | ||
{ | ||
SmsListener = smsListener; | ||
Message = message; | ||
} | ||
} |
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,82 @@ | ||
// <copyright file="SmsListener.cs" company="Automate The Planet Ltd."> | ||
// Copyright 2024 Automate The Planet Ltd. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
// <author>Miriam Kyoseva</author> | ||
// <site>https://bellatrix.solutions/</site> | ||
|
||
using Twilio.Rest.Api.V2010.Account; | ||
|
||
namespace Bellatrix.SMS; | ||
|
||
public class SmsListener | ||
{ | ||
public static event EventHandler<SmsEventArgs> MessageReceived; | ||
Check warning on line 21 in src/Bellatrix.SMS/SmsListener.cs GitHub Actions / build
|
||
|
||
private readonly string phoneNumber; | ||
private readonly List<MessageResource> messages = new List<MessageResource>(); | ||
private CancellationTokenSource cancellationTokenSource; | ||
private DateTime start; | ||
|
||
public SmsListener(string phoneNumber = null) | ||
Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs GitHub Actions / build
Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs GitHub Actions / build
Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs GitHub Actions / build
|
||
{ | ||
this.phoneNumber = phoneNumber; | ||
} | ||
|
||
public List<MessageResource> GetMessages() | ||
{ | ||
return new List<MessageResource>(messages); | ||
} | ||
|
||
public MessageResource GetLastMessage() | ||
{ | ||
return messages.LastOrDefault(); | ||
Check warning on line 40 in src/Bellatrix.SMS/SmsListener.cs GitHub Actions / build
|
||
} | ||
|
||
public void Listen() | ||
{ | ||
start = DateTime.UtcNow; | ||
cancellationTokenSource = new CancellationTokenSource(); | ||
Task.Run(() => CheckForMessages(cancellationTokenSource.Token)); | ||
} | ||
|
||
public void StopListening() | ||
{ | ||
if (cancellationTokenSource != null) | ||
{ | ||
cancellationTokenSource.Cancel(); | ||
cancellationTokenSource.Dispose(); | ||
} | ||
} | ||
|
||
private async Task CheckForMessages(CancellationToken cancellationToken) | ||
{ | ||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
var messageReader = MessageResource.ReadAsync( | ||
dateSentAfter: start, | ||
from: phoneNumber, | ||
limit: 1 | ||
); | ||
|
||
var foundMessages = await messageReader; | ||
|
||
if (foundMessages.Any()) | ||
{ | ||
var message = foundMessages.First(); | ||
messages.Add(message); | ||
MessageReceived?.Invoke(this, new SmsEventArgs(this, message)); | ||
start = DateTime.UtcNow; | ||
} | ||
|
||
await Task.Delay(TimeSpan.FromMilliseconds(500), cancellationToken); | ||
} | ||
} | ||
} |
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,44 @@ | ||
using Twilio; | ||
using Twilio.Rest.Api.V2010.Account; | ||
|
||
namespace Bellatrix.SMS; | ||
|
||
public class SmsService | ||
{ | ||
private static readonly TwilioSettings settings; | ||
|
||
static SmsService() | ||
{ | ||
settings = ConfigurationService.GetSection<TwilioSettings>(); | ||
TwilioClient.Init(settings.AccountSID, settings.AuthToken); | ||
} | ||
|
||
public static SmsListener ListenForSms(string fromNumber) | ||
{ | ||
var smsListener = new SmsListener(fromNumber); | ||
smsListener.Listen(); | ||
return smsListener; | ||
} | ||
|
||
public static SmsListener ListenForSms() | ||
{ | ||
var smsListener = new SmsListener(); | ||
smsListener.Listen(); | ||
return smsListener; | ||
} | ||
|
||
public static void StopListeningForSms(SmsListener smsListener) | ||
{ | ||
smsListener.StopListening(); | ||
} | ||
|
||
public static List<MessageResource> GetMessages(SmsListener smsListener) | ||
{ | ||
return smsListener.GetMessages(); | ||
} | ||
|
||
public static MessageResource GetLastMessage(SmsListener smsListener) | ||
{ | ||
return smsListener.GetLastMessage(); | ||
} | ||
} |
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,22 @@ | ||
// <copyright file="TwilioSettings.cs" company="Automate The Planet Ltd."> | ||
// Copyright 2024 Automate The Planet Ltd. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
// <author>Miriam Kyoseva</author> | ||
// <site>https://bellatrix.solutions/</site> | ||
|
||
namespace Bellatrix.SMS; | ||
|
||
public class TwilioSettings | ||
{ | ||
public string? AccountSID { get; set; } | ||
public string? AuthToken { get; set; } | ||
public string? PhoneNumber { get; set; } | ||
} |