-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
314 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
ChatGPT/src/main/java/cloud/cleo/squareup/ChatGPTLambdaLex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cloud.cleo.squareup; | ||
|
||
import static cloud.cleo.squareup.ChatGPTLambda.mapper; | ||
import static cloud.cleo.squareup.lang.LangUtil.LanguageIds.UNHANDLED_EXCEPTION; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.LexV2Event; | ||
import com.amazonaws.services.lambda.runtime.events.LexV2Response; | ||
import java.util.concurrent.CompletionException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* Process incoming Lex Events | ||
* | ||
* | ||
* @author sjensen | ||
*/ | ||
public class ChatGPTLambdaLex extends ChatGPTLambda implements RequestHandler<LexV2Event, LexV2Response> { | ||
|
||
// Initialize the Log4j logger. | ||
Logger log = LogManager.getLogger(ChatGPTLambdaLex.class); | ||
|
||
@Override | ||
public LexV2Response handleRequest(LexV2Event lexRequest, Context cntxt) { | ||
// Wrapped Event Class | ||
final LexV2EventWrapper event = new LexV2EventWrapper(lexRequest); | ||
try { | ||
log.debug(mapper.valueToTree(lexRequest).toPrettyString()); | ||
// Intent which doesn't matter for us | ||
log.debug("Intent: " + event.getIntent()); | ||
|
||
// For this use case, we only ever get the FallBack Intent, so the intent name means nothing here | ||
// We will process everythiung coming in as text to pass to GPT | ||
// IE, we are only using lex here to process speech and send it to us | ||
return switch (event.getIntent()) { | ||
default -> | ||
processGPT(event); | ||
}; | ||
|
||
} catch (CompletionException e) { | ||
log.error("Unhandled Future Exception", e.getCause()); | ||
return buildResponse(new LexV2EventWrapper(lexRequest), event.getLangString(UNHANDLED_EXCEPTION)); | ||
} catch (Exception e) { | ||
log.error("Unhandled Exception", e); | ||
// Unhandled Exception | ||
return buildResponse(new LexV2EventWrapper(lexRequest), event.getLangString(UNHANDLED_EXCEPTION)); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
113 changes: 113 additions & 0 deletions
113
ChatGPT/src/main/java/cloud/cleo/squareup/ChatGPTLambdaPinpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package cloud.cleo.squareup; | ||
|
||
import static cloud.cleo.squareup.lang.LangUtil.LanguageIds.UNHANDLED_EXCEPTION; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.LexV2Response; | ||
import com.amazonaws.services.lambda.runtime.events.SNSEvent; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.concurrent.CompletionException; | ||
import lombok.Data; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.sns.SnsAsyncClient; | ||
|
||
/** | ||
* Process incoming SMS from Pinpoint and respond as another Channel Type. | ||
* | ||
* https://docs.aws.amazon.com/sms-voice/latest/userguide/phone-numbers-two-way-sms.html | ||
* | ||
* @author sjensen | ||
*/ | ||
public class ChatGPTLambdaPinpoint extends ChatGPTLambda implements RequestHandler<SNSEvent, Void> { | ||
|
||
// Initialize the Log4j logger. | ||
Logger log = LogManager.getLogger(ChatGPTLambdaPinpoint.class); | ||
|
||
final static SnsAsyncClient snsAsyncClient = SnsAsyncClient.builder() | ||
// Force SMS sending to east because that's where all the 10DLC and campaign crap setup is done | ||
// Otherwise have to pay for registrations and numbers in 2 regions, HUGE HASSLE (and more monthly cost) | ||
// Also then all texts are sourced from the same phone number for consistancy | ||
.region(Region.US_EAST_1) | ||
.httpClient(crtAsyncHttpClient) | ||
.build(); | ||
|
||
@Override | ||
public Void handleRequest(SNSEvent input, Context cntxt) { | ||
// Only 1 record is every presented | ||
SNSEvent.SNS snsEvent = input.getRecords().get(0).getSns(); | ||
log.debug("Recieved SNS Event" + snsEvent); | ||
|
||
// Convert payload to Pinpoint Event | ||
final var ppe = mapper.convertValue(snsEvent, PinpointEvent.class); | ||
|
||
// Wrapped Event Class | ||
final LexV2EventWrapper event = new LexV2EventWrapper(ppe); | ||
LexV2Response response; | ||
try { | ||
response = processGPT(event); | ||
} catch (CompletionException e) { | ||
log.error("Unhandled Future Exception", e.getCause()); | ||
response = buildResponse(event, event.getLangString(UNHANDLED_EXCEPTION)); | ||
} catch (Exception e) { | ||
log.error("Unhandled Exception", e); | ||
// Unhandled Exception | ||
response = buildResponse(event, event.getLangString(UNHANDLED_EXCEPTION)); | ||
} | ||
|
||
// Take repsonse body message from the LexV2Reponse and respond to SMS via SNS | ||
final var botResponse = response.getMessages()[0].getContent(); | ||
final var result = snsAsyncClient.publish(b -> b.phoneNumber(ppe.getOriginationNumber()) | ||
.message(botResponse)) | ||
.join(); | ||
log.info("SMS Bot Response sent to " + ppe.getOriginationNumber() + " with SNS id of " + result.messageId()); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* The SNS payload we will receive from Incoming SMS messages. | ||
* | ||
*/ | ||
@Data | ||
public static class PinpointEvent { | ||
|
||
/** | ||
* The phone number that sent the incoming message to you (in other words, your customer's phone number). | ||
*/ | ||
@JsonProperty(value = "originationNumber") | ||
private String originationNumber; | ||
|
||
/** | ||
* The phone number that the customer sent the message to (your dedicated phone number). | ||
*/ | ||
@JsonProperty(value = "destinationNumber") | ||
private String destinationNumber; | ||
|
||
/** | ||
* The registered keyword that's associated with your dedicated phone number. | ||
*/ | ||
@JsonProperty(value = "messageKeyword") | ||
private String messageKeyword; | ||
|
||
/** | ||
* The message that the customer sent to you. | ||
*/ | ||
@JsonProperty(value = "messageBody") | ||
private String messageBody; | ||
|
||
/** | ||
* The unique identifier for the incoming message. | ||
*/ | ||
@JsonProperty(value = "inboundMessageId") | ||
private String inboundMessageId; | ||
|
||
/** | ||
* The unique identifier of the message that the customer is responding to. | ||
*/ | ||
@JsonProperty(value = "previousPublishedMessageId") | ||
private String previousPublishedMessageId; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.