Creating a Twitter Maze Bot

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:

  1. Build everything in PHP to run locally.
  2. Schedule the tweets using the Buffer API.
  3. 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.

A dummy maze to test the image sizes

A dummy maze to test the maze dimensions.

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):

  1. Store the current timestamp
  2. Create a new timestamp about 12 hours in the future. Could be a couple of hours either way for some randomness.
  3. Check the new timestamp is posted in the hours I am awake.
  4. Store the new timestamp.
  5. Generate the tweet content and post it.
  6. 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.

Related Posts

28 Sep 2021

Creating Generative Art with PHP

These last few weeks I’ve been experimenting with Generative Art, using PHP. You can see the evolution of my latest series on Twitter. Generative Art is creating artworks through programming. Generative art has a few different names, Procedural art and...
05 Oct 2021

Randomness with PHP

When I was making my generative art I needed to generate a lot of random things. The simplest way to do that with PHP is to use the rand() function.The rand() function selects a random integer (whole number) between 0...
30 Jul 2023

Cracking the Code: Creating NinjaPuzzles

Ever since becoming a Dad, I have enjoyed creating paper-based games and puzzles for my son. I began with mazes, starting with hand-drawn ones and later moving on to computer-generated ones. As he got older, he became interested in Sudokus,...
01 Jun 2011

A Day in the Life of a Web Designer

I recently had someone email me and ask what a day in my life, or that of a web designer, was like. A slightly unusual request but I thought I would take up the challenge and document an average day.I...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
04 Nov 2016

Automating WordPress Development with Gulp, Bash, and PHP

When I wrote about the things I had learned from releasing 20 WordPress themes, I mentioned that I had automated as much as I could. One of the things I automated was the build and deployment process for my themes...