How To Phone Number Masking - Sms Proxy Flowroute
- separate piece of work and individual numbers
- international numbers while traveling (without buying a local sim)
- an event hotline for Lawmaking of Conduct violations
- an alias number for a Tinder date, which yous can delete if things go badly...
- a Twilio account – sign upwardly for gratis
- a Twilio phone number with calling and texting capability
- Python 3.ten
- the micro spider web framework Flask
- the Twilio Python helper library
- ngrok - sign up for costless
- texts from an event attendee to our event hotline will reach our normal number
- we can reply to them
A Phone Number Proxy 📱↔️👻↔️📱
What would y'all do if y'all could take a defended telephone number for anything you wanted? This blog post will show you how to create a phone number that hides your private phone number and acts equally an intermediary betwixt your phone number and other phone numbers. The use cases are many:
Let's assume we demand a number for an outcome Code of Conduct hotline considering nosotros would prefer not to give attendees our individual number. In this post, we volition build just such a phone number proxy using:
Before we start, please ensure you have Python three and ngrok installed. To be able to use ngrok to build your dog or security cam, you have to likewise sign up for a free ngrok account. We will fix everything else as we become.
Surroundings and project setup
Install & configure virtualenv
We are going to use Virtualenv to gear up up a virtual environment for this project, in gild to isolate the dependencies of this project from your other projects. Please create a file in your projection directory named requirements.txt, with the following equally its contents:
These are the dependencies nosotros would like to install in our virtual surroundings. Side by side nosotros volition install virtualenv to create and actuate your virtual environment. Once this is done nosotros will install the dependencies from the dependencies file you created above into your virtual environment. Run the following commands in your command-line:
# installs virtualenv (use pip3 if you are using python3) python3 -yard pip install --user virtualenv # sets upwardly the environment python3 -grand venv env # activates the environment source env/bin/activate # installs our dependencies pip3 install -r requirements.txt Run ngrok
In a different terminal window, let's start ngrok. Ngrok will let you lot to betrayal your localhost at port 5000 to incoming requests. We will be using this to allow Twilio to communicate with our local python server. Information technology is important that y'all exercise not shut this last window, so delight have two last windows open – ane for python and the other for ngrok.
One time you type in the above your last window should look similar to this:
Acquire a Twilio Number
Adjacent, apply the Twilio Console to learn a Twilio number with calling and texting capability. We'll use this as our event hotline.
Pull it all together
At present that nosotros take our Twilio phone number, let's set a config file with all the of import details so that we can keep them separately managed. Create a file called config.py with the post-obit as its contents, replacing the placeholder phone numbers with your values:
TWILIO_NUMBER = "+447123456789" PRIVATE_NUMBER = "+447987654321" Adjacent, create a file called server.py. We'll be building this file up as we proceed. (If you adopt to copy the unabridged file as i, y'all can find the complete file on GitHub.)
Let's finish our setup by importing the libraries nosotros'll need and our configuration variables – delight add these at the summit of your file:
import re from flask import Flask, asking from twilio.twiml.voice_response import Gather, VoiceResponse from twilio.twiml.messaging_response import Bulletin, MessagingResponse from config import PRIVATE_NUMBER, TWILIO_NUMBER Receive and ship texts using your Twilio number
With everything set up, we want to make sure that
After all, nosotros do desire to talk to our attendee, despite non wanting to requite them our private phone number. Did I say attendee? I meant attendees! Nosotros evidently desire to talk to more than one of them. However, all letters routed through our proxy will show upward on our telephone as beingness from the proxy number. To not go confused about which attendee sent united states what, we will have to come up with a way to know who each text is from.
Encode & decode messages from multiple senders
To know who sent a certain text, we will use a protocol-like formatting that allows the states to see both, so Hello World! becomes +447987654321: Hello Earth!.
Add the following part to the end of your server.py file:
def encode_message(msg, number): render "{}: {}".format(number, msg) We will use the in a higher place function to get an encoded message when someone letters us. When we ship a message from our telephone, we will accept to ship it formatted the same mode, and our server volition decode it before information technology gets sent to the bodily recipient. Add this decoding role to server.py:
def decode_message(msg): pattern = re.compile('^\+\d*') number = pattern.friction match(msg).grouping() body = msg[len(number) + ii:] return body, number Integrate with Twilio Programmable SMS
Now nosotros can employ the above helper functions to integrate with Twilio. The code below will create the Flask app and gear up the first endpoint. The Flask app is initialized in the outset line and started in the terminal line with app.run(). In betwixt, we add together the /sms endpoint.
Afterwards in this section, we will configure our Twilio number to send a request to the /sms endpoint upon receiving a text message. This request contains the message trunk and the number of the sender. In our endpoint code, we store both in variables. We utilise the sender number to determine whether the sender number is ours or an attendee's and execute the respective clause in the if-else-statement.
If we are the sender, nosotros await the bulletin body to be of the format +447987654321: Hullo Globe!. In this case nosotros decode the message torso so telephone call the send_message. If the message comes from an attendee, information technology should be of the format Hullo Earth!. We encode the bulletin and ship it to our number using the send_message function.
The send_message function takes equally input a message and the designated recipient's number. Afterward creating a MessagingResponse object, nosotros create the response with the given message and number by calling the bulletin method on the object. Afterwards, we return the response object to terminate the request.
Add together the following lines to the bottom of your server.py file:
app = Flask(__name__) @app.route('/sms', methods=['Mail service']) def sms(): from_number = request.form['From'] msg_body = request.form['Body'] if from_number == PRIVATE_NUMBER: # if I am sending the sms msg, to_number = decode_message(msg_body) return send_message(msg, to_number) else: # if an attendee is sending the sms msg = encode_message(msg_body, from_number) render send_message(msg, PRIVATE_NUMBER) def send_message(msg, number): response = MessagingResponse() response.message(msg, to=number, from_=TWILIO_NUMBER) return str(response) if __name__ == '__main__': app.run() If you have been post-obit along and copying the lawmaking into your server.py file, you can now save it. Run this code by typing python server.py into your last (python3 server.py if you lot are using python3). One time the lawmaking is running your server volition exist live at the forwarding URL ngrok displayed in your other terminal window.
Copy your server'due south URL, and navigate to the dashboard page for your Twilio phone number (you lot tin find this by navigating to the phone numbers overview and then selecting your Twilio number). You should see a grade that looks like the i below. We need to paste the Ngrok URL with /sms appended to the end into "Messaging" > "A Message Comes In" equally a webhook. While you're at it, you can work ahead and fill in "Vox & Fax" > "A Call Comes In" with the URL and /call.
"A Call Comes In" and "http://016a0331.ngrok.io/sms" in "Messaging" > "A Message Comes In"." class="richtext-image left" height="247" sizes="500px" src="https://twilio-cms-prod.s3.amazonaws.com/images/dMXgw3Pk9MRURfoB5F-YalrNh4ORlHo1DTV7f65XFMUfpS.width-500.png" srcset="https://twilio-cms-prod.s3.amazonaws.com/images/dMXgw3Pk9MRURfoB5F-YalrNh4ORlHo1DTV7f65XFMUfpS.width-500.png 500w, https://twilio-cms-prod.s3.amazonaws.com/images/dMXgw3Pk9MRURfoB5F-YalrNh4ORlHo1DTV7f65XFMUfp.width-1000.png 1000w" width="500">
Twilio will at present know to ship an HTTP POST request to your webserver when your Twilio number gets a message which will hitting the endpoint we created above. You lot're now fix to text attendees through your proxy and receive replies. Try information technology by asking a friend to send a text message to the Twilio number! Alternatively, acquire some other Twilio number and transport 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 encounter whether your friend gets your reply as well. If you sent the number with some other Twilio number, y'all can encounter received messages in the console by navigating to "Messages Log" for your second Twilio number.
Telephone call and Receive Calls Using Your Twilio Number
Now that texting works, nosotros've near got a fully functional proxy number! The merely thing left is to fix up calling.
Forward calls with TwiML
Let'due south 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 (equally we configured above). We have to create a response that instructs Twilio to forward the phone call to our private number when nosotros receive that request.
TwiML is exactly such a response. TwiML is an XML encoded prepare of instructions that tell Twilio what to do when an incoming call (or SMS) is received.
In our instance we want to redirect a received call to our personal number. The snippet beneath does this using the TwiML verb . We pass two arguments: the number to which we want to frontwards the call and caller_id. Passing caller_id as None volition let us to come across the original caller on our telephone. 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 tin can meet their number, and when y'all call an attendee, they see your TWILIO_NUMBER.
Copy the following role below your send_message function but above the if-argument:
def perform_call(number, caller_id=None): response = VoiceResponse() # Connect the dialed number to the incoming caller. response.dial(number, caller_id=caller_id) return str(response) Make calls with Python
Next, we'll add two endpoints to our server: /call and /aliasing.
/call: One endpoint to receive calls
A asking to this endpoint volition exist made both when attendees phone call usa as well as when we want to call attendees. Fortunately, when a request is received at this endpoint, the payload includes the number of the caller. The number of the caller will permit u.s.a. to place whether we are calling ourselves past checking whether the number is equal to our PRIVATE_NUMBER. If it's not our number it is probably an attendee. In that case let'southward simply laissez passer the phone call on to the PRIVATE_NUMBER.
Add the post-obit higher up the send_message role:
@app.route('/call', methods=['Get', 'POST']) def call(): from_number = request.course['From'] if from_number != PRIVATE_NUMBER: # if an attendee is calling render perform_call(PRIVATE_NUMBER) If the number is our number nosotros have some more work to do. The code below uses some other TwiML verb, <Gather>, to deliver a warm welcome message asking united states of america to punch the number of the person we wish to call, followed by the #-symbol. After we type in a number and printing the #-symbol, the Gather implementation volition perform the activity that is specified when initializing the Assemble object. In our example, it sends a request with the gathered information to the endpoint /aliasing. Nosotros volition create the /aliasing endpoint in the next step.
Add together the following else argument to your /call road:
else: # if I am calling response = VoiceResponse() g = Gather(action="/aliasing", finish_on_key="#", method="POST") g.say("Hi organizer. Dial the number you want to call followed past the hash symbol.") response.append(chiliad) return str(response) /aliasing: I endpoint to transport calls
Within the aliasing endpoint, we volition get the digits that we entered on the phone and perform a phone call to that number. In this case we will laissez passer the TWILIO_NUMBER as the caller_id so that the person on the other end does not see our private number. Add together the following right below the send_message function nosotros but added.
@app.road("/aliasing", methods=['Go', 'POST']) def aliasing(): # Get the digit pressed by the user number = asking.values.get('Digits') if number: return perform_call(number, TWILIO_NUMBER) return "Aliasing failed." Test what we've built
Restart your server and that's it! Dial your Twilio number from a friend's phone and it should forward the phone call to your phone. Then try calling your Twilio number from your personal number, and y'all'll be prompted to input a number you'd like to call. Input your friend's number and yous'll encounter your Twilio number showing up on your friend'south screen.
You can encounter the complete projection on GitHub. Go on and make as many numbers as you'd like! If you don't demand the number anymore or need to ghost, just delete the number from your Twilio business relationship. If yous're interested in taking this projection to the next level yous could implement logic for a number that only redirects calls during work hours or even come up with semi-automated answers. To have this proxy number permanently running you will as well want to move your project to a server instead of your local motorcar.
If you lot have any questions experience complimentary to accomplish out at @naomi_pen or find me at naomi.codes.
How To Phone Number Masking - Sms Proxy Flowroute,
Source: https://www.twilio.com/blog/2018/02/phone-number-forward-mask-python-flask.html
Posted by: parishmanthaten.blogspot.com

0 Response to "How To Phone Number Masking - Sms Proxy Flowroute"
Post a Comment