Problem statement
We have an issue where users who provide a leading zero on their phone number for MFA cause the message to fail to send. This is inconsistent with how the passwordless SMS connection appears to work, which removes leading zeros from the resulting user profile’s phone number (presumably following the E.164 format).
However, in my testing, setting up an SMS MFA for a user will store the full number provided, i.e., it can include leading zeroes, not E.164 format based on comparing the masks. While Twilio appears to handle this, for some SMS delivery providers, this is not handled and causes the message to fail to send.
Solution
We have a backlog item to address this, however, for the moment, there is an alternate way to handle leading zeros:
This solution adds code to the Phone Message Hook that uses the google-libphonenumber library to ensure the recipient is E.164 formatted.
Implementation Details
A phone message hook calling your phone message provider must be created if one does not already exist. The latest version of the google-libphonenumber npm module should be added to the hook via the Management API/DeployCLI or tools menu in the dashboard.
The below formatting code should then be added to your hook to E.164 format the recipient.
module.exports = function phoneMessageHook(recipient, text, context, cb) {
// Start formatting code
const PNF = require('google-libphonenumber').PhoneNumberFormat;
const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
// Load recipient into the phone number object.
let enteredNum = phoneUtil.parse(recipient);
// E.164 format recipient
let e164Num = phoneUtil.format(enteredNum, PNF.E164);
// End formatting code
// Not shown: Call Phone message provider as normal using the value of e164Num instead of recipient.
return cb(null, {});
};
Dependencies
The Phone Message Factor must be configured to use the Phone Message Hook delivery provider.
The google-libphonenumber npm module is used in the hook and must be added to the hook via its tool menu or the Management API.
Note that this sample should not be considered production-ready. Error-handling code should be added, and the solution tested with phone numbers in all expected formats. The google-libphonenumber should be updated regularly with any new releases of the module.