I made a Twitter bot!
Disclaimer: This is not a how-to, it’s just a description of my bot-creating adventure, although I do link to some of the tutorials and resources I found helpful.
I had been looking for a new Python project and a Twitter bot seemed like a good opportunity to write some code, do some problem-solving and end up with a fun ‘thing’ to share with people.
After reading this article about literary bots I decided to make a bot that tweets lines from a literary text.
I had recently watched the film The Green Knight, based on the Middle English poem in which Sir Gawain is challenged to a beheading game by the mysterious and otherworldly knight, and decided it would make a good corpus.
I found a lovely version of the poem on Project Gutenberg – ‘Gawayne and the Green Knight: A Fairy Tale’ by Charlton Miner Lewis from 1903 – and decided to create a dedicated account to tweet sequential rhyming couplets once a day.
I thought I would find a tutorial online, spend an afternoon on it and be on my way, but it wasn’t quite that easy!
This beginners’ guide to building a bot got me started. It provided an accessible introduction to some basic things to do with a bot and the Tweepy package for Python that connects to the Twitter API.
But, like a lot of Twitter bot tutorials, it focuses on bots that generate tweets at random from a corpus. It also doesn’t cover tweeting over a long period of time.
The first big hurdle was connecting my Twitter developer privileges with a new bot account as users are restricted to having one developer account. I’m not the first person to have this problem and ended up trying multiple different solutions to no avail until I found a successful workaround in this helpful blog post about negotiating authentication credentials.
3. Tweeting sequential lines
Most of the twitter bot tutorials online are designed to generate random tweets so I was on my own for the tweet-generating part of the code. I wanted to tweet sequential lines and keep track of the last line tweeted. I ended up having the script write the last line tweeted to an external file and, to generate the next tweet, reading the updated line number from that external file. Simple but effective!
The final challenge I encountered was scheduling the bot to tweet regularly without keeping my computer switched on all the time or using an external server. I decided that using the Windows Task Scheduler to tweet every day at a time my laptop is usually switched on was the simplest solution and this handy dandy tutorial on Windows Task Scheduler made that possible.
5. A Word on Ethics
Bots are allowed on Twitter (currently) but there are Terms of Service and codes of best practice to follow, such as disclosing whether an account is a bot, not spamming unsuspecting Twitter users, etc.
The holy grail (i.e. a functional bot):
Thanks to all of the resources available online and a lot of googling, and trial-and-error on my part, the Green Knight Bot is now live on Twitter and has been tweeting for a few days, just in time for the poem’s Christmas setting.
Please give it a follow if you feel like it!