In this tutorial you will learn how to do very simple twitter bot. Requirements is Linux/Mac system, but it should be possible to get this on windows.
Install python-twitter
First, you need to install python-twitter package. Installion is simple. Just download package from python-twitter’s website and follow instructions.
Lets get started
In this code we import libraries needed to get this working. First library is the twitter-python package and another is time-library for creating simple timer. We need timer for doing a loop. Commands library is needed to get output from external command. (In this case, uptime)
#!/usr/bin/python
import twitter
import time
import commands
Intializing api
api = twitter.Api(username='username', password='password')
This will intialize twitter api and create new class, api. Change username and password to your accounts details.
Tweeting something
while 1 == 1:
api.PostUpdate(getoutput('uptime'))
print(getoutput('uptime'))
time.sleep(3600)
This will generate infinite loop that will send twitter update every hour. Tweet contains output of uptime command. This also prints output to console so its easier to see what is tweeted.
I run this command inside screen, so it will run 24/7 tweeting uptime of my vps.
If you want to learn more about python-twitter I recommend to read docs on python-twitters website.
Download source code of this tutorial: twitter_bot.py