Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bellatrix-twilio-integration #46

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions BellatrixTestFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.Playwright", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.Playwright.GettingStarted", "templates\Bellatrix.Playwright.GettingStarted\Bellatrix.Playwright.GettingStarted.csproj", "{EACDDFC5-A313-4981-9FFB-F41639713BB1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.SMS", "src\Bellatrix.SMS\Bellatrix.SMS.csproj", "{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bellatrix.Plugins.Jira.Zephyr", "src\Bellatrix.Plugins.Jira.Zephyr\Bellatrix.Plugins.Jira.Zephyr.csproj", "{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bellatrix.Playwright.Tests", "src\Bellatrix.Playwright.Tests\Bellatrix.Playwright.Tests.csproj", "{FC40FB5D-EABC-4A20-9695-3D52C0093FBC}"
Expand Down Expand Up @@ -1170,6 +1172,38 @@ Global
{EACDDFC5-A313-4981-9FFB-F41639713BB1}.STAGE|x64.Build.0 = Debug|Any CPU
{EACDDFC5-A313-4981-9FFB-F41639713BB1}.STAGE|x86.ActiveCfg = Debug|Any CPU
{EACDDFC5-A313-4981-9FFB-F41639713BB1}.STAGE|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|Any CPU.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|ARM.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|ARM.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x64.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x64.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x86.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x86.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x86.Build.0 = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|Any CPU.ActiveCfg = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|Any CPU.Build.0 = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|ARM.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -1273,6 +1307,7 @@ Global
{1AFF3507-F2C7-4240-A9D4-B166BEAC9B6F} = {E8AA67CB-09C6-4E4B-AFF1-0D5875903C34}
{08D46A7C-4BCC-4813-B20F-C7A9039621C9} = {1DF0F472-E4CA-4EFD-9459-F5B90E2E8E67}
{EACDDFC5-A313-4981-9FFB-F41639713BB1} = {74747D87-4B09-479B-B2FC-63F2CCAC4201}
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C} = {E8AA67CB-09C6-4E4B-AFF1-0D5875903C34}
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB} = {1DF0F472-E4CA-4EFD-9459-F5B90E2E8E67}
{FC40FB5D-EABC-4A20-9695-3D52C0093FBC} = {A48D5A62-96E5-4D44-818C-18748101CCC5}
EndGlobalSection
Expand Down
17 changes: 17 additions & 0 deletions src/Bellatrix.SMS/Bellatrix.SMS.csproj
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>
29 changes: 29 additions & 0 deletions src/Bellatrix.SMS/SmsEventArgs.cs
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;
}
}
82 changes: 82 additions & 0 deletions src/Bellatrix.SMS/SmsListener.cs
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;

private readonly string phoneNumber;
private readonly List<MessageResource> messages = new List<MessageResource>();
private CancellationTokenSource cancellationTokenSource;
private DateTime start;

public SmsListener(string phoneNumber = null)
{
this.phoneNumber = phoneNumber;
}

public List<MessageResource> GetMessages()
{
return new List<MessageResource>(messages);
}

public MessageResource GetLastMessage()
{
return messages.LastOrDefault();
}

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);
}
}
}
44 changes: 44 additions & 0 deletions src/Bellatrix.SMS/SmsService.cs
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();
}
}
22 changes: 22 additions & 0 deletions src/Bellatrix.SMS/TwilioSettings.cs
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; }
}
Loading