Search code examples
amazon-web-servicessmsamazon-sns

AWS SNS SMS - Programmatically remove a phone number from the opt-out list (i.e. opt-in)


It seems impossible to use the AWS command line API (or Java SDK) to opt-in a phone number that has opted out of SMS (by sending a STOP message). Here's a command line example (with phone number redacted):

> aws sns check-if-phone-number-is-opted-out --phone-number "612XXXYYYY"
{ "isOptedOut": false }

So a country code is required. Adding that, I see the opt out with either a "+" or not:

> aws sns check-if-phone-number-is-opted-out --phone-number "+1612XXXYYYY"
{ "isOptedOut": true }

> aws sns check-if-phone-number-is-opted-out --phone-number "1612XXXYYYY"
{ "isOptedOut": true }

So then I try to opt that number back in:

> aws sns opt-in-phone-number --phone-number "+1612XXXYYYY"
An error occurred (InvalidParameter) when calling the OptInPhoneNumber operation: Invalid parameter: 

> aws sns opt-in-phone-number --phone-number "1612XXXYYYY"
An error occurred (InvalidParameter) when calling the OptInPhoneNumber operation: Invalid parameter: 

> aws sns opt-in-phone-number --phone-number "612XXXYYYY"

That last one without a country code doesn't return an error, but it also doesn't do anything:

> aws sns check-if-phone-number-is-opted-out --phone-number "1612XXXYYYY"
{ "isOptedOut": true }

For fun, here is the Java SDK version, which also doesn't work:

SnsClient snsClient = SnsClient.builder().region(Region.US_WEST_2).build();
OptInPhoneNumberResponse response = snsClient.optInPhoneNumber(b -> b.phoneNumber("1612XXXYYYY"));

Is there a way to programmatically remove a phone number from the SMS opt-out list? I can do it manually using the web console, but this doesn't help.


Solution

  • The above commands/code will work for reversing a STOP request, but only once. Apparently after sending the second STOP reply, AWS will no longer allow you to opt in to receiving SMS messages again with that phone number. So if you want to support more than one STOP->START sequence if you have to "Enable self-managed opt-outs" in Pinpoint and take care of handling it all yourself.