Teleward: a CAPTCHA bot for Telegram in Clojure + GraalVM

(This is a copy of the readme file from the repository.)

Teleward is a Telegram captcha bot written in Clojure and compiled with GraalVM native image. Runs on bare Linux/MacOS with no requirements. Fast and robust.

Why

Telegram chats suffer from spammers who are pretty smart nowadays. They don’t use bots; instead, they register ordinary accounts and automate them with Selenium + web-version of Telegram. Personally, I found Shieldy and other bots useless when dealing with such kind of spammers. This project aims the goal to finish that mess.

Another reason I opened Teleward for is to try my skills in developing Clojure applications with GraalVM. Binary applications are nice: they are fast, and they don’t need installing JDK. At the same time, they’re are still Clojure: REPL is here, and that’s amazing.

Features

Algorithm

The bot listens for all the messages in a group. Once a new pack of messages arrives, the bot applies the following procedure to each message:

Please note: the bot processes only messages no older than two minutes from now. In other words, the bot is interested in what is happening now (with a slight delay), but not in the far past. This is to prevent a sutuation what a bot had been inactive and then has started to consume messages. Without this condition, it will send captcha to chat members who have already joined and confuse them.

Java version

To make a Jar artefact, run:

make uberjar

The uberjar target calls lein uberjar and also injects the VERSION file into it. The output file is ./target/teleward.jar.

Binary version, Linux

Linux version is built inside a Docker image, namely the ghcr.io/graalvm/graalvm-ce one with native-image extension preinstalled. Run the following command:

make docker-build

The output binary file appears at ./target/teleward.

Binary version, MacOS

gu install native-image
make

Setting Up Your Bot

teleward -t <telegram-token> -l debug

If everything is fine, the bot will start consuming messages and print them in console.

Configuration

See the version with -v, and help with -h. The bot takes into account plenty of settings, yet not all of them are available for configuration for now. Below, we name the most important parameters you will need.

Example:

./target/teleward -t <...> -l debug \
  --lang=en --telegram.offset-file=mybot.offset \
  --captcha.style=normal

For the rest of the config, see the src/teleward/config.clj file.

Deploying on bare Ubuntu

sudo useradd -s /bin/bash -d /home/ivan/ -m -G sudo ivan
sudo passwd ivan
mkdir /home/ivan/teleward
scp ./target/teleward ivan@hostname:/home/ivan/teleward/
sudo mcedit /etc/systemd/system/teleward.service
[Unit]
Description = Teleward bot
After = network.target

[Service]
Type = simple
Restart = always
RestartSec = 1
User = ivan
WorkingDirectory = /home/ivan/teleward/
ExecStart = /home/ivan/teleward/teleward -l debug
Environment = TELEGRAM_TOKEN=xxxxxxxxxxxxxx

[Install]
WantedBy = multi-user.target
sudo systemctl enable teleward
sudo systemctl stop teleward
sudo systemctl start teleward
sudo systemctl status teleward

For Jar, the config file would be almost the same except the ExecStart section. There, you specify something like java -jar teleward.jar ....

Health check

The bot accepts the /health command which it replies to “OK”.

Further work

© 2022 Ivan Grishaev