Hello Folks, I am back with one more new thing which I have and tried to automate and I am happy to say that I did it! 🙂
Recently, I attended Amma’s 64 Birthday and I can say that I enjoyed a lot, along with the Seva. Meanwhile, I was posting a lot of tweets daily as an update to everyone. But I was little fed up because when I need to post a tweet, I have to
- Open Twitter app then
- Click ‘Compose a new tweet’ and then
- Type all that what I have to tell then
- Click on post and
- Wait for some time to see whether it is posted or not.
Such a huge process!! (surely for me bcoz, I am too lazy..😜 )
Later this situation reminded me of my dear friend, Python. I then decided to automate this such that simply running a python script will post my tweet.
In this tutorial, you will learn how to send tweets using Python. I will try to keep it as simple as possible. (Make sure you have Python installed in your system already 😛)
- You must add your mobile phone to your Twitter profile before creating an application.
- Go to: Settings -> Add Phone -> Add Number -> Confirm -> Save.
- Do not forget to turn off all text notifications.
- Set up a new app
- Go to: Twitter Apps -> Create New App -> Leave Callback URL empty -> Create your Twitter application.
- You should see “Your application has been created. Please take a moment to review and adjust your application’s settings”.
- By default, app’s access level is read-only . To send out tweets, it requires write permission.
- Go to: Permissions tab -> What type of access does your application need? -> Choose Read and Write -> Update Settings.
- You should see “The permission settings have been successfully updated. It may take a moment for the changes to reflect.“
- Time to get the keys and access tokens for OAuth.
- Go to: Keys and Access Tokens tab. You’ll see this under “Your Access Token” : You haven’t authorized this application for your own account yet. By creating your access token here, you will have everything you need to make API calls right away. The access token generated will be assigned your application’s current permission level.
- Click Create my Access Token.
- You should see “Your application access token has been successfully generated. It may take a moment for changes you’ve made to reflect. Refresh if your changes are not yet indicated. This access token can be used to make API requests on your own account’s behalf. Do not share your access token secret with anyone.”
- Verify that you see access token/secret – and the permission is set to “read and write”.
- From this page, note down the Access Token, Access Token Secret, Consumer Key (API Key), Consumer Secret (API Secret) and then type those in the keys.py script. Consumer Key/Secret help twitter identify the app and Access Token/Secret help twitter identify the user (that is you).
#save this file as 'keys.py'
ckey = '//Consumer Key//'
csecret = '//Consumer Secret//'
akey = '//Access Token//'
asecret = '//Access Token Secret//'
- We will use tweepy to access Twitter’s API. You can install it using pip: pip install tweepy (try to setup a virtualenv for this – they are very useful).
Finally, by running this simple python script sends out a tweet:
#save this file as 'tweet.py'
from keys import *
auth = tweepy.OAuthHandler(ckey,csecret)
api = tweepy.API(auth)
api.update_status('//Your tweet goes here..//')
You can find these files also in my Github repository – PyTweet
That’s it. Your tweet will be posted just like mine which you can see in my twitter profile.