11
votes

I am currently integrating into the twilio rest api and need to perform a check on a users phone number to determine if that user has blacklisted themselves or not. I have little experience with this api and scouring through the documentation and google has turned up nothing.

In our application we are going to have a notification center and if the user has blacklisted themselves I do not want to give them the ability to turn on their SMS notifications. Potentially a user could have SMS notifications on but twilio would block any messages. I know there is the ability to get a status code back from twilio when an SMS is queued that shows the user is blacklisted (https://www.twilio.com/docs/api/rest/message). However, I will not be sending messages on the notifications screen and need a direct way (if at all possible) to check twilio to determine if a number is blacklisted. Any help is much appreciated. Let me know if anymore information will be of help.

2
Will you ever be sending them messages? If so, create a flag on their account if you get returned the blacklisted error from the Message endpoint. There does not appear to be any other way to see if the user has blacklisted themselves.Mark Silverberg
Appreciate the quick response. Yea this sounds like a fine idea. Defiantly a nice work around since there seems to be no way to directly check.Eric Baril
I believe any user that replies "STOP" or similar will A) be "blacklisted" by Twilio for the phone number or message service, and B) have the stop message forwarded into your app, where you could detect the blacklisting and update something locally. (As far as an API to see banned numbers, I don't know of any such thing.)Scott Swezey

2 Answers

15
votes

Megan from Twilio.

I'd be curious to see if you ever tried your own workaround. But I wanted to note for others in a similar situation how you could grab the blacklist error and then do whatever you may want with it.

In Ruby it would look something like this:

require 'rubygems'
require 'twilio-ruby'

account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'

@client = Twilio::REST::Client.new account_sid, auth_token

begin
  @message = @client.messages.create(
    from: 'TWILIO_NUMBER',
    to: 'USER_NUMBER',
    body: 'Howdy!'
  )
rescue Twilio::REST::RestError => e
  if e.code == 21610
    # User is blacklisted
    # Store info however you choose
    puts e.message
  end
end

We check for blacklisting specifically using the code '21610'. For more information about errors you can visit the reference page.

Hope this helps!

1
votes

Twilio recommends developers to store the opt-out/in statuses in their side. I have stored it in DB. There are 2 ways to collect the unsubscribed users list.

1) Use SMS webhooks. You can find how to configure your Twilio number to receive webhook events here

@PostMapping(value = "/twilio", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public String twilioConsumer(TwilioEventDTO twilioEventDTO) {
// twilioEventDTO.getBody() => returns the body of the SMS user replied.
        twilioService.consume(twilioEventDTO);
        return new MessagingResponse.Builder().build().toXml();
    }

2) Since I implemented webhooks later, I had to collect already unsubscribed users. When you send sms to the number that has been opted-out, Twilio API throws an exception with the status number of 21610. You can catch it and store the number in DB.

try {
            Message result = Message.creator(
                    new PhoneNumber(toPhoneNumber),
                    new PhoneNumber(fromPhoneNumber),
                    messageBody)
                    .create();

            response = result.getStatus().name();
        } catch (ApiException e) {
            if (e.getCode().equals(21610))
                updateSubscription(toPhoneNumber, false);
            logger.warn("Error on sending SMS: {}", e.getMessage());
        }

P.S.: examples written in Java - Spring Boot framework.