Last active
November 29, 2017 15:51
-
-
Save CcDora/b361b1c43e9327f69715177d8b49ff8f to your computer and use it in GitHub Desktop.
Python AWS Lambda function: processes input data from Amazon Lex and forwards the data to recipient's email address using AWS Simple Email Service (SES).
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
#Invoke this AWS Lambda function with an input event similar to the sample in testInputData.txt file | |
import logging | |
import boto3 | |
from botocore.exceptions import ClientError | |
import os | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
def lambda_handler(event, context): | |
logger.debug('Entering Lex data processing') | |
logger.debug('Printing received event data') | |
logger.debug(event) | |
# Define constants | |
SENDER = "Helpdesk Caller <sender@domain.com>" | |
RECIPIENT = "recepient@domain.com" | |
AWS_REGION = "us-east-1" | |
CHARSET = "UTF-8" | |
# Parse data from Lex bot | |
ticketSubject = event['currentIntent']['slots']['SubjectLine'] | |
ticketText = event['currentIntent']['slots']['Text'] | |
ticketProductCategory = event['currentIntent']['slots']['TicketProduct'] | |
callerRegion = event['currentIntent']['slots']['City'] | |
openTicketConfirmationStatus = event['currentIntent']['confirmationStatus'] | |
logger.debug('Data picked up from lex ' + ticketSubject | |
+ ticketText + ticketProductCategory + callerRegion | |
+ openTicketConfirmationStatus) | |
# Build e-mail content | |
emailSubject = ticketSubject | |
emailBody = """ | |
<html> | |
<head></head> | |
<body> | |
Hi Support,<br><br> | |
A new ticket request has arrived in your queue. | |
<p><b>Ticket category: </b>%s | |
</p><p><b>Caller region: </b>%s | |
</p><p><b>Ticket content: </b>%s | |
</p><p>Please try to handle the request as soon as possible. | |
</p><p><i>--Self-service-ticketing-system--</i></p> | |
</body> | |
</html>""" % (ticketProductCategory, callerRegion, ticketText) | |
logger.debug(emailBody) | |
# Send email using SES resource | |
sesClient = boto3.client('ses',region_name=AWS_REGION) | |
try: | |
response = sesClient.send_email( | |
Destination={ | |
'ToAddresses': [ | |
RECIPIENT, | |
], | |
}, | |
Message={ | |
'Body': { | |
'Html': { | |
'Charset': CHARSET, | |
'Data': emailBody, | |
}, | |
}, | |
'Subject': { | |
'Charset': CHARSET, | |
'Data': emailSubject, | |
}, | |
}, | |
Source=SENDER, | |
) | |
# Display an error if something goes wrong. | |
except ClientError as e: | |
print(e.response['Error']['Message']) | |
else: | |
print("Email sent! Message ID:"), | |
print(response['ResponseMetadata']['RequestId']) | |
# Return sucess to Amazon Connect IVR | |
return{ | |
"dialogAction": | |
{"type": "Close", | |
"fulfillmentState": "Fulfilled", | |
"message": { | |
"contentType": "PlainText", | |
"content": "Your ticket data has been saved." | |
} | |
} | |
} |
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
{ | |
"currentIntent": { | |
"dialogState": "ReadyForFulfillment", | |
"intentName": "CollectTicketProperties", | |
"message": null, | |
"responseCard": null, | |
"slotToElicit": null, | |
"slots": { | |
"City": "Boston", | |
"SubjectLine": "Not able to use the webchat", | |
"Text": "I can't reach the client webchat please assist", | |
"TicketProduct": "Webchat" | |
}, | |
"confirmationStatus": "confirmed" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment