How I Wished my Friend for Christmas Eve?

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.

Christmas Wishes

How I did all these?

This application is done using the libraries in python-

  1. requests to fetch all the quotations from the website
  2. time
  3. twilio

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.

Step 1: 


from twilio.rest import TwilioRestClient

import time

These two statements will import necessary modules for working with our application

  1. time module will enable us to send an SMS with a specified delay
  2. TwilioRestClient module is for authenticating, creating messages and sending to receiver (your friend).

Step 2:


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

Step 3:

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)

Explanation:

  1. The statement f = open(“quotes.txt”,”r”) will open the specified file in read mode.
  2. The statement for quote in f.readLines(): will loop us till all the lines in the file are parsed.
  3. The statement sendmsg(quote) will send the quote fetched to a function sendmsg().
  4. 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

Explanation:

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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s