Saturday 5 January 2013

The Python Tutorial for Twitter Bots

A Python Tutorial For Bots With Twitter
If you are a programmer and if even once in your lifetime a thought popped out of your mind to build a python bot for twitter then you have arrived the right place.
Today I will write down the step by step tutorial to build a simple python bot.

  • First Things First Up
  1. As you cannot directly start exercising and you need to warm up first, in the same way you need a twitter application for the purpose of building twitter bot. If you already have an application with all 'read,write and access direct messages' privileges set you can directly skip below steps.
  2. To create an application go to http://dev.twitter.com and sign in with your twitter account
  3. Then click on create new application
  4. Fill out name, description, and website fields also one thing you need to keep in mind that you don't need to specify the callback URL field as no callback is needed for our bot. And then click on create your twitter application.
  5. After the application has been created go to settings tab and click on 'read,write and access direct messages' radio button under Application Type and then click on update this settings(Note: This is one of the most important steps as because if you forget to give all privileges in app you will get exhausted finding out the error after whole set up is ready.)
  6. Now again go to details tab and click on 'create my access token' and now go to oauth tool tab and you can see all the keys and secret ready.Keep it open or copy all the keys and token because you will need it again.
  •  Gearing Up Further
  1. Now I believe you already have python ready on your laptop then you can move along as below else check out How to install python on windows? on this page.
  2. Now download the following following zipped file here and extract it in a suitable folder.
  3. Now using command line in your respective directory enter the following command.
    python setup.py build
    python setup.py install
  4. Now create a new file and name it login.py and enter the following python code in to it.
import twitter 

#enter your consumer key,secret and access token secret,key in below function as parameters 

api=twitter.Api(consumer_key='',consumer_secret='',access_token_key='',access_token_secret='') 

#now using PostUpdate method of the api we can use to post an update on twitter account 

status = api.PostUpdate('My first tweet using python..:):):):)') 
print status.text 
   4. Now go to command line and the respective dir and then run your login.py file.
   You can get all the Api reference here.
Now everything goes well then congrats you have made your first tweet auto updater using python!!
Also you can check your tweet on your twitter page.
The following post shows how to use crontab to automatically update tweets.
   



Ads : Electronics, Cars, Fashion, Collectibles, Coupons and Much More Online Shopping from eBay www.ebay.com is a wonderful site that allows you to have a unique shopping experience Malaysians have it here: www.ebay.com.my For uk:www.ebay.co.uk

11 comments:

  1. Two problems I was hoping you could help with.

    1. I keep getting a syntax error on line 8 "twitter status = api.PostUpdate('')"

    2. On step 4. where I go to command line and run login.py. My command line refuses to change to the directory where login.py is. Any ideas why?

    ReplyDelete
    Replies
    1. Hey,
      First of all did the setup.py gave any error on install??
      if not then please provide me with some more detail of the code so i can help you..:)
      For second problem can you tell me which OS are you using?
      And also mention the exact error given by the console!!

      Delete
    2. No everything went fine on the install.

      When I run the program I get this.. http://screencap.net/f/b21cab05.png. Thanks for the help.

      Delete
    3. Really sorry a mistake from my side ..
      Make a small change on that line
      instead of:
      twitter status = api.PostUpdate('My first tweet using python..:):):):)')

      make it:
      status = api.PostUpdate('My first tweet using python..:):):):)')

      And i guess that should work fine..:)

      Delete
  2. Actually a mistake in formatting the post now it should look fine

    ReplyDelete
  3. The mediafire link is broken. :(

    ReplyDelete
  4. Thanks for posting this tutorial. I'm getting an error when I run login.py:

    ImportError: No module named oauth2

    Not sure what to do.

    ReplyDelete
  5. in the twitter folder go to the oauth2-1.5.211 folder from command line
    and the type
    python setup.py install
    and then you can run login.py with your own api secret, tokens and keys..

    ReplyDelete
  6. Thanks for the tutorial.

    When I run my script I get 'None' returned. What does this mean? It doesn't post to my Twitter account.

    ReplyDelete
  7. I have never worked with all this before. But for school I'm making a Twitter Bot. Now, at step 3 I get stuck. My command line closes immediatly. I have searched and put " input() " in the code from the self made path, yet nothing changes. Can you help me?

    ReplyDelete