Block Spam Calls and Robocalls with Ruby
Time to read: 3 minutes
Spam, scams, and robocalls are at best annoying. For high-volume customer service centers, they can significantly impact the bottom line. Let’s leverage the Twilio Marketplace and our ruby skills to block spam callers, robocallers, and scammers.
Before getting started, you should already know how to handle incoming calls with Twilio.
Get a Spam Filtering Add-on
You can integrate third-party technologies without leaving the comfort of the Twilio API. You can access the Add-ons from your Twilio Console. Today, we’re going to look at two Voice Add-ons that can help us with this spam problem: Marchex Clean Call, and Nomorobo Spam Score.
Installing the Add-on
Once you’ve decided on the Add-on you’d like to use, click the Install button and agree to the terms. In our use case today, we want to make use of these Add-ons while handling incoming voice calls, so make sure the Incoming Voice Call box for Use In is checked and click Save to save any changes:
Note the “Unique Name” setting. You need to use this in the code that you will write to read the Add-on’s results. In the code for this guide, we are sticking with the default names.
Check Phone Number Score in Ruby
When Twilio receives a phone call from your phone number, it will send details of the call to your webhook (more on how to configure that later). In your webhook code, you create a TwiML response to tell Twilio how to handle the call.
For spam-checking, our code needs to check the spam score of the number and deal with the call differently depending on whether the Add-on considers the caller to be a spammer or not. In our example code here, we’ll return a <Reject> TwiML tag to send spammers packing and a <Say> TwiML tag to welcome legit callers.
The code is a simple Sinatra application:
How to Check Marchex Clean Call
Here’s an example of what Marchex Clean Call will return:
This function uses only information found in the key results.marchex_cleancall
. Here we advise blocking the call if Marchex' recommendation is set to BLOCK
.
How to Check Nomorobo Spam Score
Here’s an example of what Nomorobo Spam Score will return:
This function uses only information found in the key results.nomorobo_spamscore
. Here we advise blocking the call if Nomorobo Spam Score is 1.
Now we've seen how to get advice from each Add-on but we have different options for handling the call. Let's look at how we're making the final decision.
Making a decision to block a call
In our example, unanimity is required for accepting a call. So for each Add-on, we look at their advice, and if even one of them tells us to block it, we'll reject the call.
We could have also implemented it differently by using a majority rule. In any case, once we make a decision, all that is left to do is act.
Call Handling Options
Rejection Options
Using <Reject> is the simplest way to turn away spammers. However, you may want to handle them differently. The whole universe of TwiML is open to you. For example, you might want to record the call, have the recording transcribed using another Add-on, and log the transcription somewhere for someone to review.
Call Accept Options
For this example, we’re just greeting the caller. In a real-world scenario, you would likely want to connect the call using <Dial> (to call another number or Twilio Client), <Enqueue> the call to be handled by TaskRouter, or build an IVR using <Gather>.
Configuring a Phone Number Webhook
Now we need to configure our Twilio phone number to call our application whenever a call comes in. So we just need a public host for our application. You can serve it any way you like as long as it's publicly accessible or you can use ngrok to test locally.
Armed with the URL to the application, open the Twilio Console and find the phone number you want to use (or buy a new number). On the configuration page for the number, scroll down to "Voice" and next to "A CALL COMES IN," select "Webhook" and paste in the function URL. (Be sure "HTTP POST" is selected, as well.)
Everything is set up now, you can pick up your phone and call your Twilio number. Hopefully, if you are not a spammer your call should be accepted and you should hear the greeting.
Testing a Blocked Call
You can quickly call your Twilio number to make sure your call goes through. However, how can we test a blocked spam result? The easiest way is to write some unit tests that pass some dummied up JSON to our controller action. For example, if we wanted to test a Nomorobo “BLOCK” recommendation, we could use the following JSON:
What’s Next?
As you can see, the Twilio Add-ons Marketplace gives you a lot of options for extending your Twilio apps. Next, you might want to dig into the Add-ons reference or perhaps glean some pearls from our other Ruby tutorials. Wherever you’re headed next, you can confidently put spammers in your rearview mirror.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.