In this series of articles I will be creating a Twitter bot in Python (because, maybe I said this before, but Python is THE language). Creating a basic bot is something really easy and at the end of this article we will have a basic bot posting tweets.
All the code up to date is in a public repository in github, so you can freely clone it from here
What we need:
- Python (the glorious computer language) 3.X
- Tweepy (there are other libraries, but, this one is great).
- A Twitter account (that will be the bot’s account)
- A Twitter app (I will explain this in the first point of the article).
Create a Twitter App
First of all you need a Twitter app, this is a way to tell Twitter guys that you will be using their API. To do that you need to go to developers link in Twitter, then search for “Manage my apps” and there you will see a “Create new App” button.
From there is just to complete a form with some basic data about the application you want to create:
- Name of the application
- Website (a place where people can download, use or get info about the app).
- Callback URL (optional, it depends if you will use it or not).
Once you save, you will have a access token, access secret token, consumer key and a secret key to access your account and do things with your bot.
Ok, now Twitter know we are going to do something with his API, so we need a library to easily access that API. We are going to use Tweepy for this tutorial but there are plenty of other libraries out there. Simply pip install it (you may need sudo on Linux)
pip install tweepy
Now we can start programming!
Now, lets open our favorite python (the almighty) IDE (in my case is Pycharm), and lets start
The first class I will like to create is the connector class, I will call it Twitter.py
import tweepy class Twitter: def __init__(self): self.key="your key code" self.secretKey="your secret key code" self.access = "your access token" self.secretAccess = "your secret access token" self.auth="" self.api ="" def connect(self): self.auth = tweepy.OAuthHandler(self.key, self.secretKey) self.auth.set_access_token(self.access, self.secretAccess) self.api = tweepy.API(self.auth) def tweet(self,message): self.api.update_status(message)
Let me explain a little what we did here:
First I imported Tweepy so this class can use it (kind of obvious but I am here for all level of developers).
In the init part of the class I added the keys I get when I created the app in Twitter and two more variables for auth and the api manager.
The first method “connect” is self descriptive 🙂 it does the OAuth part, connecting to Twitter (if you want more info about OAuth please check this website) and sets the API manager for future use.
Now that we have this class (in future articles we will update this class, but for now it is a good start) we want to tweet something, so, lets create another class to do a main class to send a tweet (it is obvious that this will be updated as the project grows).
from Twitter import * twitter = Twitter() twitter.connect() twitter.tweet("Hello World!")
This works, but, second time you run it, it will not post your message because you cannot duplicate twits. But the idea of this first article was just to understand how you can twit something as a bot, from here you can do lots of things. For example you can do a loop to twit different things with some time in the middle.
Stay tuned, next time we will do lots of fun stuff with Tweepy!