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.
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:
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:
Hello World! 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,
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.
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:
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:
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:
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.update_status(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.
Learn With Us
We want to take you on a journey through the other side of the creative process: making a living. Explore the necessary evils of business as we develop a creative media studio using free and open source software. Sign up for our New Moon newsletter delivered on, you guessed it, every New Moon.
Master the commands and structure of the fastest-growing computer operating system, Linux. Join super friends Adelie the penguin and Aramis the gnu on surreal adventures saving files in the file tree. Learn the essential commands you need to take greater control of your computer including help, cd, pwd, ls, mkdir, rmdir, cp, mv, rm, cat, and man.
How To Write a Twitter Bot with Python and tweepy was posted by Jared on . Jared is one half of the creative force behind Dototot. In addition to writing scripts and tutorials, he draws and animates both the digital and the analog.