bot/setup.md
2026-04-20 22:31:45 +03:00

3 KiB

Setup

Requirements

  • Node.js 20+ or newer
  • npm
  • A Telegram bot token from @BotFather

Project Setup

  1. Install dependencies:
npm install
  1. Create .env from .env.example and fill in your bot token:
BOT_TOKEN=your_telegram_bot_token_here
BOT_API_ROOT=
  1. Start the bot:
npm start

What The Bot Does

  • Saves text messages to assets/request.txt by appending new lines
  • Saves photos, videos, voice messages, and documents to assets/
  • Replies with Ок for supported message types

Optional: Local Bot API Server For Files Larger Than 20 MB

Telegram's hosted Bot API cannot download files larger than 20 MB. To support larger files, run a local telegram-bot-api server and point the bot to it with BOT_API_ROOT.

Official references:

1. Get api_id and api_hash

Create them here:

2. Build The Local Bot API Server In ~/dev/lamda/bot_api

The commands below match the local layout already prepared on this machine.

mkdir -p ~/dev/lamda/bot_api

~/miniconda3/bin/conda create -y \
  -p ~/dev/lamda/bot_api/.buildenv \
  --override-channels \
  -c conda-forge \
  gperf openssl zlib cmake

git clone --recursive https://github.com/tdlib/telegram-bot-api.git ~/dev/lamda/bot_api/telegram-bot-api

git -C ~/dev/lamda/bot_api/telegram-bot-api submodule update --init --depth 1 td

mkdir -p ~/dev/lamda/bot_api/telegram-bot-api/build
mkdir -p ~/dev/lamda/bot_api/install

export PATH=~/dev/lamda/bot_api/.buildenv/bin:$PATH
export CMAKE_PREFIX_PATH=~/dev/lamda/bot_api/.buildenv

cmake -S ~/dev/lamda/bot_api/telegram-bot-api \
  -B ~/dev/lamda/bot_api/telegram-bot-api/build \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=~/dev/lamda/bot_api/install \
  -DOPENSSL_ROOT_DIR=~/dev/lamda/bot_api/.buildenv \
  -DZLIB_ROOT=~/dev/lamda/bot_api/.buildenv

cmake --build ~/dev/lamda/bot_api/telegram-bot-api/build -j"$(nproc)"
cmake --install ~/dev/lamda/bot_api/telegram-bot-api/build

3. Start The Local Bot API Server

mkdir -p ~/dev/lamda/bot_api/data
mkdir -p ~/dev/lamda/bot_api/tmp

~/dev/lamda/bot_api/install/bin/telegram-bot-api \
  --api-id <API_ID> \
  --api-hash <API_HASH> \
  --local \
  --http-port 8081 \
  --dir ~/dev/lamda/bot_api/data \
  --temp-dir ~/dev/lamda/bot_api/tmp

4. Switch The Bot To The Local Server

Before switching away from Telegram's hosted Bot API, log the bot out there once:

https://api.telegram.org/bot<BOT_TOKEN>/logOut

Then set this in .env:

BOT_TOKEN=your_telegram_bot_token_here
BOT_API_ROOT=http://127.0.0.1:8081

Restart the bot after that:

npm start

Notes

  • BOT_API_ROOT is optional. Leave it empty to use the default Telegram hosted Bot API.
  • The local Bot API server listens over HTTP by default.
  • If both the bot and the local Bot API server run on the same machine, 127.0.0.1:8081 is enough.