This snippet helps make a routing decision whether to add a new contact to the queue or take another action (like terminating the call) based on the queue length and the business hours for that day.
The solution includes 2 AWS Lambda functions, and an Amazon DynamoDB table:
-
UpdateQueueHoursLambda function:- Triggered by AWS EventBridge scheduled rule
UpdateRateparameter defines Hours of Operation refresh rate, between DynamoDB and Amazon Connect Instance- Retrieves all Queues from an Amazon Connect instance
- For each queue, retrieves the queue Hours of Operation
- Stores Queue and Hours of Operation configuration in a DynamoDB table
-
GetTimeLeftLambda function:- Triggered by Amazon Connect Contact Flow
- Receives contact's
Queue ARNandOldest contact in queue - Retrieves Queue and Hours of Operation configuration from the DynamoDB table
- Checks the Hours of Operation and determines
HoursOfOperationStatusas:QUEUE_NOT_FOUND- in case the Queue was not found in the DynamoDB tableOUT_OF_HOURS- in case:current daywas not found in the Hours of Operation, orcurrent timewas outside ofStartandEndtime window for thecurrent day
OPEN_ALL_HOURS- in caseStartandEndtime were equal for thecurrent dayIN_HOURS- in casecurrent timewas betweenStartandEndtime window for thecurrent day
- Only if
HoursOfOperationStatuswasIN_HOURS, the function adds the following to the response:TimeLeftMinutes= time difference betweencurrent timeandEndtimeTimeDeltaMinutes= time difference betweenTimeLeftMinutesandOldest contact in queue
Based on GetTimeLeft function response, you can design your Amazon Connect Contact Flow to either add the contact to the queue, if there was enough time left before the queue closes, or take another action.
To invoke GetTimeLeft function from your Amazon Connect Contact Flow:
- Place
Set working queueblock and set your Queue (i.e. BasicQueue) - Place
Get queue metricsblock - Place
Invoke AWS Lambda functionand selectGetTimeLeftLambda function - In Lambda function input parameters add:
- Select
Use attribute - Destination key:
OldestContactTimeSeconds - Type:
Queue metrics - Attribute:
Oldest contact in queue
- Select
- Place
Check contact attributesand select Type:External, Attribute:HoursOfOperationStatus, then add conditions:QUEUE_NOT_FOUND,OUT_OF_HOURS,OPEN_ALL_HOURS,IN_HOURS, and complete the contact treatment based on the result. - For
IN_HOURSresult, you can add anotherCheck contact attributesblock to checkExternal->TimeLeftMinutesandExternal->TimeDeltaMinutesresponse parameters, then complete the contact treatment based on the result.
An AWS Serverless Application Model (SAM) defines the application's AWS resources in template.yaml file.
The application includes AWS resources such as AWS Lambda functions, Amazon DynamoDB and Amazon CloudWatchEvents. You can update the template to add additional AWS resources, through the same deployment process that updates your application code.
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing serverless applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- Python 3 installed
- Docker (for local testing only) - Install Docker community edition
To build and deploy your application for the first time, run the following in your shell:
sam build
sam deploy --guidedIf you no longer need the AWS resources that you created by running this script, you can remove them by deleting the AWS CloudFormation stack that you deployed.
aws cloudformation delete-stack --stack-name <stack_name> --region <region>