If you have been following along and copying the code into your server.py file, you can now save it. Run this code by typing python server.py into your terminal ( python3 server.py if you are using python3). Once the code is running your server will be live at the forwarding URL ngrok displayed in your other terminal window.

Copy your server’s URL, and navigate to the dashboard page for your Twilio phone number (you can find this by navigating to the phone numbers overview and then selecting your Twilio number). You should see a form that looks like the one below. We need to paste the Ngrok URL with /sms appended to the end into “Messaging” > “A Message Comes In” as a webhook. While you’re at it, you can work ahead and fill in “Voice & Fax” > “A Call Comes In” with the URL and /call .



Twilio will now know to send an HTTP POST request to your webserver when your Twilio number gets a message which will hit the endpoint we created above. You’re now ready to text attendees through your proxy and receive replies. Try it by asking a friend to send a text message to the Twilio number! Alternatively, acquire another Twilio number and send a text message using this number.

When your friend or your friendly second Twilio number sends a text to your Twilio proxy number, your normal number should receive an encoded message. Reply in the encoded format and see whether your friend gets your reply as well. If you sent the number with another Twilio number, you can see received messages in the console by navigating to “Messages Log” for your second Twilio number.

Call and Receive Calls Using Your Twilio Number

Now that texting works, we’ve almost got a fully functional proxy number! The only thing left is to set up calling.

Forward calls with TwiML

Let’s start with forwarding calls made to our Twilio number. When a call is received by our Twilio number, Twilio will make a request to our server’s /call endpoint (as we configured above). We have to create a response that instructs Twilio to forward the call to our private number when we receive that request.

TwiML is exactly such a response. TwiML is an XML encoded set of instructions that tell Twilio what to do when an incoming call (or SMS) is received.

In our case we want to redirect a received call to our personal number. The snippet below does this using the TwiML verb . We pass two arguments: the number to which we want to forward the call and caller_id . Passing caller_id as None will allow us to see the original caller on our phone. When we are making a call, we will pass the TWILIO_NUMBER as the caller_id . Thus we can program it so that when an attendee is calling you can see their number, and when you call an attendee, they see your TWILIO_NUMBER .