Some years ago SMS messages were the most used medium, and cheaper, to communicate with friends or family.
With the arrival of smartphones and mobile messaging applications, it seems that SMS were left aside and are only used for notifications from our telephone provider or some other commercial announcement..
Today many companies continue to use the mobile messaging service, above platforms such as WhatsApp or Telegram, since SMS does not depend on the recipient having an internet connection, making them an excellent medium to launch publicity or send notifications to clients.
For these and many other reasons, having a system that is capable of sending SMS messages in an automated way is a great advantage..
In this short tutorial we will teach you how to send SMS messages from the Python programming language.
The first thing would be to install the appropriate library for our purpose, in this case we are talking about Twilio and you can install it for free using PyPi:
pip install twilio
In order to use the Twilio service it is necessary to obtain credentials, which you can acquire by registering on their website, you can try the service for free during 7 days:
If you have questions about how to obtain the credentials you can consult the Twilio documentation on its website:
However, to be able to send a message it is necessary to follow the following steps:
- Import thetwilio customer sincetwilio.rest .
- Get and store theaccount_sid Yauth_token from your Twilio account.
- Create an instance ofClient goingaccount_sid Yauth_token .
- Now, you can send the message usingclient.messages.create (body = message, from_ = from_number, to = Verified_number).
- message: It is the message in text format to send.
- from_number: It's the Twilio number.
- Verified_number: It is the destination number to send the message (if you are using a free account the number must be verified in Twilio)
The complete code can be found below:
from twilio.rest import Client
account = "ACXXXXXXXXXXXXXXXXX"
token = "YYYYYYYYYYYYYYYYYY"
client = Client(account, token)
message = client.messages.create(to="+52xxxxxxxxxx", from_="+52xxxxxxxxxx", body="Saludos desde Facialix")
Once you run this code the target phone will receive an SMS message, like the following:
And in this simple way you can send SMS messages using the Python programming language. In future tutorials we will teach you how to make calls using Python.