How I created a bot that tweets the correct Wordle word daily

Wordle is intriguing, I know. But what was more intriguing to me was how this web app worked. So, I decided to peek through the code, and eventually, I decided to build a Twitter bot that tweets the correct word daily.

Finding the correct Wordle word

First things first. How did I find the right Wordle word for each day?

Well, before I traversed through the code, I expected the app to use a frontend app and a backend app. I thought the frontend app interacted with the backend to check the validity of my guesses. But when I traced the network requests while I played the game, I couldn’t find any API calls to a backend.

To investigate further, I examined the local storage to see if the app was storing the game data there. And I wasn’t wrong. I could find the entire state of the game and my progress stored against a key called “nyt-wordle-state”.

Wordle Local Storage
The state of the application stored in the Local Storage

Besides, as you can see, the solution for that day was also right there in the local storage. But how did I find out the answer for each day? To begin with, I was curious to know how the local storage was being populated. Surely, it can’t be from API calls for I couldn’t find any API calls. So, did the developer hard code the right words?

Are the correct words hard-coded?

So, I finally decided to go through the code. Since this is a web app, the JavaScript code has been expectedly mangled, or uglified as some of us like to call it, rendering the code difficult to read. Hence, I decided to search for the answer in the code, and I found the motherlode!

The answers are hard-coded

The developer had indeed hard-coded both the correct words and the list of accepted words. I found two arrays, one containing the correct words and the other containing the accepted words. The app was launched on the 19th of May 2021. The code subtracts the current date from this date and uses the output value as an index to fetch the right word from the array. For instance, this article was written on the 27th of February 2022. So, the difference is 253 days. This means that the correct answer is the 253rd element in the array and that is none other than “chant”.

Tweeting the correct Wordle word

I got a cheeky idea here. Why not create a bot that would find the right answer automatically? And why not tweet it to the world? So, I copied the array of correct words and wrote a simple Python script to find the correct answer.

However, I also wanted to tweet it. In order to do that I needed a Twitter developer account. I tried creating one and the experience was far from smooth. I was asked a million questions from why I wanted to create a developer account to what I was planning to do with it before I was allowed to create an account. But even after going through this tiring rigmarole, Twitter wasn’t done yet. They sent me an email with similar questions and expected an elaborate answer. I wasn’t ready to write an essay just to create a developer account and the fact that even after replying to that email I will have to wait for a few days to have my account verified dissuaded me from going down that route.

Using Zapier

But I wasn’t prepared to throw in the towel just yet. Definitely, there must be another way—an alternative or a hack. Enter Zapier—an online tool that allows workflow automation. It allowed me to send out tweets following a trigger.

First, I had to create a trigger. A trigger, as the name implies, kick starts the flow. I chose catch hook as the trigger. This gives us a webhook URL. By sending an HTTP request to this hook, we will be able to trigger the workflow. I planned to send the correct word as a query parameter called “answer” along with the request to the webhook URL. So, I entered “answer” into the “Pick off a Child Key” textbox.

Then, I created the second step, chose the Twitter app from the list of apps, and selected the “Create Tweet” action. Next, I authorized Zapier to access my Twitter account and specified the content of the tweet. I selected “Querystring Answer” from the autocomplete dropdown and added the hashtag “todaysWordle” as the content of the tweet. Finally, I switched on the “Zap” to activate the workflow.

The completed Zapier Workflow

After that, I modified my Python script to send the answer as a query parameter in the get request to the webhook URL. Now, I wanted to find a way to automatically run this script daily. And GitHub Actions came to my rescue.

Executing the script daily

Consequently, I set up a GitHub workflow that runs daily at 10:00 A.M. UTC. The rationale behind this time is that this is the earliest UTC time a new day begins in the world. I added the Zapier webhook URL to GitHub secrets and passed it as an argument into the Python script. This allowed me to make sure the webhook URL is not compromised.

My final Python script is as follows:

The GitHub workflow is as follows:

Once completed, this bot started discovering the right Wordle word and tweeting it out to the world daily. You can follow this Twitter bot at @todaywordle. You can find the repository with the Python script and the GitHub workflow here.

Leave a Reply

placeholder="comment">