6

Twilio - Send Text Message Using Python - JournalDev

 2 years ago
source link: https://www.journaldev.com/55941/send-text-message-using-python-twilio
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Working with Python is like working with an ocean of possibilities. Tell me what we cannot do with Python. It has numerous libraries whose applications are endless.

From data science to web development to AI, AR and VR, there is nothing better than Python. You might have used python for many automation assignments. Today, let’s discuss sending a text message using python with the help of an awesome library – Twilio.

More read: Top 24 Best Android Messaging Apps


1. What is Twilio?

Twilio is a communication API provider and globally lakhs of developers are using these API services to establish communication with their product users or the customers.

Basically, it is a customer engagement platform that also provides API for different communication channels such as SMS, WhatsApp, Voice, Text, Email, and Video.

Their messaging service is now used by over 3.4 billion contact numbers every year.

Twilio offers many other developers tools such as documentation, consoles, and debuggers which are mainly used to account management and monitoring.

This is a short introduction to the Twilio platform. In the next steps, we will see how we can send the text messages using python with the help of Twilio SMS API.


2. Platform Introduction

Before we dive into the technical part of the tutorial, I will first take you to the platform and you have to create an account to get your SID and TOKEN as well.

To sign up for Twilio, follow this link – Here

Twilio 1
  • Enter the required details to create your account with Twilio.
  • After this, you have to verify your mail and phone number.
  • By verifying your credentials, you will see the homepage of the platform.
  • Click on -> Console in the top right corner of the homepage.
  • After that, you can get to see your ACCOUNT SID and AUTH TOKEN as shown below.
Twilio 2
  • Copy and save your account info in some other safe place for future usage. You can also get these details from Twilio account whenever you want.
  • It is not a free platform. So, keep an eye on your FREE TRAIL as well.

Once you have completed your account setup and copied the details, You can start with installing the Twilio library in Python and write some code to get it started.


3. Installing the Twilio Python Library

We need to install the Twilio library in Python to work with it. Execute the below code to PiP install the library into your working environment.

#Installation
pip install twilio
Twilio 3

Observe the last line for the successful installation message. If you can see it on your screen, you are good to go.


4. Sending Text Using Python Twilio

No more Drama, it’s time to code :P.

I hope you have saved your credentials. Now, let’s import the Twilio library into python and then we work on establishing a client and with your saved SID and TOKEN, hopefully, we can send the SMS.

#Setup
import os
from twilio.rest import Client
#Add your personal SID and TOKEN at the respective places in the code
sid = os.environ['XXXX']
token = os.environ['XXXX']
#launch the client
client = Client(sid,token)
#Initiate the message
your_message = client.messages \
.create(
body='Hey, I am Twilio, a communication API provider',
from_='+911234567890',
to='+11234567890'
)
print(your_message.sid)

I have used dummy numbers just for illustration. All you need to do is to add your ACCOUNT SID number and AUTH TOKEN to the code and launch the client. 

Next, you have to define the message and add the form and contact numbers to the code to send the SMS from one phone number to another phone number.

Once you execute this code through the command line, the number in the ‘to’ code will get an SMS with the defined message in the code.

Twilio is one of the fastest and easy-to-use communication API platforms to merge with Python. When you want to send an SMS to your dear ones, remember the name – Twilio.


Wrapping Up

Twilio is a communication API provides which can be integrated with Python. You can sign up for the platform and get your SID and TOKEN to get started. As I have shown in this tutorial, we can do a lot more than merely send SMS. You can communicate your message through audio, video, and mail as well.

I hope you find this interesting. That’s all for now. Happy Python!!!

More read: Communication API’s


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK