k00k
k00k

Reputation: 17573

AWS SNS - Invalid Parameter - Phone Number is not valid to publish to. On specific phone number only

We are using AWS SNS to send SMS messages. In testing, it works for all but one of our devs who isn't receiving messages, the logs show the following:

Invalid parameter: PhoneNumber Reason: +1‬207XXXXXXX is not valid to publish to

I left his area code in case it's relevant. Again, this is the only number we've had issues with, it's an iPhone. It works fine for all the other numbers we've tried. I can also successfully SMS that number via the AWS SNS Console without issue.

I should note, we're only sending a 6 character string (for 2 factor auth).

We're doing this from a Lambda. Here's the relevant portion of the code:

export function sendSNS(PhoneNumber, Message) {
  return new Promise<boolean>((resolve, reject) => {
    const sns = new AWS.SNS({ region: 'us-east-1' })
    const params = {
      MessageStructure: 'String',
      Message,
      PhoneNumber
    }
    sns.setSMSAttributes({
      attributes: {
        DefaultSenderID: 'mycompany',
        DefaultSMSType: 'Transactional'
      }
    })

    sns.publish(params, function(err, data) {
      if (err) {
        console.log(err)
        reject(false)
      } else {
        console.log(`Sent this SMS via Amazon: ${Message} to ${PhoneNumber}`)
        console.log(data)
        resolve(true)
      }
    })
  })
}

Upvotes: 1

Views: 4005

Answers (2)

alexkb
alexkb

Reputation: 3356

Also be aware that not all AWS regions support sending SMS's and you'll see this same error "InvalidParameter: Invalid parameter: PhoneNumber Reason: +614##### is not valid to publish" when sending messages to a region that doesn't support it (in my case us-west-1).

For a list of regions that do support sending SMS's, see the sns amazon docs on supported regions.

Credit to user RichPeaua in this comment of the AWS forums.

Upvotes: 1

k00k
k00k

Reputation: 17573

I was able to fix this by updating the user's phone number directly in our MySQL DB by hand. Not sure if it was a character encoding issue or similar, but am assuming it must've been. I'll post back if I determine the exact cause.

UPDATE: This was definitely caused by an encoding issue, paste the code below into jsfiddle, and mouseover to see the warning on the first plus sign which reads:

This character may get silently deleted by one or more browsers

var x = '+1‬207XXXXXXX'
var y = '+1207XXXXXXX'

You can also try deleting/backspacing the + or 1 in the offending string (var x). Some weird results.

This data was initially entered into the MySQL DB via a GraphQL mutation from Prisma Playground using Chrome on Mac.

If I convert both strings above to hex to inspect, you can see they are indeed different:

2b31202c32303758585858585858 (bad)

2b3132303758585858585858 (good)

Upvotes: 2

Related Questions