Robocall-Resistant Voicemail with Add-ons and Functions

Developer enhancing voicemail feature with Twilio add-ons on his laptop
September 01, 2022
Written by
Alexander Chen
Contributor
Opinions expressed by Twilio contributors are their own
Reviewed by
Paul Kamp
Twilion
Wade Catron
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Add-ons are designed to enhance existing messaging and voice systems and APIs.

This tutorial covers how to use the Trestle Reverse Phone Add On to prevent robocallers from leaving voicemails. This tutorial uses Twilio Functions, a serverless, Node.js platform for deploying code. 

Install and configure the Trestle Reverse Phone Add-on

This section covers how to install and enable the Trestle Reverse Phone Add-on, which provides you with caller information. 

  1. In the Console, navigate to the Add-ons catalog in either of the following ways:
    • In the search bar at the top of the page, search for Add-ons Catalog.
    • In the left navigation pane, click on Explore Products. Click on the Marketplace heading and click on Add-ons. You can also click the pin icon to pin the Add-ons page to your Console navigation. 

Find Add-ons in Console by clicking on Explore Products > Marketplace > Add-ons. Click the pin icon to pin to your Console navigation.


  • On the Add-ons page, click on Trestle Reverse Phone

Trestle Reverse Phone icon in Console Marketplace


  • On the Trestle Reverse Phone page in the Console, click on Install

Click the install button on the Trestle Reverse Phone page in the Console


Install Trestle Reverse Phone modal from Console showing the "I agree to Trestle's Terms of Service" checkbox selected and the Agree & Install button


  • You now see a Configure tab on the Trestle Reverse Phone Console page. Find the USE IN field and select the Incoming Voice Call checkbox. 

Trestle Reverse Phone Add On - "Use In" field has "Incoming Voice Calls" selected


  • Click Save
  • With this configuration, Twilio sends the Trestle Reverse Phone data in all incoming call webhooks. The next section covers how to receive and use the data from Trestle Reverse Phone in order to reject voicemails from invalid phone numbers. 

    Create a Twilio Function to handle incoming calls

    This section covers how to create a Twilio Function that parses the data from Trestle Reverse Phone and dynamically creates TwiML instructions based on that data. 

    Callers with valid phone numbers are prompted to leave a voicemail with <Say> and <Record> TwiML, while callers with invalid phone numbers are rejected with <Reject>.

    1. In the Twilio Console, in the left navigation pane, click on Explore Products.  
    2. Under Developer Tools, click on Functions and Assets.
    3. On the Functions Overview page, click the Create Service button.
    4. In the Service Name field, enter test-voicemail and click Next
    5. Click the Add + button and click Add Function
    6. Change /path_1 to /voicemail and hit the enter key. 
      • Once you buy and configure a Twilio Phone Number in the next section, this is where Twilio sends incoming call webhooks that contain the data from Trestle Reverse Phone. 
    7. Next to /voicemail, click the lock icon and select Protected
Twilio Functions - Change Function Path visibility to "Protected" to allow Twilio to send incoming call webhooks

  • This allows Twilio to send incoming call webhooks to your /voicemail endpoint. 
  • You now see sample code. Delete that code and replace it with the code below: 
    // This code sample serves to filter robocalls prior to taking a voicemail.
    
    // Twilio Functions automatically executes the exports.handler method in any
    // code you write. It comes with inputs for a context, event, and callback.
    // Full documentation: https://www.twilio.com/docs/runtime/functions/invocation
    
    exports.handler = (context, event, callback) => {
        // Use the Twilio Node helper library to generate valid TwiML in JavaScript.
        const twiml = new Twilio.twiml.VoiceResponse();
    
        // Save the AddOns data that Twilio provides in the incoming call webhook
        const addOns = event.AddOns;
    
        // Save the data from Trestle Reverse Phone   
        const trestleData = addOns.results.trestle_reverse_phone;
    
        let callIsValid = false;
    
        // Check if both AddOns and Trestle Reverse Phone were successful
        if (addOns.status == 'successful' &&  trestleData.status == 'successful') {
    
            // Check whether the caller's phone number is valid
            // Learn more on the Documentation tab of the Trestle Reverse Phone page in the Console
            callIsValid = trestleData.result.is_valid;        
        };
    
        if (callIsValid) {
    
          // Prompt the caller to leave a message. 
          twiml.say('Please leave a message after the beep. Please hang up when you are finished.');
      
          // Use <Record> to record the voicemail.
          twiml.record();
    
        } else {
    
            // Reject the call with <Reject> if the caller's phone number is invalid
            voiceResponse.reject();
        }
    
        return callback(null, twiml);
      }
      ​
  • Click Deploy All. Wait until the Function has been deployed before continuing to the next section. 

You must click Deploy All after any changes to Twilio Functions. Don't forget this step!

Now you have a deployed endpoint that returns TwiML instructions to Twilio that either records a voicemail or rejects the call.

In the next section, you will buy and configure a phone number so that Twilio sends incoming call webhooks (and the data from Trestle Reverse Phone) to your Twilio Function. 

Follow the steps below to purchase a Twilio Phone Number with Voice capabilities. 

  1. Log in to your Twilio Console.
  2. Navigate to the Buy a Number Console page: Search for "Buy a number" in the search bar at the top of your Console, or in the navigation sidebar, click on Phone Numbers > Manage > Buy a number
  3. Under Capabilities, make sure that the Voice checkbox is checked. 
  4. Under Search criteria, enter any criteria you want in your phone number and click the Search button. 
  5. Choose a number from the list and click its associated Buy button. 

Console Screenshot - Buy a Phone Number page
  • On the Review Phone Number pop-up, click the Buy (XXX) XXX-XXXX button to confirm your purchase.
Console Screenshot - Review Phone Number Modal

Configure your phone number

  1. On the Phone Number's Console page, scroll down to the Voice Configuration section.
  2. Under A call comes in, select Function from the dropdown. 
  3. Under Service, select test-voicemail.
  4. Under Environment, select ui.
  5. Under Function Path, select /voicemail.
Trestle Reverse Phone - Phone Number Configuration Page - Select the Twilio Function, Environment, and Path for "A Call Comes In"

  • Don't see /voicemail? Make sure you completed step 7 from the "Create a Twilio Function to handle incoming calls" section above. Don't forget to click Deploy All
  • Click Save.
  •  

    Try it out

    Call your Twilio Phone number! Since you're not a robocaller, you are able to leave a voicemail. 

    You can view the data from Trestle Reverse Phone in your Call Logs in the Console, as well as listen to the recording. 

    1. In Console, in the left navigation pane, click Monitor > Logs > Calls
    2. Click on the most recent Call. 
    3. Under Media, you can download or listen to the recording. 
    4. Under Request Inspector, click Expand All (on the right side of the page). 
    5. Under the first POST request's Parameters, find the AddOns parameter. You can see all of the information provided by Trestle Reverse Phone (along with any other Add-ons you may have installed). 

    Next Steps

    Find out more about the data provided by Trestle Reverse Phone in the Console. Navigate to the Trestle Reverse Phone page and click on the Documentation tab. 

    Learn more about recordings with Twilio on the Recordings Reference page