My First Twitter Bot

I love Twitter bots. My favorite used to be the @iaminigomontoya bot that would reply to anyone tweeting the word “inconceivable”. 🤣

I’m also a fan of the @LegoSpaceBot (no real surprise there), which tweets pictures of old LEGO Space sets from the 70’s, 80’s, and 90’s.

These are for the most part exactly the sets that I have been building for my LEGO Space Project for the past two years. So it turns out that I have built and taken photos of most of the sets that @LegoSpaceBot tweets about. I thought it would be fun to make a bot that sort of trolled the @LegoSpaceBot by quote retweeting (almost) every post with my photo of the built set together with a short comment.

After a bit of Googling I decided that I would write and host my bot at Glitch.com. I needed to create a new Twitter account (@LEGOSpaceBotBot) and to sign up for the developer program. Glitch has multiple Twitter bot templates to choose from and lets you get started quickly by “remixing” one of their templates. I started with a template that tweeted random photos, but I also looked at templates that responded to other accounts. I would need both: to be able to search for tweets and to be able to post images.

JavaScript is … interesting. After working in Swift for the past year and a half, it’s quite the change. JS developers: respect.

The basic bot idea was to look at tweets from @LegoSpaceBot, find those which mention specific sets (some posts are of catalog pages), parse out the set number, and check to see whether I have a photo of that set. If it finds a match, then quote retweet that post with the photo and a random message. If no match is found then quote retweet with a different random message.

To avoid having to do too much work, I want it to keep track of the most recent tweet that it has successfully processed. This is written to a simple text file. I also wanted to make sure it only responds to the most recent tweet even if it has been offline for a period of time. (I wanted to make sure it didn’t accidentally respond to @LegoSpaceBot’s entire backlog of 8k+ tweets…)

I started out with an array of 8 phrases that would be chosen randomly to accompany each post. After the same phrase was randomly chosen for the second and third posts, I realized that I needed something a bit more advanced than just simply random. Fortunately, the author of the @LegoSpaceBot already solved this problem and has a good discussion of the issue here. The basic gist is to split the array into two halves and shuffle through one half before shuffling through the other half. That guarantees a minimum distance of N/2 between any repeats. So for my array of 8 phrases, any given phrase would be at least 4 tweets apart. (Obviously I need more phrases.) I adapted my randomizing method from the code here. This solution relies on being able to seed the randomizer, which to my surprise JavaScript.Math does not handle by default! Fortunately there’s an answer for that too and you can add seed functionality by including seedrandom.js from here.

En fin, I hereby present the LEGO Space Build Bot.

1k swim

Tonight I swam 1 km for the first time. My Watch presented me with this award. We joined a pool in mid-November and have been trying to swim at least 3x a week. (The best part is the heated swim-out pool with jets and bubbles for after. Especially when it’s snowing.)