How To Write a Twitter Bot with Python and tweepy
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.


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:
Below is our complete code. Enter your Twitter application keys and tokens accordingly:
Hello World!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.
I’m a robot!
Robots are superior to humans in every conceivable way!
Now we’re ready to go! At the command line enter:
Hello World!Let’s break that down into byte sized pieces.
Our first line of Python,
The next line is how we feed the file to our robot.
The next big chunk of code is how we connect our robot to Twitter through our Application:
After that, we open and read the helloworld.txt file:
The last block of code is where the magic happens:
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.

