Skip to content
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
34 changes: 34 additions & 0 deletions bellatrix.sms/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>bellatrix.sms</artifactId>

<dependencies>
<dependency>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix.core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>10.3.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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.
*/

package solutions.bellatrix.sms;

import com.twilio.rest.api.v2010.account.Message;
import lombok.Getter;

@Getter
public class SmsEventArgs {
private SmsListener smsListener;
private Message message;

public SmsEventArgs(SmsListener smsListener, Message message) {
this.smsListener = smsListener;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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.
*/

package solutions.bellatrix.sms;

import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.api.v2010.account.MessageReader;
import solutions.bellatrix.core.plugins.EventListener;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

public class SmsListener {
public static EventListener<SmsEventArgs> MESSAGE_RECEIVED = new EventListener<>();

private String phoneNumber;
private final List<Message> messages = new ArrayList<>();
private ScheduledExecutorService scheduler;
private Instant start;

public SmsListener(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public SmsListener() {
}

public List<Message> getMessages() {
return new ArrayList<>(messages);
}

public Message getMessage(Predicate<Message> condition) {
return messages.stream().filter(condition).findFirst().orElse(null);
}

public Message getFirstMessage() {
return messages.get(0);
}

public Message getLastMessage() {
return messages.get(messages.size() - 1);
}

public Message getLastMessage(Predicate<Message> condition) {
var foundMessages = messages.stream().filter(condition).toList();
return foundMessages.get(foundMessages.size() - 1);
}

public void listen() {
start = Instant.now();
this.scheduler = Executors.newScheduledThreadPool(1);
this.scheduler.schedule(this::checkForMessages, 0, TimeUnit.MILLISECONDS);
}

public void stopListening() {
if (this.scheduler != null) {
this.scheduler.shutdownNow();
this.scheduler = null;
}
}

private void checkForMessages() {
var messageReader = new MessageReader()
.setDateSentAfter(ZonedDateTime.ofInstant(start, ZoneId.of("UTC")));
if (phoneNumber != null && !phoneNumber.isBlank()) messageReader.setFrom(phoneNumber);

var foundMessages = messageReader.read();

while (foundMessages.iterator().hasNext()) {
var message = foundMessages.iterator().next();
messages.add(message);
MESSAGE_RECEIVED.broadcast(new SmsEventArgs(this, message));
}

start = Instant.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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.
*/

package solutions.bellatrix.sms;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import lombok.experimental.UtilityClass;
import solutions.bellatrix.core.configuration.ConfigurationService;

import java.util.List;

@UtilityClass
public class SmsService {
private static final TwilioSettings settings = ConfigurationService.get(TwilioSettings.class);

static {
Twilio.init(settings.getAccountSID(), settings.getAuthToken());
}

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<Message> getMessages(SmsListener smsListener) {
return smsListener.getMessages();
}

public static Message getFirstMessage(SmsListener smsListener) {
return smsListener.getFirstMessage();
}

public static Message getLastMessage(SmsListener smsListener) {
return smsListener.getLastMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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.
*/

package solutions.bellatrix.sms;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter @NoArgsConstructor
public class TwilioSettings {
private String accountSID;
private String authToken;
private String phoneNumber;
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<!-- Getting Started -->
<module>getting-started/bellatrix.web.getting.started</module>
<module>getting-started/bellatrix.playwright.getting.started</module>
<module>bellatrix.sms</module>
</modules>

<properties>
Expand Down