One of the many allures of Twitter is that you can tweet at your favorite celebrity and (maybe) get a response. Still though, tweeting isn’t quite as intimate as trading text messages. So we thought it’d be fun to use Markov Chains, Programmable SMS, and Python to create a bot that impersonates your favorite Twitter personality.

We could use the code below to create an SMS chat bot that sounds like anyone with a Twitter account. But to show off it’s true potential, we need to someone with a distinct and recognizable tweeting style. Someone with a huge personality. Someone who has the best words.

Someone like Donald Trump.

Ever wish that you could debate Trump? Drop him a text at: 847-55-TRUMP (847-558-7867).

There are three steps to create this bot:

Download the tweets for a given user to create a corpus of text. Use the corpus to generate a sentence in the style of the Tweeter. Reply to a text message with that sentence.

To follow along, you’ll need Python, a Twitter account, and a free Twilio account.

Download All Tweets from a User

Before we get started, let’s give credit to Filip Hráček whose Automatic Donald Trump was the inspiration for this idea. Check out his post for an excellent explanation on how to implement Markov chains in Dart.

Markov chains begin with a corpus — a library of text to train your model. We’ll use a modified version of this tweet_dumper script to pull in down tweets from the Twitter API.

To get started, create and activate a new virtual environment: