Building a messaging system Using Twilio via the REST API and Python

How do you send and receive response via SMS on your phone using Twilio and Python?

Well follow me and I will show you the best way to do this.

Firstly you need to register a free trial account on Twilio using your number. Use this link to register: https://www.twilio.com/try-twilio

After registration you are given a $15 free credit so use it wisely.

To start sending message you need to get a number generated by Twilio (If you are using the trial account), click on GET TRIAL NUMBER,

Trial Number

the best Number to choose from is the US because it has the capability of sending MMS, SMS, Fax and also placing a phone call.

First we need to copy some important information from the dashboard, which is the ACCOUNT SID and the Authentication Token, you can copy that and paste in a sticky note. To send SMS you need to add the mobile numbers you intend sending SMS to in your verified phone numbers on Twilio, to do this click on the three dots right (All products and services) underneath the home button then click on phone numbers , then click on Verified caller ID’s to add the list of phone numbers using the + button. After adding your numbers, make sure python is installed, python 2x and above will suffice. You will need to install some library using pip.

Use this command on your terminal to install Twilio:

pip install Twilio

After succesfully installing the Twilio library , write this code in you preffered Text IDE:

Sending SMS code

Run the code in your command line by navigating to where the document is saved then type:

python name_of_file.py

you should receive an SMS on the phone number you specified.

How about replying to a message? A typical example is you asking your customer to perform some action using SMS then you send a response based on the action performed.

To do this Twilio uses a user defined HTTP callbacks known as Webhooks, for this post I will be using ngrok as my Webhook. To install ngrok, visit this page: https://ngrok.com/download and follow the instructions to check if it was successfully installed, fire up your command line and type:

./ngrok help

You should see this:

ngrok Success

Now that ngrok is installed

It’s time you install some other library called Flask use this command in your command line:

pip install Flask

Its time to write some python code:

Replying SMS code

Its time to configure your Webhook, type this command in your command line to open a port of connection between Twilio and ngrok:

./ngrok http 5000

ngrok webserver

Go back to your Twilio dashboard and on the phone numbers page, click on the number that was generated, then scroll down to where you have the Messaging menu, on the tab with the title: A MESSAGE COMES IN, delete the link that was generated by Twilio and paste the link generated by ngrok. The on with with the title:

Forwarding http://3………...ngrok.io → localhost 5000

Then click on save

Change your webhook to ngrok generated http callback

Now its time to test our code, open your command line and tyoe the command:

python file_name_2.py

You should receive an SMS and also when you reply with Hi you should get a response with How are you doing.

N.B: Your PC needs to be connected to the internet.

SWE| DevOps Engineer| Terminal Freak| Data Scientist| Building Community| https://adefemi171.github.io