How do I prevent agents in my Amazon Connect contact center from making outbound calls to certain phone numbers?

Last updated: 2020-03-25

I don't want agents in my Amazon Connect contact center to call certain countries or premium rate phone numbers. How do I prevent those calls?

Short Description

In an outbound whisper flow contact flow, invoke an AWS Lambda function that checks if a dialed phone number is allowed. If the phone number isn't allowed, stop the contact using the Amazon Connect StopContact API.

Note: This solution has some limitations:

  • It can't block queued callbacks. When a callback request is initiated from the agent's end, the contact ID that's passed through the outbound whisper flow is not the actual contact ID.
  • It can't block calls if the Lambda function fails to invoke for any reason. If the function fails, the contact follows the error branch of the contact flow and the call is not stopped.
  • It's a basic example. For your production environment, customize your setup for your use case and agents' dialing permissions.

Resolution

Create a Lambda function

Using the runtime of your choice, create a Lambda function. Include function logic that checks an incoming JSON request from Amazon Connect for the following:

  • Agent user name (under "Attributes")
  • Current contact ID ("ContactId")
  • Dialed phone number (under "CustomerEndpoint")
  • Initial contact ID ("InitialContactId")
  • The Amazon Resource Name (ARN) for the contact center instance ("InstanceARN")

For more information, see Contact Attributes Available in Amazon Connect and the following example JSON request to a Lambda function:

{
    "Details": {
        "ContactData": {
            "Attributes": {},
            "Channel": "VOICE",
            "ContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
            "CustomerEndpoint": {
                "Address": "+1234567890",
                "Type": "TELEPHONE_NUMBER"
            },
            "InitialContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
            "InitiationMethod": "INBOUND | OUTBOUND | TRANSFER | CALLBACK",
            "InstanceARN": "arn:aws:connect:aws-region:1234567890:instance/c8c0e68d-2200-4265-82c0-XXXXXXXXXX",
            "PreviousContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXX",
            "Queue": "QueueName",
            "SystemEndpoint": {
                "Address": "+1234567890",
                "Type": "TELEPHONE_NUMBER"
            }
        },
        "Parameters": {
            "sentAttributeKey": "sentAttributeValue"
        }
    },
    "Name": "ContactFlowEvent"
}

Tip: Add specific logic to your function code according to your use case. For example, you could check contact attributes against an internal database of agent dialing permissions. For more information, see Using Attributes with a Lambda Function.

Your function must also include a call to the Amazon Connect StopContact API. In the API call, specify the "ContactId" and "InstanceId" of the contacts that you want to stop.

Additionally, you must attach an AWS Identity and Access Management (IAM) policy with sufficient permissions to call the StopContact API to the function's execution role. The following example JSON policy document includes the basic permissions to call the API:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "StopContactPermission",
            "Effect": "Allow",
            "Action": "connect:StopContact",
            "Resource": "*"
        }
    ]
}

Use the following Python function code as an example. Be sure to customize it for your use case.

Note: This example code includes the condition to identify which number to block. If that number is a requested callback number, the function handles the API error and passthrough.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import boto3
from botocore.exceptions import ClientError
connect = boto3.client('connect')

def lambda_handler(event, context):
    response = {
            'phoneNumberBlockerMessage': 'Passing through AmazonConnectPhoneNumberBlocker. No contact is blocked.'
    }

    try:
            if event['Details']['ContactData']['CustomerEndpoint']['Address'] == '<telephone-number-condition>':
                # Stop contact here

                connect.stop_contact(
                    ContactId = event['Details']['ContactData']['InitialContactId'],
                    InstanceId = event['Details']['ContactData']['InstanceARN'].split('/')[1]
                )
                response = {
                    'phoneNumberBlockerMessage': 'Call is blocked.'
                }
    except ClientError as e:
            if e.response['Error']['Code'] == 'ContactNotFoundException':
                response = {
                    'phoneNumberBlockerMessage': 'This is either a callback contact or contact ID does not exist, contact pass through.'
                }
            else:
                # Handle other error
                pass
    return response

Add the Lambda function to your instance

In the Amazon Connect console, add your Lambda function to your contact center instance.

Create an outbound whisper flow

If you haven't already, create an Outbound whisper contact flow.

Important: To create and edit a contact flow, you must log in to your Amazon Connect instance as a user that has sufficient permissions in their security profile.

  1. Log in to your Amazon Connect instance using your access URL (https://alias.awsapps.com/connect/login).
    Note: Replace alias with your instance's alias.
  2. In the left navigation pane, pause on Routing, and then choose Contact flows.
  3. On the Contact flows page, next to the Create contact flow button, choose the down arrow, and then choose Create outbound whisper flow.
  4. In the contact flow designer, for Enter a name, enter a name for the contact flow. (For example, Outbound whisper (Stop).)
  5. Choose Save.

For more information, see Create a New Contact Flow.

Add a Set contact attributes block

Configure this block to get the agent's user name, which you send to Lambda to parse.

  1. In the contact flow designer, expand Set.
  2. Drag and drop a Set contact attributes block onto the canvas.
  3. Choose the block title (Set contact attributes). The block's settings menu opens.
  4. Under Attribute to save, choose Use attribute, and then do the following:
    For Destination key, enter a key name. For example, agent_username.
    For Type, choose Agent.
    For Attribute, choose User name.
  5. Choose Save.

Add an Invoke AWS Lambda function block

  1. In the contact flow designer, expand Integrate.
  2. Drag and drop an Invoke AWS Lambda function block onto the canvas.
  3. Choose the block title (Invoke AWS Lambda function). The block's settings menu opens.
  4. Under Function ARN, choose Select a function, and then choose the Lambda function that you added to your instance.
  5. (Optional) For Timeout (max 8 seconds), enter a number of seconds that Amazon Connect waits to get a response from Lambda before timing out.
  6. Choose Save.

For more information, see Invoke the Lambda Function.

Finish the contact flow

  1. Add and connect more contact blocks as needed for your use case. For guidance, refer to the sample contact flows.
  2. Connect all the connectors in your contact flow to a block. Be sure to connect the Success node of the Set contact attributes block to the Invoke AWS Lambda function block. You must use at least these blocks:
    Entry point > Set contact attributes > Invoke AWS Lambda function > End flow / Resume
  3. Save and publish the contact flow.

For more information, see Create a New Contact Flow.

Configure a queue with the outbound whisper flow

In the routing profile to which your agents are assigned, identify the default outbound queue. For more information, see Create a Routing Profile and RoutingProfile Object.

Edit the queue by doing the following:

  1. In your Amazon Connect instance, in the left navigation pane, pause on Routing, and then choose Queues.
  2. On the Queues page, choose the name of the queue that you identified as the default outbound queue.
  3. On the Edit queue page, for Outbound whisper flow (optional), search for and choose the name of the outbound whisper flow that you created.
  4. Choose Save.