Hello guys! Today I’m going to share my another experience in coding with a beautiful language PYTHON.
What exactly I did?
I have been working on python since 2 weeks. I have gathered information from my friend about APIs using Python. Then I have learnt Twilio API. This API enables us to send SMS/voice calls/MMS for a verified numbers from your account. Using this API, I created an application that will send a quotation as an SMS for every 10 seconds to my friend.
How I did all these?
This application is done using the libraries in python-
- requests to fetch all the quotations from the website
For this application I have fetched all the popular quotations about Christmas from the popular website BrainyQuote .
You can find the code for fetching all the quotations from my github repository
Now lets go deep into the coding part of the application.
from twilio.rest import TwilioRestClient import time
These two statements will import necessary modules for working with our application
- time module will enable us to send an SMS with a specified delay
- TwilioRestClient module is for authenticating, creating messages and sending to receiver (your friend).
auth_id = "your account SID here" auth_token = "your secret_token here" client = TwilioRestClient(auth_id,auth_token)
These statements will authenticate us to the Twilio API
You can get your account SID and auth_token from the Twilio website
Now we have to fetch the quotations from the text file we already have.
f = open("quotes.txt","r") for quote in f.readlines() : sendmsg(quote) time.sleep(10)
- The statement f = open(“quotes.txt”,”r”) will open the specified file in read mode.
- The statement for quote in f.readLines(): will loop us till all the lines in the file are parsed.
- The statement sendmsg(quote) will send the quote fetched to a function sendmsg().
- The statement time.sleep(10) will make the program to sleep for 10 seconds befor sending another quotation to the function.
Step 5: sendmsg() function
def sendmsg(body): message = client.messages.create(body=body,to="the verified receiver's number",from_="your twilio account number") print client.messages.get(message.sid).status
The sendmsg(body) module will create a message with body, to_address and from address.
This will also send the message as soon as it is created.
For conformation we can know the status of message
For full documentation visit: Twilio Docs
The full source is available at my github repository