Twitter is the social media site for robots. You probably have robot friends and followers and don’t even realize it! In this tutorial, you will write your own Twitter bot with Python and tweepy, and then set it loose in the world.
First we need to create a Twitter Application. Go to https://dev.twitter.com/ and log in with your Twitter account.
Under your account toggle, select ‘My applications’. On the following screen, select the option to create a new application and fill in the required information.
Once your new application is created, select its Settings tab and towards the bottom of the page click the ‘Read and Write’ radio button. Return to the Details tab and click the big blue button at the bottom of the page to generate your access keys.
Next, we need to install tweepy. tweepy is the library we will be using to access the Twitter API with Python. From the command line, run:
pip install tweepy
If you don’t have pip installed, run:
sudo apt-get install python-pip
Now it’s time to make our robot. Open your favorite text editor or IDE and create a new file (don’t use a word processor; it will load your file with unnecessary junk). Save it as helloworld.py
Below is our complete code. Enter your Twitter application keys and tokens accordingly:
#!/usr/bin/env python # -*- coding: utf-8 -*- import tweepy, time, sys argfile = str(sys.argv) #enter the corresponding information from your Twitter application: CONSUMER_KEY = '1234abcd...'#keep the quotes, replace this with your consumer key CONSUMER_SECRET = '1234abcd...'#keep the quotes, replace this with your consumer secret key ACCESS_KEY = '1234abcd...'#keep the quotes, replace this with your access token ACCESS_SECRET = '1234abcd...'#keep the quotes, replace this with your access token secret auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) filename=open(argfile,'r') f=filename.readlines() filename.close() for line in f: api.update_status(line) time.sleep(900)#Tweet every 15 minutes
That’s our robot. But it’s hungry. Let’s feed it! Create a new text file in the same directory as helloworld.py. Save it as helloworld.txt. Enter a few memorable lines, such as:
I’m a robot!
Robots are superior to humans in every conceivable way!
Be sure to use lots of exclamation points so your robot can be heard. Twitter is a noisy place. Also be sure there are no blank lines in-between your lines of text. Our robot is not an existentialist.
Now we’re ready to go! At the command line enter:
python helloworld.py helloworld.txt
Check your Twitter feed and you should see:
Let’s break that down into byte sized pieces.
Our first line of Python,
import tweepy, time, sys
includes the three packages we need for our program: tweepy, time & sys. We already know what tweepy is for. time will allow us to schedule intervals between our Tweets (so we don’t get in trouble with Twitter), and sys will allow us to feed our robot a file for it to read and Tweet.
The next line is how we feed the file to our robot.
argfile = str(sys.argv)
We’re assigning our text file to argfile. No, not arg as in the sound a pirate makes, but arg as in short for argument. When we run our program from the command line, we are passing the python interpreter two arguments, the first argument, argv, is our .py file, helloworld.py; the second argument, argv, is our text file, helloworld.txt. What we are saying here is that argfile contains the string, helloworld.txt.
The next big chunk of code is how we connect our robot to Twitter through our Application:
CONSUMER_KEY = 'YOUR CONSUMER KEY' CONSUMER_SECRET = 'YOUR CONSUMER SECRET KEY' ACCESS_KEY = 'YOUR ACCESS KEY' ACCESS_SECRET = 'YOUR ACCESS SECRET KEY' auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth)
Here we are creating a variable, auth, and via tweepy, we are authorizing our account with our consumer and access keys. We then create a variable, api, and via tweepy connect to the Twitter API with auth.
After that, we open and read the helloworld.txt file:
filename=open(argfile,'r') f=filename.readlines() filename.close()
Here we’re using the open() function to read argfile, which you will recall is holding the string helloworld.txt. We read the file with the parameter, ‘r’, for read. Next we read the lines of our file and pass them to a variable called f, for file. Finally, we close the file. Closing something you’ve opened is a good habit. Like the refrigerator.
The last block of code is where the magic happens:
for line in f: api.update_status(status=line) time.sleep(900)#Tweet every 15 minutes
Using a for loop, we iterate through every line stored in f. For each line, we send out a Tweet using api.updatestatus(line). Then we tell our robot to snooze with _time.sleep(900). The for loop will continue until it reads and Tweets the last line in f(or finds an error in your file), and will then exit.
That’s it! Keep in mind there are best practices to be followed on Twitter. You will want to check before you modify this code or you risk getting your account suspended. And that’s no fun for you or your robot.
Special thanks to robincamille for writing the post that inspired this tutorial.