Twilio SMS provider added support Phone Number's SID or Messaging Service SID

This commit is contained in:
Vladyslav_Prykhodko 2021-03-01 19:16:13 +02:00
parent 7237497946
commit c81854e614
6 changed files with 24 additions and 11 deletions

View File

@ -25,6 +25,7 @@ import java.util.regex.Pattern;
public abstract class AbstractSmsSender implements SmsSender { public abstract class AbstractSmsSender implements SmsSender {
private static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$"); private static final Pattern E_164_PHONE_NUMBER_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
private static final Pattern PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID = Pattern.compile("^(PN|MG).*$");
private static final int MAX_SMS_MESSAGE_LENGTH = 1600; private static final int MAX_SMS_MESSAGE_LENGTH = 1600;
private static final int MAX_SMS_SEGMENT_LENGTH = 70; private static final int MAX_SMS_SEGMENT_LENGTH = 70;
@ -37,6 +38,14 @@ public abstract class AbstractSmsSender implements SmsSender {
return phoneNumber; return phoneNumber;
} }
protected String validatePhoneTwilioNumber(String phoneNumber) throws SmsParseException {
phoneNumber = phoneNumber.trim();
if (!E_164_PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches() && !PHONE_NUMBERS_SID_MESSAGE_SERVICE_SID.matcher(phoneNumber).matches()) {
throw new SmsParseException("Invalid phone number format. Phone number must be in E.164 format/Phone Number's SID/Messaging Service SID.");
}
return phoneNumber;
}
protected String prepareMessage(String message) { protected String prepareMessage(String message) {
message = message.replaceAll("^\"|\"$", "").replaceAll("\\\\n", "\n"); message = message.replaceAll("^\"|\"$", "").replaceAll("\\\\n", "\n");
if (message.length() > MAX_SMS_MESSAGE_LENGTH) { if (message.length() > MAX_SMS_MESSAGE_LENGTH) {

View File

@ -33,7 +33,7 @@ public class TwilioSmsSender extends AbstractSmsSender {
if (StringUtils.isEmpty(config.getAccountSid()) || StringUtils.isEmpty(config.getAccountToken()) || StringUtils.isEmpty(config.getNumberFrom())) { if (StringUtils.isEmpty(config.getAccountSid()) || StringUtils.isEmpty(config.getAccountToken()) || StringUtils.isEmpty(config.getNumberFrom())) {
throw new IllegalArgumentException("Invalid twilio sms provider configuration: accountSid, accountToken and numberFrom should be specified!"); throw new IllegalArgumentException("Invalid twilio sms provider configuration: accountSid, accountToken and numberFrom should be specified!");
} }
this.numberFrom = this.validatePhoneNumber(config.getNumberFrom()); this.numberFrom = this.validatePhoneTwilioNumber(config.getNumberFrom());
this.twilioRestClient = new TwilioRestClient.Builder(config.getAccountSid(), config.getAccountToken()).build(); this.twilioRestClient = new TwilioRestClient.Builder(config.getAccountSid(), config.getAccountToken()).build();
} }

View File

@ -18,14 +18,14 @@
<form [formGroup]="twilioSmsProviderConfigurationFormGroup" style="padding-bottom: 16px;"> <form [formGroup]="twilioSmsProviderConfigurationFormGroup" style="padding-bottom: 16px;">
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label translate>admin.number-from</mat-label> <mat-label translate>admin.number-from</mat-label>
<input type="tel" required [pattern]="phoneNumberPattern" matInput formControlName="numberFrom"> <input type="tel" required [pattern]="phoneNumberPatternTwilio" matInput formControlName="numberFrom">
<mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('numberFrom').hasError('required')"> <mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('numberFrom').hasError('required')">
{{ 'admin.number-from-required' | translate }} {{ 'admin.number-from-required' | translate }}
</mat-error> </mat-error>
<mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('numberFrom').hasError('pattern')"> <mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('numberFrom').hasError('pattern')">
{{ 'admin.phone-number-pattern' | translate }} {{ 'admin.phone-number-pattern-twilio' | translate }}
</mat-error> </mat-error>
<mat-hint innerHTML="{{ 'admin.phone-number-hint' | translate }}"></mat-hint> <mat-hint innerHTML="{{ 'admin.phone-number-hint-twilio' | translate }}"></mat-hint>
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label translate>admin.twilio-account-sid</mat-label> <mat-label translate>admin.twilio-account-sid</mat-label>
@ -36,7 +36,7 @@
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label translate>admin.twilio-account-token</mat-label> <mat-label translate>admin.twilio-account-token</mat-label>
<input required type="password" matInput formControlName="accountToken"> <input required type="password" autocomplete="new-password" matInput formControlName="accountToken">
<mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('accountToken').hasError('required')"> <mat-error *ngIf="twilioSmsProviderConfigurationFormGroup.get('accountToken').hasError('required')">
{{ 'admin.twilio-account-token-required' | translate }} {{ 'admin.twilio-account-token-required' | translate }}
</mat-error> </mat-error>

View File

@ -21,8 +21,9 @@ import { AppState } from '@app/core/core.state';
import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { isDefinedAndNotNull } from '@core/utils'; import { isDefinedAndNotNull } from '@core/utils';
import { import {
phoneNumberPattern, phoneNumberPatternTwilio,
SmsProviderConfiguration, SmsProviderType, SmsProviderConfiguration,
SmsProviderType,
TwilioSmsProviderConfiguration TwilioSmsProviderConfiguration
} from '@shared/models/settings.models'; } from '@shared/models/settings.models';
@ -40,7 +41,7 @@ export class TwilioSmsProviderConfigurationComponent implements ControlValueAcce
twilioSmsProviderConfigurationFormGroup: FormGroup; twilioSmsProviderConfigurationFormGroup: FormGroup;
phoneNumberPattern = phoneNumberPattern; phoneNumberPatternTwilio = phoneNumberPatternTwilio;
private requiredValue: boolean; private requiredValue: boolean;
@ -71,9 +72,9 @@ export class TwilioSmsProviderConfigurationComponent implements ControlValueAcce
ngOnInit() { ngOnInit() {
this.twilioSmsProviderConfigurationFormGroup = this.fb.group({ this.twilioSmsProviderConfigurationFormGroup = this.fb.group({
numberFrom: [null, [Validators.required, Validators.pattern(phoneNumberPattern)]], numberFrom: [null, [Validators.required, Validators.pattern(phoneNumberPatternTwilio)]],
accountSid: [null, [Validators.required]], accountSid: [null, Validators.required],
accountToken: [null, [Validators.required]] accountToken: [null, Validators.required]
}); });
this.twilioSmsProviderConfigurationFormGroup.valueChanges.subscribe(() => { this.twilioSmsProviderConfigurationFormGroup.valueChanges.subscribe(() => {
this.updateModel(); this.updateModel();

View File

@ -65,6 +65,7 @@ export interface UpdateMessage {
} }
export const phoneNumberPattern = /^\+[1-9]\d{1,14}$/; export const phoneNumberPattern = /^\+[1-9]\d{1,14}$/;
export const phoneNumberPatternTwilio = /^\+[1-9]\d{1,14}$|^(MG|PN).*$/;
export enum SmsProviderType { export enum SmsProviderType {
AWS_SNS = 'AWS_SNS', AWS_SNS = 'AWS_SNS',

View File

@ -122,7 +122,9 @@
"number-to": "Phone Number To", "number-to": "Phone Number To",
"number-to-required": "Phone Number To is required.", "number-to-required": "Phone Number To is required.",
"phone-number-hint": "Phone Number in E.164 format, ex. +19995550123", "phone-number-hint": "Phone Number in E.164 format, ex. +19995550123",
"phone-number-hint-twilio": "Phone Number in E.164 format/Phone Number's SID/Messaging Service SID, ex. +19995550123/PNXXX/MGXXX",
"phone-number-pattern": "Invalid phone number. Should be in E.164 format, ex. +19995550123.", "phone-number-pattern": "Invalid phone number. Should be in E.164 format, ex. +19995550123.",
"phone-number-pattern-twilio": "Invalid phone number. Should be in E.164 format/Phone Number's SID/Messaging Service SID, ex. +19995550123/PNXXX/MGXXX.",
"sms-message": "SMS message", "sms-message": "SMS message",
"sms-message-required": "SMS message is required.", "sms-message-required": "SMS message is required.",
"sms-message-max-length": "SMS message can't be longer 1600 characters", "sms-message-max-length": "SMS message can't be longer 1600 characters",