It's 2019. You've heard of bots. There are chat bots, email bots, web scraping bots, and, social media bots. Have you created a bot yet? It's surprisingly easy. Probably why they're all over the place. Embrace our bot overlords by joining their rank.
In this article, I'll show you how you can leverage Python to interact with your Twitter, Reddit, and Instagram accounts automatically.
Working With Python
This walk-through uses Python as its language of choice. Python's ease-of-use advantages outweigh any speed deficiencies it has compared to other languages. You can download Python for almost any OS. Use the editor of your choice.
Python distributions come associated with the utility called
pip
. At the time of writing, there are over 168,000 libraries available for download using
pip
. For the bots you will create today, only three are needed.
From the command line, you can install the libraries needed using these three lines.
pip install --upgrade InstagramAPI
pip install --upgrade tweepy pip install --upgrade praw
Now you'll be able to import these libraries where needed.
Getting Application Keys and Secrets
A wise philosopher once said "Secrets, secrets are no fun. Secrets, secrets hurt someone." Respectfully, the bots need secrets. The next step in setting up your bots is to allow them access to the API of each site. To do this, you'll need an application key or secret.
As you collect keys, secrets, tokens, and more (sounds like a fun game!), put them all into a single file called "credentials.py." This file would look something like this:
# keys
twitter_consumer_key = ''
twitter_consumer_secret = ''
twitter_access_token = ''
twitter_access_token_secret = ''
reddit_client_id = ''
reddit_client_secret = ''
reddit_user_agent = ''
instagram_client_id = ''
instagram_client_secret = ''
Very important privacy suggestion: do not commit this file any source control (e.g. Git). You do not want anyone else to have these keys and secrets. They are called secrets for a reason.
Setting Up Twitter Access
Check out our guide to setting up a Twitter bot with Node.js will show you how to create your Twitter app. Note that Twitter will take some time to verify your account and ensure you're not creating a spam account.
After following those steps, copy the consumer key, consumer secret, access token, and access token secret into your credentials.py file.
Setting Up Reddit Access
Getting your Reddit client ID and secret is very similar to Twitter. You'll need to register an app as a Reddit User Agent. After doing this, you should absolutely put "Reddit User Agent" on your resume.
Sign in to the Reddit account of your choosing. Navigate to the apps preferences page and Click create another app. Name your user agent something useful. Select script for the purpose of your app. The about URL can be left blank. Fill in any address for the redirect box.
After you Press create app, the following screen will be presented to you with a list of your apps. The digits under your application name represent the client ID and the client secret is located below that. Copy these values to your credentials.py file.
Setting Up Instagram Access
Instagram access differs from Twitter and Reddit. Instead of registering an app, you use your username and password. I suggest creating a separate public account and using those details for your bot.