A CLI tool to quickly buy a Twilio phone number and forward calls/SMS to another number
CPNF
)A small CLI utility that buys a Twilio number and forwards incoming calls/SMS’ to your own number
To buy and configure phone numbers using CPNF
you need to have a free Twilio account and a UNIX environment.
⚠️ Running this script will create cost because it buys a US Twilio phone number. If you want to have more information have a look at the pricing page.
Twilio is a global cloud communications and customer engagement platform that has several APIs at it’s core. These APIs enable developers to build and automate flows that usually happen over phone, SMS, Whatsapp, Email, …
Make sure you have the following configuration values at hand (or stored in environment variables):
TWILIO_ACCOUNT_SID
)TWILIO_AUTH_TOKEN
)MY_PHONE_NUMBER
)CPNF
will ask for these values if they were not present in the environment variables.
npx run create-phone-number-forwarding
# or alternatively
npm i -g create-phone-number-forwarding
create-phone-number-forwarding
That’s it to buy and configure a phone number. And it should like the following: 👇
Whenever someone calls or sends a text message to a Twilio phone number, Twilio will make an HTTP request (a so called webhook) to a configurable URL. This URL should return TwiML (Twilio’s configuration file format).
To forward SMS and phone call what you need to publicly accessible URLs that return the following configuration
<Response>
<Dial>$YOUR_PHONE_NUMBER</Dial>
</Response>
<Response>
<Message to="$YOUR_PHONE_NUMBER">From: $SENDER. Message: $MESSAGE_BODY</Message>
</Response>
Luckily, Twilio offers also a serverless product. These let you deploy quick HTTP endpoints in a few minutes.
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.dial(context.MY_PHONE_NUMBER);
callback(null, twiml);
};
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message(`From: ${event.From}. Message: ${event.Body}`, {
to: context.MY_PHONE_NUMBER
});
callback(null, twiml);
};
CPNF
sits on top of twilio-run and the Twilio CLI. All the logic and functionality can be found in create-phone-number-forwarding.sh.
Phone calls and SMS to your new Twilio number will now be forwarded to your personal number.