A 2nd Number with Twilio & NodeJS (Part I)

Jimmy Guo
5 min readMar 12, 2019

--

We all go through it sometimes, whether it’d be giving your phone number to a real estate agent, or that person at the bar that you have no plans talking to in the future.

Sometimes, you’d just wish at that moment in time, you had a second phone number that you can give out, that you can control and monitor.

Well let me tell you about this great service called Twilio (Warning: Some coding will be necessary). For under $10 a month (depending on your usage rates of course), you can have a second phone number! Without needing to purchase another device!

This article will teach you the basics of utilizing Twilio and NodeJS to allow you to have a second number, at a cheap cost. Now you could also add a client facing app or webapp to make things a bit better looking, but for the purpose of this tutorial, functionality will be programmed via Programmable SMS.

It’s worth mentioning that Twilio has it’s own Markup language, TWIML. Although functionally, TWIML is very limited but there are benefits to it as it is quick to implement (pretty much just like XML) and you can run it instantly on the Twilio console in response to events. I use it when I need to sign up for an account on some app, and do not want to expose my personal number. Three lines of TWIML will forward access codes to my phone number.

Twilio also has Serverless Function capabilities, but I’ll cover that in part two of this tutorial series. For part I of this tutorial series, we will sacrifice some convenience for some quick code and exposure to the powers of Twilio.

Tools you will need for this exercise:

Clone the boilerplate and enter your Twilio AccountID and AuthToken into .env.example file on the root directory, and rename that file to .env

Great! Now install the dependencies via npm install and run a local version of the server (set at port 9999) via npm run dev. Run a ngrok instance on that port number when you’re running in dev by ngrok http 9999

Let’s get a number set up now on Twilio. Purchase any number of your choice with SMS and voice capabilities for $1 a month. Once you’ve purchased this number, set it up following the image below with your new ngrok address.

Remember to replace the ngrok address with that of your own ngrok instance!

Let’s Dive into the Code!

The main files we’re going to look at are router.js and routes/twilio_routes.js

router.js will contain the routes available in the microservice. So far we have receive_sms and receive_call

In routes/twilio_routes.js, make sure you’ve assigned your account_sid and auth_token while importing twilio. Also make sure to replace the global variable my_number with your actual phone number in E.164 format (eg. +14155556373)

To send a message via Twilio, it’s as easy as twilio.messages.create({ to: ‘number’, from: ‘number’ , body: ‘this is a message’ })

! IMPORTANT ! Make sure you respond to the Twilio server in the callback after you’ve created the message so Twilio’s bug reports won’t act up.

To create a phone call with twilio, it’s as easy as using twilio’s voiceResponse and dial/call features. I prefer to use dial if you’d want to record calls and add analytics to the phone call, and/or to apply some natural language. There are plenty of cool add-ons in the Twilio AddOns Catalog.

Well these are the basics of using Twilio and NodeJS. The boilerplate contains additional code for a simple method for utilizing these basics to create a second phone number. What gets tricky is managing one phone number across many people. That’s why in this boilerplate, a response message from your phone number to the incoming message’s phone number requires you to type in your response message in a specific format

Response Message Format: [type number] [space] [type message]

And an outgoing phone call requires you to dial your Twilio phone number and type in the outgoing number.

These are not the most optimal in terms of convenience, but for the scope of this tutorial, we will be sacrificing convenience for ease of code and quick functionality.

We can improve convenience by maintaining the states of the phone calls by leveraging a database, and/or a simple client facing app/webapp.

This is a simple web dialer I’ve built because the signal from my cellular provider is pretty terrible in the outskirts of town. I’ve used this web dialer to make many, many sales call and it’s still stable to this day.

The other caveat is utilizing ngrok. I’ve chose ngrok for the ease of use and so I did not have to run this microservice on a server. Your ngrok address will expire after eight hours (unless you pay for the pro plan), and you’d have to consistently replace your ngrok address on the Twilio console for this to consistently work.

For production, run this microservice on a server. However, there is something even better that will be released in part II. Twilio currently is in BETA for their serverless functions. That means no maintaining a ngrok, no maintaining a server. Write all your code on Twilio, and let your code run independently on Twilio.

That being said, this is a quick tutorial to demonstrate what you can do with Twilio and how easy it is to use with NodeJS. Stay tuned for my next article on creating a second phone number with Twilio Serverless Functions!

Till next time,

Zeedann

--

--