Skip to content

Commit 5b23070

Browse files
authored
Merge pull request #41 from AutomateThePlanet/bellatrix-twilio-integration
bellatrix-twilio-integration
2 parents 71ed8d8 + 6bea988 commit 5b23070

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

bellatrix.sms/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>solutions.bellatrix</groupId>
8+
<artifactId>bellatrix</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>bellatrix.sms</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>solutions.bellatrix</groupId>
17+
<artifactId>bellatrix.core</artifactId>
18+
<version>1.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.twilio.sdk</groupId>
22+
<artifactId>twilio</artifactId>
23+
<version>10.3.0</version>
24+
<scope>compile</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<properties>
29+
<maven.compiler.source>19</maven.compiler.source>
30+
<maven.compiler.target>19</maven.compiler.target>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
</properties>
33+
34+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.sms;
15+
16+
import com.twilio.rest.api.v2010.account.Message;
17+
import lombok.Getter;
18+
19+
@Getter
20+
public class SmsEventArgs {
21+
private SmsListener smsListener;
22+
private Message message;
23+
24+
public SmsEventArgs(SmsListener smsListener, Message message) {
25+
this.smsListener = smsListener;
26+
this.message = message;
27+
}
28+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.sms;
15+
16+
import com.twilio.rest.api.v2010.account.Message;
17+
import com.twilio.rest.api.v2010.account.MessageReader;
18+
import solutions.bellatrix.core.plugins.EventListener;
19+
20+
import java.time.Instant;
21+
import java.time.ZoneId;
22+
import java.time.ZonedDateTime;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.concurrent.Executors;
26+
import java.util.concurrent.ScheduledExecutorService;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.function.Predicate;
29+
30+
public class SmsListener {
31+
public static EventListener<SmsEventArgs> MESSAGE_RECEIVED = new EventListener<>();
32+
33+
private String phoneNumber;
34+
private final List<Message> messages = new ArrayList<>();
35+
private ScheduledExecutorService scheduler;
36+
private Instant start;
37+
38+
public SmsListener(String phoneNumber) {
39+
this.phoneNumber = phoneNumber;
40+
}
41+
42+
public SmsListener() {
43+
}
44+
45+
public List<Message> getMessages() {
46+
return new ArrayList<>(messages);
47+
}
48+
49+
public Message getMessage(Predicate<Message> condition) {
50+
return messages.stream().filter(condition).findFirst().orElse(null);
51+
}
52+
53+
public Message getFirstMessage() {
54+
return messages.get(0);
55+
}
56+
57+
public Message getLastMessage() {
58+
return messages.get(messages.size() - 1);
59+
}
60+
61+
public Message getLastMessage(Predicate<Message> condition) {
62+
var foundMessages = messages.stream().filter(condition).toList();
63+
return foundMessages.get(foundMessages.size() - 1);
64+
}
65+
66+
public void listen() {
67+
start = Instant.now();
68+
this.scheduler = Executors.newScheduledThreadPool(1);
69+
this.scheduler.schedule(this::checkForMessages, 0, TimeUnit.MILLISECONDS);
70+
}
71+
72+
public void stopListening() {
73+
if (this.scheduler != null) {
74+
this.scheduler.shutdownNow();
75+
this.scheduler = null;
76+
}
77+
}
78+
79+
private void checkForMessages() {
80+
var messageReader = new MessageReader()
81+
.setDateSentAfter(ZonedDateTime.ofInstant(start, ZoneId.of("UTC")));
82+
if (phoneNumber != null && !phoneNumber.isBlank()) messageReader.setFrom(phoneNumber);
83+
84+
var foundMessages = messageReader.read();
85+
86+
while (foundMessages.iterator().hasNext()) {
87+
var message = foundMessages.iterator().next();
88+
messages.add(message);
89+
MESSAGE_RECEIVED.broadcast(new SmsEventArgs(this, message));
90+
}
91+
92+
start = Instant.now();
93+
}
94+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.sms;
15+
16+
import com.twilio.Twilio;
17+
import com.twilio.rest.api.v2010.account.Message;
18+
import lombok.experimental.UtilityClass;
19+
import solutions.bellatrix.core.configuration.ConfigurationService;
20+
21+
import java.util.List;
22+
23+
@UtilityClass
24+
public class SmsService {
25+
private static final TwilioSettings settings = ConfigurationService.get(TwilioSettings.class);
26+
27+
static {
28+
Twilio.init(settings.getAccountSID(), settings.getAuthToken());
29+
}
30+
31+
public static SmsListener listenForSms(String fromNumber) {
32+
var smsListener = new SmsListener(fromNumber);
33+
smsListener.listen();
34+
return smsListener;
35+
}
36+
37+
public static SmsListener listenForSms() {
38+
var smsListener = new SmsListener();
39+
smsListener.listen();
40+
return smsListener;
41+
}
42+
43+
public static void stopListeningForSms(SmsListener smsListener) {
44+
smsListener.stopListening();
45+
}
46+
47+
public static List<Message> getMessages(SmsListener smsListener) {
48+
return smsListener.getMessages();
49+
}
50+
51+
public static Message getFirstMessage(SmsListener smsListener) {
52+
return smsListener.getFirstMessage();
53+
}
54+
55+
public static Message getLastMessage(SmsListener smsListener) {
56+
return smsListener.getLastMessage();
57+
}
58+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.sms;
15+
16+
import lombok.Getter;
17+
import lombok.NoArgsConstructor;
18+
import lombok.Setter;
19+
20+
@Getter @Setter @NoArgsConstructor
21+
public class TwilioSettings {
22+
private String accountSID;
23+
private String authToken;
24+
private String phoneNumber;
25+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<!-- Getting Started -->
3030
<module>getting-started/bellatrix.web.getting.started</module>
3131
<module>getting-started/bellatrix.playwright.getting.started</module>
32+
<module>bellatrix.sms</module>
3233
</modules>
3334

3435
<properties>

0 commit comments

Comments
 (0)