SMS Chess bot with Twilio, Python, and Integromat.

Carter McKay
5 min readAug 9, 2021

My phone doesn’t have the ability to install apps, so as a way around that, I designed a way to leverage SMS to play chess against a Python bot.

How this “App” works is whenever I text my bot’s number with certain chess commands it will respond with screenshots of the current state of the chess board including my move and the bot’s response. Example:

The following is a step-by-step tutorial of how I made this so called “app”.

Twilio Part 1:

The first step in the process is to create a Twilio account. To do this, go to https://www.twilio.com/try-twilio and enter your information. Then login and you’ll see a screen like this:

Navigate to where the number symbol, where the red arrow is pointing and click on it.

After that go to “buy a number” and then pick one of those numbers to buy, you can sort by area codes if you want a number that will be similar to yours.

Integromat:

Once you have bought a number we will go over to Integromat and signup for an account if you don’t already have one. Once signed in go to “Scenarios” and click on “Create a new scenario”:

The Scenario that we will be making will look like this:

To explain it briefly: when the Webhook gets triggered, it will send the request to the router which will check if the body of the text starts with “chess” if it does, then it will send the message to the first text parser.

Below are pictures of the setup for the router and the first text parser.

The text parser replaces the command “chess” with python3 chess-game.py. Then the output gets sent to the SSH module which SSH’s into my cloud machine and executes the parsed text which looks like “python3 chess-game.py d2d4” d2d4 being the move we want to make. The second text parser changes HTML to text:

And Finally, the output of that second text parser sends a message back to the person who sent it with a Twilio module:

The Phone number in this case is the number we bought a couple of minutes ago, and the media is the output of the second text parser module. I found that without the second text parser, the “media url” would fail to send.

You will have to setup the SSH module by yourself because every computer is different and will require unique setup.

Twilio Part 2:

Now head back over to Twilio and setup a flow by navigating to the icon, where the red arrow in the picture is pointing and click studio.

Then click the little plus button near the middle of the screen.

Set up your flow to look like the picture below.

When an incoming message hits the Twilio phone number, it will send a POST hook to our Integromat trigger and if it is successful it will respond “Incoming”, otherwise it will respond with “hook failed”. (The Incoming and hook failed messages aren’t important and are only useful for making sure everything is working correctly) The POST request looks like this:

It sends the body of the text and who it’s from. Copy your own request URL into the request URL box. You can find this in the Integromat Webhook trigger.

Now go back to “Phone Numbers” in Twilio and click on the phone number that you bought. Scroll all the way down until you see this.

Make sure that “A MESSAGE COMES IN” is set to “Studio Flow” and the title of your first flow.

Integromat Part 2:

This will be an extension of the Scenario we just built and will reset the chess board when ever you send “Reset chess”.

First the router has the chess-reset filter on it, saying that if the body of the text starts with “Reset chess” then run the following modules.

The text parser runs this:

And then send the output to the SSH module:

Then finally it will send the text message using the Twilio module including the screenshot of the reset chessboard.

Actual Code:

Follow the instruction in the code for setting up.

That’s It

If anyone has any suggestion about how to make improvements or anything then please let me know. One thing I will say though is that I purposefully built this system with a lot of different tools. I know that I can make it more efficient by just using the Twilio API and Python but for this project I wanted the experience of playing with Integromat and Twilio flows.

--

--

Carter McKay

Full-Stack Develop. Data and Web Dev Engineer. Problem Solver. Terrible at Graphic Design. Give me a wireframe and I’ll build it.