From fc1f1e55ac517d2440e35206624cb140ccc18bb6 Mon Sep 17 00:00:00 2001 From: Steve Jensen Date: Wed, 29 Nov 2023 04:55:55 -0600 Subject: [PATCH] Driving directions to store (add US number validation) --- .../squareup/functions/AbstractFunction.java | 18 ++++++++++++++++++ .../functions/DrivingDirectionsVoice.java | 19 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/ChatGPT/src/main/java/cloud/cleo/squareup/functions/AbstractFunction.java b/ChatGPT/src/main/java/cloud/cleo/squareup/functions/AbstractFunction.java index fc38f45..cb28003 100644 --- a/ChatGPT/src/main/java/cloud/cleo/squareup/functions/AbstractFunction.java +++ b/ChatGPT/src/main/java/cloud/cleo/squareup/functions/AbstractFunction.java @@ -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; @@ -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 diff --git a/ChatGPT/src/main/java/cloud/cleo/squareup/functions/DrivingDirectionsVoice.java b/ChatGPT/src/main/java/cloud/cleo/squareup/functions/DrivingDirectionsVoice.java index 3817d4a..78b9a7f 100644 --- a/ChatGPT/src/main/java/cloud/cleo/squareup/functions/DrivingDirectionsVoice.java +++ b/ChatGPT/src/main/java/cloud/cleo/squareup/functions/DrivingDirectionsVoice.java @@ -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; @@ -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