Developer Resources

Using Twilio to Screen Nuisance Callers

When we set up a company phone number for Doorkeeper, we chose to get a number through Twilio. Being able to programatically manage phone calls appealed to me, as I thought it would allow us to start with a simple phone system, and expand it as needed. Initially, we were simply forwarding calls, but I've recently expanded it to block certain phone numbers.

A common scam targetting business owners

The reason I wanted to block incoming calls was because we've been targeted by a scam that seems pretty common. The way it works is you receive a call saying you have a business report waiting for you, and some additional details about your business is needed to send it. I've heard that if you give the caller the details, they'll then use them to send you an invoice for the report, and hope it gets through to your billing department without you noticing. We've never got that far though, as I tell them we're not interested in the report and hang up. Unfortunately, the scammers won't take no for an answer, and kept calling us continuously.

Eventually I got fed up with telling the same scammer to go away, and even if I didn't answer the phone, I still received a phone call every 20 minutes or so. I noticed that the phone calls were always coming from the same two numbers +811045000 and +266696687. I suppose the scammer was trying to make it appear as though the call was originating domestically with the +81 number (Japan's country code), but it would translate to 010 45000, and given that 010 is Japan's international call prefix, it's obviously a fake number. Similarly, I learned 266696687 will spell out ANONYMOUS on a telephone keypad, and so it is used by people who have Caller ID blocked, and sometimes people calling out of Skype.

So I decided to block calls from those numbers. Theoretically, a legitimate caller could be calling from 266696687, however given the low volume of calls we receive to our phone number (we don't provide phone support), I was willing to take the risk of it.

Twilio to the rescue

Previously, we were using Twimlets to handle the forwarding of phone calls. Since they don't have any twimlet for blocking (and I wanted to try developing for Twilio myself), I whipped up a quick Sinatra application that uses the Twilio Ruby Client.

banned_numbers = %w[+266696687 +811045000]

get "/xdf2kl5n9vvxl4nknk34n" do do |r|
    if banned_numbers.include?(params['From'])
      r.Dial timeout: 20 do
        r.Number ENV["FORWARD_TO"]

Initially I had used the Hangup verb instead of the Reject verb. Though by hanging up immediately, I didn't get bothered by the nuisance caller, Twilio still picks up the call and then hangs it up, so we got charged $0.01 each time (or about $0.20 per day until I realized it). By switching to Reject, the call is never answered and thus we aren't charged anymore.