DEV Community

Cover image for Handling your business calls and texts like a... boss!
Marcos Placona for Twilio

Posted on • Originally published at twilio.com on

Handling your business calls and texts like a... boss!

I dread the idea of having ever to change my phone number. Be it because I'm getting spammed, or because someone thinks it's a good idea to call me during the night every night for the...rest...of...my...life. I'd much rather throw away all my business cards with a disposable phone number than to dispose of my real phone number which I had for 15 years now.

To make sure we never have to dispose of our real number, today we are going to look at how to provision new phone numbers that you can give out to people or put on your business cards without leaving the Twilio Console.

Our tools

  • A Twilio account - you can get one for free here
  • (optional) A picture frame - because you're going to want to put your picture up looking like a boss when you finish this.

Handling text messages

The first thing we need to do is go to the phone numbers page and buy a phone number.

The beauty of these numbers is that we can release them just as easily as we can get one, which ties well with the idea of never having to "release" the beloved phone numbers we've held for years.

I chose a UK mobile number and then headed to the setup page. In that page and under "Messaging", select "TwiML" and press the "+" button.

On the modal window that the "+" button just opened, enter a name for your TwiML bin. I called mine "Business Card". Add the following code to handle incoming SMS messages.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message to="YOUR_PHONE_NUMBER">
        {{From}}: {{Body}}
    </Message>
</Response>
Enter fullscreen mode Exit fullscreen mode

Replace the value YOUR_PHONE_NUMBER with your actual mobile number so any incoming messages to your Twilio phone number get redirected to you and then hit save. You're done with this setup and ready to handle phone calls.

Handling text messages

We could do the same thing we did above with phone calls and be done with it, but I like to treat people who call me directly a little better.

Head to Runtime and create a new Twilio Function. On the Management page click the "+", choose "Blank" and click "Create". Give that a name – I called mine "Personal Voicemail" – and set the path to be "/personal-voicemail".

We want to do a couple of things with this function:

  • Be able to answer calls to our Twilio number on our own number
  • Block certain numbers

Choose "Incoming Voice Calls" under "Event" and add the following code:

exports.handler = function (context, event, callback) {

        /***** configuration *****/

        const phoneNumber = 'YOUR_PHONE_NUMBER';
        const timeout = event.Timeout || 12;

        const secureRecordingLinks = false;

        const voiceOpts = {
                'voice': 'alice',
                'language': 'en-US'
        };

        const reject = [
                // To block a caller, add the E164 formatted number here
        ];

        let rejectMessage = "You are calling from a restricted number. Goodbye.";

        /***** end configuration *****/


        let thisFunction = 'https://' + context.DOMAIN_NAME + '/personal-voicemail';

        function shouldReject() {
                return reject.length && event.From && reject.includes(event.From);
        }

        function rejectInbound() {
                let twiml = new Twilio.twiml.VoiceResponse();
                if (rejectMessage) {
                        twiml.say(rejectMessage, voiceOpts);
                }
                twiml.hangup();
                return twiml;
        }

        function handleInbound() {
                const dialParams = {
                        'action': thisFunction
                };

                if (event.CallerId) {
                        dialParams.callerId = event.CallerId;
                }

                if (timeout) {
                        dialParams.timeout = timeout;
                }

                const twiml = new Twilio.twiml.VoiceResponse();
                twiml.dial(dialParams, phoneNumber);

                return twiml;
        }

        function redirect() {
                const twiml = new Twilio.twiml.VoiceResponse();
                twiml.redirect(thisFunction);
                return twiml;
        }

        switch (true) {
                case event.CallStatus === 'queued':
                        callback(null, redirect());
                        break;
                case event.CallStatus === 'ringing':
                        callback(null, shouldReject() ? rejectInbound() : handleInbound());
                        break;
                default:
                        callback();
        }
};
Enter fullscreen mode Exit fullscreen mode

The code above check that the number calling your Twilio number is not in the blacklist and in that case will forward the call to your real telephone number.

Make sure you replace the YOUR_PHONE_NUMBER value with your actual mobile number. If you have any numbers you want to add to the blacklist, add them comma-delimited like so:

const reject = [
                "+123456789","+15555555","+44123456789"
        ];
Enter fullscreen mode Exit fullscreen mode

Go back to the phone numbers page. Under "Voice & Fax" choose "Function" for "When a Call Comes in" and then select your new function and hit save.

When someone calls or texts your Twilio phone number, they should get redirected to your real phone number. You will then have the option to answer it or send straight to voicemail as you would normally.

Boss mode: Unlocked!

Now that our number is safe from spam or abuse, we can rest assured knowing that if ever anyone gets hold of our Twilio number and decides to abuse it, we can always release it and get another one using the code we've already written.

Furthermore, we can have several numbers using the same code so we can give them away at different occasions.

I would love to hear about smart ways in which you use Twilio to make your life easier. Hit me up on @marcos_placona or leave a comment below.

Top comments (0)