Twitter is a social media platform where people send tweets that act as 140 characters of snippets of whatever information you want the world to see. With that in mind, computers can and do navigate the Twitterverse posting updates, following accounts and interacting with real people. With very little work we can build a simple bot that can tweet on your behalf.
- Twitter Set-Up
- Creating our tweets
- Python fun!
- Finishing touches…
The main hurdle facing us in our quest for Twitter takeover is installing the right tools. We are going to use Python for our main script plus one library to help us interact with Twitter. Starting off, let’s install Python.
(If Python is already installed, feel free to skip but make sure you have installed pip)
(If you are having issues installing Python, check this out)
The macOS Terminal is an application you can use to access the command line interface. Y can find it by going into Finder, navigate to your Applications folder, and then into the Utilities folder. From here, double-click the Terminal to open it up. You can also use Spotlight by holding down the command and spacebar keys to find Terminal by typing it out in the box that appears.
Welcome to your Terminal! We are going to install Homebrew which is Mac’s package manager. This will allow us to install and update Python and any related tools. Go ahead and enter the following into the terminal –
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now, this should take a few minutes to install but once that is done we can install Python. To do so we type in the following –
brew install python3
This will install Python 3, pip, setuptools and wheel for us. Pip is Python’s package manager and we will be using that next!
(If you have issues downloading on Windows, this should help)
First, we will download Python directly from Python’s website: Download
Then we will have to set a PATH environment variable. To do this we navigate to our Advanced System Settings of your computer. Go to Control Panel->Advanced System Settings-> Advanced-> Environment Variables. Once there, click Environment Variables to set up the Python Executable for our Command Prompt.
Here we see that there is already a PATH variable declared with some other file path directories already set up. Yours may look different and you may not have a PATH variable declared so click New and add PATH as a variable name. Highlight the Path variable, then click edit. From here, add the following lines with “New”:
Yay! We have Python installed. We can check by running python in your Command Prompt. We can access our Command Prompt by right clicking the Start Menu, selecting Run and typing in cmd.
We will be using Pip, which will allow us to download packages for us to use with Python. As of version 2.7.9, Pip was included as a Script in our Python download. Try running python -m pip -V. If it says something along the lines of “pip 8.1.2 from …” then everything worked! However, if you have issues running the Pip command you may have to install it locally first. These directions here or here should help you install Pip.
In either Mac Terminal or Windows Command Prompt, run the following –
pip install tweepy
This will allow us to interact with the Twitter API. Let’s set up Twitter next!
Twitter Set Up
If you have not already done so, make a Twitter account. We will need one to get access to API keys. After that has been completed; in your browser navigate to https://developer.twitter.com/apps and sign in with your account information. Click “Create New App”, afterward we should be greeted by the following –
Fill out the required information like is shown above. You may be required to add your phone number to your account information before being able to create your application.
We will come back to manage our application in a little bit. Let’s move onto creating some Twitter Content.
Creating our Tweets
Let’s do a little setup. On your desktop, right click and create a new folder. Navigate inside of the folder and create a file called ‘content.txt’ which will house all of our Tweets and ‘app.py’ which will read content.txt and tweet out its contents. Open content.txt in any text editor you wish. I enjoy using VS Code which can be downloaded here.
Next, we are going to fill that file with phrases, sentences, and words. Enter in the following or your own personalized messages.
Hello World I’m a robot! Robots are superior to humans in every conceivable way…
Save that file and we are good to go! We will use Python to tweet out each line of our content file next.
Now that our content file is good to go, let’s open our app.py file in VS Code. Let’s write the following.
# tweepy will allow us to communicate with Twitter, time will allow us to set how often we tweet import tweepy, time #enter the corresponding information from your Twitter application management: CONSUMER_KEY = '...' #keep the quotes, replace this with your consumer key CONSUMER_SECRET = '...' #keep the quotes, replace this with your consumer secret key ACCESS_TOKEN = '...' #keep the quotes, replace this with your access token ACCESS_SECRET = '...' #keep the quotes, replace this with your access token secret # configure our access information for reaching Twitter auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) # access Twitter! api = tweepy.API(auth) # open our content file and read each line filename=open('content.txt') f=filename.readlines() filename.close() # for each line in our contents file, lets tweet that line out except when we hit a error for line in f: try: api.update_status(line) print("Tweeting!") except tweepy.TweepError , err: print(err) time.sleep(90) #Tweet every 2 minutes print("All done tweeting!")
(If you want more information about Python you can check out Google’s Python Class)
As you can see we need consumer and access information to actually Tweet. At this point navigate to your Twitter Bot application management page from earlier (https://developer.twitter.com/apps). If you click your bot that you made earlier and navigate to the Keys and Access Tokens page, you will notice there is a Consumer Key and Consumer Secret listed. Copy both and put them in the appropriate place (make sure to keep the quotations).
CONSUMER_KEY = 'Your Key Here' #keep the quotes, replace this with your consumer key CONSUMER_SECRET = Your Secret Here' #keep the quotes, replace this with your consumer secret key
Finally, scroll a little further down and click “Create my access token”. We will use the Access Token and Access Token Secret to update the corresponding information in app.py.
ACCESS_TOKEN = ‘Your Access Token Here' #keep the quotes, replace this with your access token ACCESS_SECRET = 'Your Access Token Secret Here' #keep the quotes, replace this with your access token secret
That’s it! We are ready to go! From your Terminal or Command Prompt navigate to your folder that you created on your desktop. You can do so easily by entering in `cd ` and drag and dropping the folder from your desktop to the Terminal or Command Prompt. Next, enter the following – python app.py
Your BOT is now tweeting! Leave your terminal open, and your code should exit when it is done tweeting all of the content you wrote previously. In a future post, we will expand upon this bot and show how to deploy your Twitter Bot as well.