Skip to content

Instantly share code, notes, and snippets.

@rma92
Last active October 24, 2024 14:09
Show Gist options
  • Save rma92/83c65e7e383a6b4033b6bba01ac84db1 to your computer and use it in GitHub Desktop.
Save rma92/83c65e7e383a6b4033b6bba01ac84db1 to your computer and use it in GitHub Desktop.
Twilio: Set up international forwarding number

Twilio: Set up international forwarding number

I have multiple mobile numbers, and customers in various countries. I setup Twilio numbers in the various countries to allow customers to reach me using a local call, and change the forwarding number to whatever my local mobile number is.

Set up a service for call forwarding

The call forwarding service created here will be used by any number that needs to forward to my actual mobile number. As I travel, I change the forwarding number to whatever mobile number I'm using in that location.

  • On the left pane, go to Functions and Assets > Services. Click Create Service, enter a name.
    • If "Functions and Assets" is not visible, go to Explore Products, it's under Developer Tools.
  • Click Create Service, enter a name (I used "forward-call").
  • Create a function named forward call. Put this code in:
exports.handler = function (context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse();
  twiml.dial(context.MY_PHONE_NUMBER);
  callback(null, twiml);
};
  • Add an environment variable called MY_PHONE_NUMBER, and set that to the number you want to forward the calls TO.

Setting up a number to use the service

  • Go to Phone Numbers > Manage > Active Numbers. Click the phone number. Go to the configure tab. Set "A call comes in" to "Function". For "Service", choose the service you created ("forward-call"), pick the environment ("forward-call"), and the function path ("/forward-call").

Change the forwarding number

  • On the left pane, go to Phone Numbers > Manage > Active Numbers.
  • Click the /forward-call function for a number that has the function installed.
  • Go to the Env.variables tab at the top.
  • Set the MY_PHONE_NUMBER variable to the desired destination number.

TODO: Future expansion is to forward to multiple outgoing numbers, have a voice mail system, and add a way to make outgoing calls.

Forwarding Google Voice calls internationally

Google Voice can forward an incoming call to any number in the country where the Google Voice number is located (e.g. a US Google Voice number can only forward to a US number). One can mitigate this issue by setting up a Twilio number in the country with the GV account and using this forwarder.

Creating an endpoint that calls multiple numbers:

This works, but if the first line goes to voicemail, the other phones won't be called.

exports.handler = function(context, event, callback) {
	let twiml = new Twilio.twiml.VoiceResponse();
  twiml.say('Please wait while you are connected.');
  
  const dial = twiml.dial();
  const phoneNumbers = [context.MY_PHONE_NUMBER, context.MY_PHONE_NUMBER_2];
  
  phoneNumbers.forEach(number => {
    dial.number(number);
  });

  return callback(null, twiml);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment