Background
Amazon Connect, as anyone who has used it can tell you, is an amazing piece of kit that you can quickly fire up and have a Contact Center running in a short period of time.
One particularly useful piece of functionality that we implement for the majority of our customers is the queued callback functionality, as it allows callers to maintain a queue position but not be tethered to their phone waiting to hear the notification (or speaking!) that an Agent has connected. This is all out-of-the-box with no real extra configuration required, and there is plenty of documentation around how to achieve this, so I don’t want to dwell on this.
Rather, this post is instead looking at the one issue that ends up arising from restrictions on the platform, namely that Amazon Connect requires the use of E.164 Format Numbers (the short version being these are the numbers that start with the leading + and includes country codes). If a user is simply wanting the callback to occur for the number they are on (assuming of course that they aren’t setup to appear as anonymous) then this is simple, we simple set their callback number to the number they are calling on, and jobs done.
But what about advanced situations? I’ve already mentioned the case of callers setting themselves up as anonymous, but what about other behaviours, such as wanting a callback on a different number (imagine a case where you call in on a landline but would prefer the callback on your mobile). In a lot of cases callers don’t know about E.164, or get confused when it is enforced. This also means that as a Contact Flow we would need to either enforce a standard (require the E.164 number, or local version, and then convert back and forth as desired) or add handling/branching for different scenarios.
That’s not to say this isn’t possible in a Contact Flow of course, however it starts to add up when you consider the permutations, so what if instead we use a Lambda function that we can simply vary with inputs and also validate that a number is in the expected format (which can help reduce bad numbers ending up in your system).
AWS Lambda to Validate and Transform
Connect can use AWS Lambda to achieve various tasks, and this is a perfect fit for this situation, and can also be extended as required for extra capabilities.
Rather then copy pasting code, lets link to Github and then discuss some of the specifics
As the code comments mention, this is a reasonably lightweight function that takes 2 inputs, phone_number and optionally country to process and validate a provided number.
phone_numbercan be in either local or E.164 format as appropriatecountryrepresenting the ISO code for the country we wish to validate the number against. If the number is provided in a local format this will default (in the code this goes to NZ as this is where it was built, but can be adjusted as required)
The code uses Googles libphonenumber library to validate and run checks for the provided inputs, and explicitly places the National and E.164 format numbers into the outputs to be used as desired back in Connect (with some tidying since National formats can include things like hyphens and brackets for readability).
Generally, I would recommend taking the customers input, parsing it to validate it, and then reading the phone number back if this is to be used. This can also fit in neatly after a question prompting the user if they want to be called back on the number they dialed in on (if its not anonymous) to reduce the number of questions, since in most cases people will want the call back on the same number.
If the response message returns with the field message as INVALID we should take it that something is malformed or mis-entered (some examples being too short, invalid characters such as *, or simply not conforming to the expected standard for a given country), and we can use the Contact Flow to re-prompt them, optionally by reading back what they entered.
An important note is this may not be perfect, there may be edge cases that fail to resolve or changes to number formats that require a repackage to ensure up-to-date functionality, however by doing this we get a bit of cleanliness in our environment