@ChessContestBot is a Mini App designed to offer a multiplayer chess gaming experience. Users have the option to choose their side and create a new game, after which they can share the provided game link with their opponent. When the opponent joins the game, the user will receive notifications from the bot regarding the game's progress. Every Telegram user with access to the game link can view the ongoing competition in real-time.
This project was created for Telegram Mini App Contest.
To facilitate local development or production builds, you must define environment variables. You can also utilize a .env
file located at the project's root directory:
REACT_APP_BOT_URI
URL to your Telegram Mini App without protocol. Example:t.me/ChessContestBot/game
WEBHOOK_DOMAIN
Domain where your Mini App deployed. Example:chessbot.kozlov.cloud
TELEGRAM_BOT_TOKEN
Token for your Telegram bot provided by @BotFatherDB_HOST
Host of PostgreSQL databaseDB_PORT
Port of PostgreSQL databaseDB_USER
User of PostgreSQL databaseDB_PASS
Password for PostgreSQL databaseDB_NAME
Name of PostgreSQL database
Example of .env
file:
REACT_APP_BOT_URI=t.me/ChessContestBot/game
WEBHOOK_DOMAIN=chessbot.kozlov.cloud
TELEGRAM_BOT_TOKEN=super:secret-bot-token
DB_HOST=localhost
DB_PORT=5432
DB_USER=chess
DB_PASS=chess
DB_NAME=chess
To build and run the Mini App server locally, you need to have the following installed:
- PostgreSQL database (version >= 14)
- Node.js (version >= 18)
Follow these steps for installation:
- In the project root directory, with your environment properly configured, run the following command to install project dependencies:
npm install
- Build the Mini App frontend by executing:
npm run build
- Start the development server on port
3000
with the following command:
npm run server
Database migrations will be automatically applied every time the server is started.
To expose your local server with a publicly available HTTPS endpoint, you can use either Cloudflare Tunnel or ngrok.
npm run build
Builds frontend for Mini App to folderbuild
npm run build-server
Create production server build to folderserver-build
npm run start
Starts development server with only frontendnpm run server
Starts development server with API, websocket, bot webhook register and frontend static files serving on port3000
npm run makemigrations
Create migration with database changesnpm run studio
Run server with Drizzle Studio to manage database
To deploy the compiled backend and frontend, you can use any server of your choice. Follow these common steps to set up a production server:
- Build the Mini App frontend by running
npm run build
. This will generate the frontend files in thebuild
folder. - Build the Mini App backend server by running
npm run build-server
. This will create theserver-build
directory. - To start the backend server, ensure you have the
node_modules
folder with all the required dependencies installed. Then, runnode server-build/index.js
(default port:3000
). You may consider using a server process manager likesupervisor
orpm2
for improved server management. Don't forget to configure the necessary environment variables. - By default, the backend server will serve static files from the
build
folder. However, it is recommended to use a dedicated web server likenginx
to handle static files and proxy API calls. You can find an example of an nginx configuration in the Helm ConfigMap here. - Ensure the security of your web server by implementing HTTPS. You can use services like Let's Encrypt or Cloudflare for this purpose.
- Configure your Bot and Mini App with @BotFather. For more information, refer to the official documentation.
- Share your bot with friends and enjoy playing chess!
Also we have working Dockerfile
with frontend and backend images.
@ChessContestBot is deployed in Kubernetes using werf.
You can find configuration in .helm
folder and werf.yaml
file.
- Set environment for Kubernetes config, like
WERF_KUBECONFIG_BASE64
. - Generate
WERF_SECRET_KEY
and configure secret values in.helm/secret-values.yaml
- Provide
REACT_APP_BOT_URI
env variable - Just run
werf converge
to deploy to Kubernetes cluster
This configuration uses Zalando Postgres Operator for database.
For more information, refer to the werf official documentation.
Chess logic: Chess.js library
Chess board: react-chessboard
Telegram Web App react bindins: react-telegram-web-app
Telegram bot server: Telegraf
Websockets: Socket.IO
Database connection and migrations: Drizzle ORM. Migrations placed in migrations
folder.
- Unit tests
- Translations to other languages
- List of your games
- Game timer
- History of game moves with possibility to go backward and forward
- Logging and tracing
Feel free to make PR
Copyright (c) 2023 Pavel Kozlov
MIT License