Create Twitter Bots for Your Medium Articles With Python*oPqz5F0Ukt4lvGHv

Original Source Here

Create Twitter Bots for Your Medium Articles With Python

Let’s explore Twitters Rich Tweepy API through the creation of Twitter bots

Photo by Souvik Banerjee on Unsplash

As a new graduate and recent hire, I have been thinking deeply about my career path and its current trajectory.

After seeking out the advice and mentorship of many superiors at my firm — I consistently got one piece of advice — to develop my brand as a consultant! It was at that point I decided to open up a Twitter and Medium account and begin producing content.

The idea of creating automated systems to help develop my brand online was intriguing and led me to create a Twitter bot.

In this article, I will show a step-by-step guide for recreating the Twitter bot I use to automatically share my most recent Medium articles. It also has the capability to automatically retweet based on hashtag or username.


In order to follow along a few things are necessary:

  1. Python 3
  2. Any IDE
  3. A Developer Account on Twitter
  4. Linux Operating System (Optional)
  5. A Basic Understanding of Bash (Optional)

Note: Click here for instructions to get a developer account. Make sure to save your API keys. You’ll need them later.

Project Structure

I partitioned this project into 3 separate python files. fetches the most recent medium article and posts it to Twitter. Retweet retweets based on hashtag or username.

Main puts both scripts together and connects to a specific Twitter account. The file installs dependencies and is not necessary. will be explained in the CronTab section.

Before you begin coding make sure to install two dependencies tweepy, and requests as follows:

pip install tweepypip install requests

Make sure to do this in the directory where you are coding. Tweepy is the Twitter API we will be using, while requests are used to construct HTTP requests. In effect, the two lines above are the contents of the file.

Fetching Medium Posts

Here we’ll be working on the file.

It requires two functions. One to fetch the most recent article and another to construct a Twitter post with the corresponding link.

In order to start, we’ll need three imports to parse a URL and do HTTP requests. They are as follows:

from urllib.request import urlretrievefrom urllib.parse import urlparseimport requests

Here’s the full source code of how to fetch your latest Medium article and extract its contents:

In the above code:

The fetch_article function takes a Medium profile URL and retrieves a list of posts using an HTTP request (which returns a stack of HTTP articles).

Create the URL by including your username in the {@username} field of the Medium RSS feed as below:


The first record in the HTTP response is the most recent article. A dictionary is constructed containing the title, link, and thumbnail of this article and returned.

To get the article, do a basic get request on the URL and create a dictionary as mentioned. After that is done, we created a function Title_Link_Post that uses basic string formatting to create the content for our Twitter post.

This is all that’s required for the file.

Retweeting Based on Hashtag

In this section, we will be completing the file to automatically retweet a preset number of posts based on hashtags.

Here we are taking a function that takes three input variables a user authenticated through OAuth — called auth (which we will create in, the hashtag as a string, and the number of tweets to retweet.

In order to do this, we will take full advantage of the tweepy API by using its Cursor function as shown below:

tweepy.Cursor(, hashtag).items(num) specifies that the authenticated account in question is searching, hashtag is simply a string to be searched for.

The extension .items(num) grabs, where num is an int representing the first article count.

After the tweets are found we will be using the .retweet function to share them.

Here’s are the functions for retweeting a user’s latest few tweets and it works in an almost identical fashion. Print statements have been included for the sake of logging.

Connecting to your account

Authenticating is simply a matter of passing the correct API keys to tweepys OAuthHandler functions.

Once that’s done we can obtain that auth variable from It represents the API and the corresponding account we’ve logged into (called auth_acc below). This is accomplished as follows:

# Authentication
auth = tweepy.OAuthHandler(API_KEY,API_SECRET_KEY)
auth.set_access_token(API_ACCESS_TOKEN, API_ACCESS_SECRET)
auth_acc = tweepy.API(auth)

Simply, substitute the values in quotes with the API keys you saved when creating your developer account.

Once that’s done you can simply import mediumPosting and retweet files into the main file and use the corresponding function calls in your main method.

See below:

auth_acc.update_status posts the Title_Link string we created in


The contents of are fairly simple.

The file contains one line that uses crontab (a Linux utility to schedule jobs) to run the bot automatically at specific time intervals. After all, bots should be automatic (at least in principle).

Simply add the entry to crontab as follows to have the file running at specific time intervals:


My entry below is set to run every 10 minutes. It’s a little much, I know, but the example works:

*/10 * * * * python3 ./


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: