2021-09-22

How to send Email from IVR in Amazon Connect

 In my previous article, I gave an example of how to send SMS from IVR in Amazon Connect.

This time I will demonstrate how to send an email.

Why someone might need this feature? For example, you can send instructions to contact center clients when they select a certain option in IVR menu.

The process is similar to sending SMS:

1) Client calls IVR and selects an option to receive information by Email

2) After that you have to identify the client in your database. In my example, I identify client simply by caller number, but in your case, you can ask client to enter  client id.

3) Also database has to identify the email address associated with the client.

4) Once the client is identified and we know the email address we can send an email using Amazon Simple Email Service (SES). Alternatively, you can send an email using Amazon Pinpoint. In my example I use SES.

Here is the full process:

Step 1.

Create DynamoDB table Clients that contains information for each client, including phone number and email address.

Phone number must be in E.164 format.


Step 2.

In order to be able to send emails using Amazon SES you need to add an Email Address to it that you will use as Mail From.

In my next step MailFrom variable contains an email address that I already added to Amazon SES.

Step 3.

I created Lambda function using Python 3.9 which is called sendSMS. 

What it does:

1) gets contact attribute with caller number

2) does query in Clients table and returns Email address of the person

3) If the client is identified and we have his email address we can now send an email message using Amazon SES

Here is the full text of Lambda function:

import json
import boto3
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):
    
    
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('Clients')
    
    CLID=event['Details']['ContactData']['CustomerEndpoint']['Address']
   
    
    response = table.query(
        KeyConditionExpression=Key('phoneNumber').eq(CLID)
        )
    resp=response['Items']
    
    for item in resp:
            Email=item['Email']
    

    MailFrom = "support@yourcompany.com"
    MailTo = Email
    MailSubject = "New application instructions"
    
    # Non-HTML Body
    MailBody = ("New application instructions\r\n"
                 "Access following link to get installation instructions http://tinyurl.com/AmazonConnectSMS"             
                )
                
    # HTML Body
    MailBodyHTML = """<html>
    <head></head>
    <body>
      <h1>New application instrunctions</h1>
      <p>Access following link to get installation instructions
        <a href='http://tinyurl.com/AmazonConnectSMS'>New application</a>
        </p>
    </body>
    </html>
    """            
    
 
    client = boto3.client('ses',region_name="us-east-1")

    # Sending email
    try:
        #Provide the contents of the email.
        response = client.send_email(
            Destination={
                'ToAddresses': [
                    MailTo,
                ],
            },
            Message={
                'Body': {
                    'Html': {
                        'Charset': "UTF-8",
                        'Data': MailBodyHTML,
                    },
                    'Text': {
                        'Charset': "UTF-8",
                        'Data': MailBody,
                    },
                },
                'Subject': {
                    'Charset': "UTF-8",
                    'Data': MailSubject,
                },
            },
            Source=MailFrom,
        )
 
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print("Email sent! Message ID:"),
        print(response['MessageId'])
        return {
            'statusCode': 200,
            'body': json.dumps('OK')
        }


Step 4.

Here is my sample call flow that sends email.

In the IVR I ask clients:

"Hello. Thank you for calling IT Helpdesk.
Please press 1 to receive an email with instructions on how to setup a new application."

if the client pressed 1 I invoke my Lambda function sendEmail.






2 comments:

  1. Thanks for great post
    Was having issues with making the lambda work , was missing attaching role for lambda to access SES and Dynamo

    ReplyDelete