Python | Send SMS with Twilio

Python Methods and Functions

In this article, we will see how to use the Twilio API to send SMS using Python. This will be a very quick and easy guide to this very interesting task.

First, we need to create an account an account on the Twilio official website, to get id and token . This is a paid service, but you will be credited with an initial amount first.

Steps to create a Twilio account:

Go to the Twilio registration page . Complete the registration by filling in the required details.

From the console (dashboard panel) copy ACCOUNT SID AUTH TOKEN ACCOUNT SID and AUTH TOKEN .

Install the Twilio library using pip.

 pip install twilio 

Below is the Python implementation:

# twilio import

from twilio.rest import Client

 
# Your account Sid and Auth T oken from twilio.com / console

account_sid = ' ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

auth_token = 'your_auth_token'

  

client = Client (account_sid, auth_token)

 
& # 39; & # 39; & # 39; Change the value of & # 39; with & # 39; to a number
received from Twilio and the value "before"
with a number, to which you want to send the message. & # 39; & # 39; & # 39;

message = client .messages.create (

from_ = '+ 15017122661' ,

  body = 'body' ,

  to = '+ 15558675310'

  )

 

print (message.sid)

In the above code, just replace account_sid and auth_token values, which you get from Twilio. Also, replace the body of the message you want to send and bingo!

Exercise:
Extract emails from your account and forward the subject and mailing address of the recipient as text message to your mobile phone. You can even filter it by limiting it to forwarding only important emails.





Get Solution for free from DataCamp guru