12

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.

Eric Baril
  • 305
  • 4
  • 14
  • 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 Aug 12 '14 at 00:48
  • 1
    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 Aug 12 '14 at 01:52
  • 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 May 04 '17 at 04:39

2 Answers2

16

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!

Reed G. Law
  • 3,897
  • 1
  • 41
  • 78
Megan Speir
  • 3,745
  • 1
  • 15
  • 25
  • 3
    My solution was extremely similar to this but instead of matching against the error code '21610' I matched the error message. For example: `e.message =~ /'is not a valid phone number'/`. Your error code is less fragile and I will be updating my method. Thank you. – Eric Baril Oct 28 '15 at 05:29
  • @EricBaril that's not the message that is returned by 21610. Message is currently "The message from/to pair violates a balcklist rule." You should really check the error code of 21610, as Twilio could always update the API error message again. – scottheckel Apr 15 '19 at 19:55
1

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.

Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70