Skip to content

Commit

Permalink
Driving directions to store (add US number validation)
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Nov 29, 2023
1 parent d2262ba commit fc1f1e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -170,6 +171,23 @@ private ChatFunction getChatFunction() {
.executor(getRequestClass(), getExecutor())
.build();
}


private static final Pattern US_E164_PATTERN = Pattern.compile("^\\+1[2-9]\\d{2}[2-9]\\d{6}$");

/**
* Is the given number a valid US Phone number
*
* @param number
* @return
*/
protected static boolean isValidUSE164Number(String number) {
if (number == null || number.isEmpty()) {
return false;
}
return US_E164_PATTERN.matcher(number).matches();
}


/**
* Override and return false to disable a particular function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cloud.cleo.squareup.functions;

import java.math.BigDecimal;
import java.util.function.Function;
import software.amazon.awssdk.services.sns.SnsClient;

Expand All @@ -19,15 +20,25 @@ protected String getDescription() {
protected Function getExecutor() {
return (var r) -> {
try {
final var result = SnsClient.create().publish(b -> b.phoneNumber(getCallingNumber()).message(DRIVING_DIRECTIONS_URL).build());
log.info("SMS Directions sent to " + getCallingNumber() + " with SNS id of " + result.messageId());
return mapper.createObjectNode().put("status", "The directions have been sent");
final var callingNumber = getCallingNumber();

// Only send SMS to validated US Phone Numbers
if ( ! isValidUSE164Number(callingNumber) ) {
return mapper.createObjectNode().put("status","FAILED").put("message", "Calling number is not a valid US phone number");
}

final var result = SnsClient.create().publish(b -> b.phoneNumber(callingNumber).message(DRIVING_DIRECTIONS_URL).build());
log.info("SMS Directions sent to " + callingNumber + " with SNS id of " + result.messageId());
return mapper.createObjectNode().put("status","SUCCESS").put("message", "The directions have been sent");
} catch (Exception e) {
log.error("Could not send Directions via SMS to caller",e);
return mapper.createObjectNode().put("status", "An error has occurred, this function may be down");
return mapper.createObjectNode().put("status","FAILED").put("message", "An error has occurred, this function may be down");
}
};
}




/**
* This function is Voice only so don't use this for text interface
Expand Down

0 comments on commit fc1f1e5

Please sign in to comment.