...
|
...
|
@@ -19,16 +19,29 @@ import com.twilio.http.TwilioRestClient; |
19
|
19
|
import com.twilio.rest.api.v2010.account.Message;
|
20
|
20
|
import com.twilio.type.PhoneNumber;
|
21
|
21
|
import org.apache.commons.lang3.StringUtils;
|
|
22
|
+import org.thingsboard.rule.engine.api.sms.exception.SmsParseException;
|
22
|
23
|
import org.thingsboard.server.common.data.sms.config.TwilioSmsProviderConfiguration;
|
23
|
24
|
import org.thingsboard.rule.engine.api.sms.exception.SmsException;
|
24
|
25
|
import org.thingsboard.rule.engine.api.sms.exception.SmsSendException;
|
25
|
26
|
import org.thingsboard.server.service.sms.AbstractSmsSender;
|
26
|
27
|
|
|
28
|
+import java.util.regex.Pattern;
|
|
29
|
+
|
27
|
30
|
public class TwilioSmsSender extends AbstractSmsSender {
|
28
|
31
|
|
|
32
|
+ private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
|
|
33
|
+
|
29
|
34
|
private TwilioRestClient twilioRestClient;
|
30
|
35
|
private String numberFrom;
|
31
|
36
|
|
|
37
|
+ private String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
|
|
38
|
+ phoneNumber = phoneNumber.trim();
|
|
39
|
+ if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
|
|
40
|
+ throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
|
|
41
|
+ }
|
|
42
|
+ return phoneNumber;
|
|
43
|
+ }
|
|
44
|
+
|
32
|
45
|
public TwilioSmsSender(TwilioSmsProviderConfiguration config) {
|
33
|
46
|
if (StringUtils.isEmpty(config.getAccountSid()) || StringUtils.isEmpty(config.getAccountToken()) || StringUtils.isEmpty(config.getNumberFrom())) {
|
34
|
47
|
throw new IllegalArgumentException("Invalid twilio sms provider configuration: accountSid, accountToken and numberFrom should be specified!");
|
...
|
...
|
|