I recently had the idea to make a Twitter Emoji Maze bot, so I tweeted my idea and it was suggested I write a tutorial about how I made it. So I’m going to try to write the article as I work on it.
I’ve not made a proper Twitter bot before, but I have a small amount of experience with automating Tweets so I will reuse the things I have used before.
Before I begin
I work on a lot of little projects like this and often they go nowhere. As such I will spend approximately no time caring about coding standards, or best practices. Since this is just something for fun, that only I will use, I will bodge it together in whatever way works.
My theory is that I can always tidy it up later if the project becomes popular or I want to work on it more. This is not a commercial project or something I will post on Github; so there’s no chance anyone will see what I am making.
Getting Started
To simplify development I am going to use systems I have used before. So, I will:
- Build everything in PHP to run locally.
- Schedule the tweets using the Buffer API.
- Use open source code from Github wherever possible.
Scheduling the Tweets
I already have a PHP API setup for sending Tweets to Buffer, which allows me to easily schedule them and saves a lot of time. I use this API to automate some of my Tweeting, such as sending messages when I publish theme updates.
For the Buffer API I use this PHP class; it’s 9 years old but it still works.
Update: I’ve just found out that the Buffer API was deprecated in 2019. It still works for existing projects, but new projects can’t be made. I’m now wondering if I can update this to work with the official twitter API. Would be nice to not pay Buffer since I only use it for these automated types of Tweets.
Creating the Maze
Rather than write my own maze code I googled for PHP Maze Generator and found a handful online. The first Google result is also the one I decided to use (it’s also 9 years old).
The code was no good on it’s own, I had to tweak it a bit. I wanted to use emojis for the walls and paths, and later decided to add a person at the start, and a building/ vehicle at the end of the maze. I also decided to create a list of different icons to use for each element so I added a list for each of those as well.
To decide what size to make I made a quick layout on Twitter to see what space I had. I decided on 11×11, which actually isn’t that big. It’s not going to be a challenge to finish them ?
You can see the modified class here. As I mentioned above, the code is not pretty; but I don’t really care. It works and I probably won’t look at it again ????.
Note: Every maze is totally random. To emphasize this I have added a number to the front of the tweet before the maze. Don’t tell anyone but it’s literally a random number. It has no relevance to anything ?
Scheduling
I want to tweet the mazes out once, maybe twice a day. I’ll do this by preloading a bunch of tweets on to Buffer. Potentially I could stick this all online and set up a cron job, but that sounds like more effort than it’s worth so I’m doing it locally.
To do this I (pseudo code):
- Store the current timestamp
- Create a new timestamp about 12 hours in the future. Could be a couple of hours either way for some randomness.
- Check the new timestamp is posted in the hours I am awake.
- Store the new timestamp.
- Generate the tweet content and post it.
- Repeat.
I am posting 30 tweets at a time, so I have to run this script once a month. I’m always on my computer so I can do this easily.
I already had the timestamp code from another side project so I tweaked it to make it more reusable.
The code below shows an example of scheduling a tweet with the buffer API I mentioned previously.
Future
It’s mostly done, a few hours worth of hacking about and I have a little maze script that tweets out mazes once or twice a day.
I’m wondering if I could setup a local cron job, maybe an Automator action, to post these without involving me.
I may also add some additional styles, or randomize the maze dimensions, for some variety.
Since making my maze bot I have made a new website that has a whole collection of downloadable mazes that you can print and solve for free.
Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).
Link to this page
Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.