Skip to content

Commit

Permalink
Send Email to employees
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Nov 29, 2023
1 parent 48647f9 commit df5e53f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public static void init() {
// Use Reflections to get all classes in the package
Reflections reflections = new Reflections(AbstractFunction.class.getPackage().getName());
final var allClasses = reflections.getSubTypesOf(AbstractFunction.class);

// Remove any further Abstract Classes
allClasses.removeIf(c -> Modifier.isAbstract(c.getModifiers()));

// Loop through each class and instantiate using the default constructor
for (var clazz : allClasses) {
try {
Expand All @@ -89,7 +89,7 @@ public static void init() {
}

} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
log.error("Error processing Function Classes",e);
log.error("Error processing Function Classes", e);
}
}
inited = true;
Expand All @@ -106,9 +106,27 @@ public static FunctionExecutor getFunctionExecuter(LexV2Event lexRequest) {
init();
}

String callingNumber = null;

if (lexRequest.getRequestAttributes() != null) {
if (lexRequest.getRequestAttributes().containsKey("x-amz-lex:channels:platform")) {
final var platform = lexRequest.getRequestAttributes().get("x-amz-lex:channels:platform");
if (platform.contains("Chime")) {
// When chime calls us, the SMA will set "callingNumber" in the session
callingNumber = lexRequest.getSessionState().getSessionAttributes() != null
? lexRequest.getSessionState().getSessionAttributes().get("callingNumber") : null;
}
}
// Check for Integration Channels (For Twilio)
if (lexRequest.getRequestAttributes().containsKey("x-amz-lex:channel-type")) {
// All the channels will populate user-id and for Twilio this will be the callers phone number
callingNumber = lexRequest.getRequestAttributes().get("x-amz-lex:user-id");
}
}

final var fromNumber = callingNumber;

final var inputMode = LexInputMode.fromString(lexRequest.getInputMode());
final var callingNumber = lexRequest.getSessionState().getSessionAttributes() != null ?
lexRequest.getSessionState().getSessionAttributes().get("callingNumber") : null;
final var list = new LinkedList<AbstractFunction>();

functions.forEach(f -> {
Expand All @@ -122,14 +140,14 @@ public static FunctionExecutor getFunctionExecuter(LexV2Event lexRequest) {
}
case SPEECH, DTMF -> {
if (func.isVoice()) {
func.setCallingNumber(callingNumber);

list.add(func);
}
}
}

func.setCallingNumber(fromNumber);
} catch (CloneNotSupportedException ex) {
log.error("Error cloning Functions",ex);
log.error("Error cloning Functions", ex);
}
});
return new FunctionExecutor(list.stream().map(f -> f.getChatFunction()).toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cloud.cleo.squareup.functions;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public Function<Request, Object> getExecutor() {
return (var r) -> {

try {
// Put the callingNumber in the subject if it exists, it might not if using lex console for example
final var subject = getCallingNumber() == null ? r.subject :
"[From " + getCallingNumber() + "] " + r.subject;
final var ses = SesClient.create();
final var id = ses.sendEmail((email) -> {
email.destination(dest -> dest.toAddresses(r.employee_email))
.message((mesg) -> {
mesg.body((body) -> {
body.text(cont -> cont.data(r.message));
}).subject(cont -> cont.data(r.subject));
}).subject(cont -> cont.data(subject));
}).source("[email protected]");
});

Expand Down

0 comments on commit df5e53f

Please sign in to comment.