Making an online chess bot using the Lichess API

Perhaps some or even many of the readers wrote a chess bot that could play with the user. Today I will tell you how to make our bot available on a famous chess website lichess.org.

Step 1. Create an account.

It is very important that the account that we are going to turn into a bot has not yet played a single game of chess. Therefore, you will have to create a new account.

Those who are already familiar with the site’s interface can do this, and for those who have heard about it for the first time, I will show a tutorial.

Once you are on the main page of the website, you will need to click on the text in the upper right corner “LOGIN” and then click “register”:

image

In the window that opens, come up with a username, password and write your email address (it is important that it is real, because the site will send an email for verification), click all the checkboxes and click “register”:

image

After this, an email will be sent to the specified email with a link to confirm your account, follow the link and registration is successfully completed.

Step 2. Create a token.

After the account has been successfully created, go to the page /account/oauth/token and click on the blue “Create” button. You will receive a message with your token. You will need to copy it and save it somewhere safe, because the token name only shows once:

image

(By the way, I have already deleted the token in the screenshot, so my security is fine)

Next, all the procedures in the browser are completed, and all we have to do is assign the title of a bot to the account and launch the main program, which will, in fact, play.

Step 3. Assign the title of bot to the account.

To assign the title of a bot to an account (and without this the account will be banned for cheating), we need to send a request to the server, including a token. The following program does this:

import requests

api = "Bearer " + input("Введите токен: ")
r = requests.post("https://lichess.org/api/bot/account/upgrade", headers = {"Authorization" : api})
print(r.status_code, r.text)

After this procedure, looking at the account page on Lichessa, you will be able to see the purple inscription “HERE”:

image

Now all we have to do is run a program that will accept the game position and also send moves.

Final program

To avoid having to reinvent the wheel, site administrators posted on GitHub the necessary program.

You just need to download the necessary files from there, and also add your engine to the right place.

You also can:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *