gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Can you write a minimalist script using TypeScript that simulates the Lichess bot server…
- User
Can you write a minimalist script using TypeScript that simulates the Lichess bot server API.
- Assistant
Certainly! Below is a minimalist TypeScript script that simulates the core of the Lichess Bot API. It uses Express to create an HTTP server that responds to the basic endpoints the Lichess Bot API expects:
/api/accountto get bot info/api/stream/eventto stream real-time events (using Server-Sent Events)/api/board/game/stream/{gameId}to stream game state/events (also SSE)/api/bot/game/{gameId}/move/{move}to receive move commands
This is a minimal skeleton, not a full bot logic implementation — it just shows how you might implement the API methods and SSE streams.
Setup Instructions
- Initialize a new npm project and add dependencies:
npm init -y npm install express npm install -D typescript @types/express ts-node- Create a
tsconfig.json:
{ "compilerOptions": { "target": "ES2018", "module": "commonjs", "strict": true, "esModuleInterop": true, "outDir": "./dist" } }-
Create a file
bot-server.tsand use the code below. -
Run with
npx ts-node bot-server.ts
minimalist Lichess Bot API simulation (
bot-server.ts):import express from 'express'; import cors from 'cors'; const app = express(); app.use(express.json()); app.use(cors()); const PORT = 3000; // Some dummy bot account info const botAccount = { id: "minibot", username: "minibot", title: "bot", url: "https://lichess.org/@/minibot", online: true, playing: 0, patron: false, engine: true, }; // Endpoint: GET /api/account app.get('/api/account', (req, res) => { res.json(botAccount); }); // SSE helper function sseHeaders(res: express.Response) { res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); res.flushHeaders(); } // Endpoint: GET /api/stream/event // Sends event stream of incoming challenges and game starts app.get('/api/stream/event', (req, res) => { sseHeaders(res); // Send a simple hello event immediately res.write(`event: hello\n`); res.write(`data: {"type":"hello","version":"1.0","auth":{"token":"dummy"}}\n\n`); // Simulate sending a challenge after 5 seconds const challengeTimeout = setTimeout(() => { const challenge = { type: "challenge", id: "challenge1", challenge: { id: "challenge1", variant: { key: "standard", name: "Standard" }, timeControl: { type: "clock", limit: 300, increment: 0 }, rated: false, challenger: { id: "opponent", name: "opponent", rating: 1500 }, } }; res.write(`event: challenge\n`); res.write(`data: ${JSON.stringify(challenge)}\n\n`); }, 5000); // Simulate sending a game start after 10 seconds const gameStartTimeout = setTimeout(() => { const gameStart = { type: "gameStart", game: { id: "game1", } }; res.write(`event: gameStart\n`); res.write(`data: ${JSON.stringify(gameStart)}\n\n`); }, 10000); // Keep connection open indefinitely (or until client disconnect) req.on('close', () => { clearTimeout(challengeTimeout); clearTimeout(gameStartTimeout); res.end(); }); }); // Endpoint: GET /api/board/game/stream/:gameId // Sends game state events (SSE) app.get('/api/board/game/stream/:gameId', (req, res) => { sseHeaders(res); const { gameId } = req.params; // Send initial game state (dummy) const initialState = { type: "gameState", moves: "", wtime: 300000, btime: 300000, winc: 0, binc: 0, status: "started", turn: "white", chess960: false, }; res.write(`event: gameState\n`); res.write(`data: ${JSON.stringify(initialState)}\n\n`); // Simulate opponent making moves every 5 seconds const moves = ["e2e4", "e7e5", "g1f3"]; let moveIndex = 0; const moveInterval = setInterval(() => { if (moveIndex >= moves.length) { // End game const gameFinish = { type: "gameState", moves: moves.join(" "), status: "mate", winner: "white", status: "mate", }; res.write(`event: gameState\n`); res.write(`data: ${JSON.stringify(gameFinish)}\n\n`); clearInterval(moveInterval); res.end(); return; } moveIndex++; const moveState = { type: "gameState", moves: moves.slice(0, moveIndex).join(" "), wtime: 300000 - moveIndex * 10000, btime: 300000 - moveIndex * 10000, turn: moveIndex % 2 === 0 ? "white" : "black", status: "started", }; res.write(`event: gameState\n`); res.write(`data: ${JSON.stringify(moveState)}\n\n`); }, 5000); req.on('close', () => { clearInterval(moveInterval); res.end(); }); }); // Endpoint: POST /api/bot/game/:gameId/move/:move // Receives a bot move command app.post('/api/bot/game/:gameId/move/:move', (req, res) => { const { gameId, move } = req.params; console.log(`Received move for game ${gameId}: ${move}`); // Normally, you'd validate and process the move here res.status(200).send('ok'); }); app.listen(PORT, () => { console.log(`Minimal Lichess bot server listening on http://localhost:${PORT}`); });
What does it do?
- Responds to GET
/api/accountwith bot info. - Opens an SSE
/api/stream/eventthat sends a hello event, then a challenge event after 5s, and a gameStart event after 10s. - Opens an SSE game stream
/api/board/game/stream/:gameIdthat sends initial state then sample moves every 5s and ends the game. - Accepts bot move submissions via POST
/api/bot/game/:gameId/move/:move
This can stand in for the Lichess API endpoints when testing your bot code locally, or for experimentation.
If you'd like, I can help you extend it with more detailed game logic or challenge handling!