Tweet Simulator

Twitter bot which takes tweets from popular hashtags as input for generating its own tweets.

Published on Apr 02, 2022

Reading time: 2 minutes.


Built with

Tweepy
NumPy
Markovify

The most annoying of my creations

How does it work?

This python script uses Tweepy to interface with the Twitter API in order to browse popular trends. It first picks a random trend from the list of popular trends in United States. Then, it attempts to grab a certain large number of tweets associated with this trend (it is important to note that trends are not necessarily hashtags) if there are less than 500 tweets associated with the trend it will just grab all of them.

62
63
64
65
66
67
68
69
70
71
72
73
74
print("Scanning tweets...")
search = [status for status in tweepy.Cursor(api.search, q=query, tweet_mode="extended", wait_on_rate_limit=True).items(trendVol)]
dataFile = open("tweets.txt", "w", encoding="utf-8")

for result in search:
    if hasattr(result, "retweeted_status"):
        dataFile.write((result.retweeted_status.full_text).replace("\n", "..."))
        dataFile.write("\n")
    else:
        dataFile.write((result.full_text).replace("\n", "..."))
        dataFile.write("\n")

dataFile.close()

After some pre-processing the bot uses Markov Chains to determine the most “likely” new tweet in this trend. This method cannot technically be called machine learning, as there is no training loop, it is simply using probability to determine the next series of characters.

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
print("Generating tweet...")
model = markovify.NewlineText(text, well_formed=False)

chain = ""

for i in range(numSentences):
    try:
        chain += (model.make_short_sentence(maxSentenceChars, tries=1000)).replace("...", "\n")
        if (chain[-1] != "." and chain[-1] != "!" and chain[-1] != "?"):
            chain += "."
        chain += " "
    except:
        print("NoneType sentence")
        break

tweet = chain.replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">")
api.update_status(status=(hashtag + tweet))
print("Tweet sent!")

Why? Why do this?

This project was inspired by Subreddit Simulator. When I saw some of the posts and comments being made by these bots, I was surprised by how well they were able to simulate the culture of their respective subreddits. I read up on their methods and decided I wanted to try to replicate it with Twitter, hoping it would be able to represent different communities with different cultures.

I didn’t think it would be spot-on, but I thought maybe it would help make some funny or interesting observations on different groups within Twitter.

The Results

Admittedly, the bot is pretty nonsensical. Sometimes it makes long winded tweets with no point and sometimes it is literally just a blank tweet. In retrospect the most important part of a markov chain is that you give it a lot of data to work with, which becomes a problem when you are changing the dataset everytime it runs. Consistency is not possible, sometimes it may have plenty of data for processing and other times it may have next to nothing.

If I were to revisit this project I would look into better ways of gathering data. But, I do appreciate the practice it gave me in working with APIs and it was interesting to learn all of the ways Twitter lets you automate. After this project I had considered making another bot just to help me with some tasks on my main Twitter account, but then I stopped using Twitter as much so there wasn’t a lot of reason for me to do it. 😬