BlueJay: A Simple Twitter Bot Written in Python

Roland Gemayel
Oct 9, 2020 · 5 min read
Image for post
Image for post
Image by Chanda Renzi from Pixabay

Are you tired of the social media game but still want to maintain your online presence?

I definitely am! So I created a Twitter bot called BlueJay which will post Tweets on your behalf and retweet posts that may be of interest to your followers.

To make this tutorial easier for you, I recommend downloading all files from the Github repository. All files should be in the same folder or directory.

Library Requirements

pip3 install tweepy

Creating your Twitter API Credentials

The credentials can be obtained from your Twitter Developer account and are comprised of four text strings:

  • Consumer key
  • Consumer secret
  • Access token
  • Access secret

Assuming you already have a Twitter account (if not, then sign up for one before proceeding), you will then need to create a Developer account.

Step 1 — Create a Developer Account

After you follow all the steps and answer all the questions about why you want to create a developer account, you will get to a Dashboard that looks like this:

Image for post
Image for post
Image by Author

Step 2 — Create an Application

To create an app:

  • go to your Twitter projects and apps page,
  • click on “+Add App” to add the app to a project, or click on “+Create App” to create it as a stand-alone app,
  • name your app, ex: “blue_jay_bot
Image for post
Image for post
Image by Author
  • after you click Complete, you will be provided with an API Key, API Secret, and a Bearer Token. Make sure you save these PRIVATELY. These credentials will be used by BlueJay to access your Twitter account.
  • In order to allow BlueJay to Tweet, we need to change the settings of the app we just created to allow Reading and Writing and Direct Messages (optional). To do so, click on the app name on the side bar, and then click on “Edit” under the App permissions
Image for post
Image for post
Image by Author
  • choose Read + Write and Direct Messages, and click Save.

Step 3 — Create Account Authentication Credentials

  • click on the name of the app on the side bar, then click on “Keys and tokens” under the name of your app in the middle of the screen.
Image for post
Image for post
Image by Author
  • under Authentication Tokens, click on the “Generate” button next to Access Token & Secret, and save this information PRIVATELY.

Change your Python Credentials

For example, your consumer key should look something like this:

consumer_key = '73y6JtrhdKjiGKtGg9ynzdSP3' # This is a fake key

This information will be saved in your local copy of the file and is not accessible by anyone other than you.

BlueJay — Tweet App

To tweet anything you want at specific time intervals, first open the my_tweets.txt file and add the texts that you want to tweet. Each line will be sent out as a separate tweet. For example, I will tweet the sample text below:

The Python script that will be connecting to your Twitter account and sending these tweets is called bluejay_tweet.py and includes the following code:

You can adjust the SLEEP_TIME parameter, which puts the bot to sleep for the desired time in seconds in between tweets. But note that a small value for this parameter means that the bot will tweet more frequently, which may result in Twitter banning you from using their API for frequent requests.

To start the bot, open a Terminal or Command Line, navigate to the folder where the files are located, and run:

python3 bluejay_tweet.py

You should then see the tweets on your Twitter page

Image for post
Image for post
Image by Author

BlueJay — Retweet App

The Python script that will be connecting to your Twitter account and sending these tweets is called bluejay_retweet.py and includes the following code:

There are four parameters that you can adjust in the above code.

  • QUERY is the hashtag that you want to search in Twitter. This will be used in the search function to return the latest tweets by others with your desired hashtag.
  • LIKE is a True (Yes) or False (No) parameter that will “like” a retweeted post if it is set to TRUE.
  • FOLLOW is a True (Yes) or False (No) parameter that will follow the user whose post you are retweeting.
  • SLEEP_TIME is the time in seconds between retweets.

To start the bot, open a Terminal or Command Line, navigate to the folder where the files are located, and run:

python3 bluejay_retweet.py

You will then see retweeted posts on your Twitter profile.

Happy Tweeting!

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store