January 28, 2023

Telegram-bot for Subspace

In this guide, I will show you how you can create your own Telegram bot to manage and monitor your Subspace node.

The guide consists of 2 parts:

1) Creating a Telegram bot and getting API keys
2) Installing and configuring the bot on your node

1.1) Registering a bot

Send a message to https://t.me/BotFather in telegram

/newbot

Invent description and name(should MUST end with bot) for bot, examples:

abcde_bot or abcdetestBot

Save HTTP API token, generated by BotFather - it will be useful to us

1.2) Getting api_hash for telegram-api-server

When using the standard Telegram server, there are restrictions and timeouts after which the bot may stop responding.

In order for our bot to be stable and available, we will deploy our own telegram-api-server on our node.

Go and log in to the official apps section in the web version

Telegram: https://my.telegram.org

Go to API development tools

Fill in the fields:
App title: any text
Shortname: any text without spaces
Url: any link or IP
Platform: Desktop
Description: any description

Then press Create application.

In case of success (not always the first time, sometimes it gives "ERROR" without a description), we get something like this

Save your api_id и api_hash

This completes the registration of the bot and the application for it.

2.1) Installing the bot on the server

Clone the repo:

git clone https://github.com/mrShakedown/subspace-bot.git

mv ./subspace-bot/* ~/

Make service unit for bot:

nano - text editor in Linux

nano /etc/systemd/system/telegram-bot.service

write there:

[Unit]
Description=Telegram-bot
After=network.target
[Service]
Type=simple
ExecStart=/root/bot.sh
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

To save and exit the editor: CTRL+O, CTRL+X

make script for run bot:

nano /root/bot.sh

write there:

#!/bin/bash
python3 /root/bot.py

Grant right for execution:
chmod +x /root/bot.sh

Edit configuration:

nano /root/config.py

Where:

token = 'YOUR_TOKEN' - token of your bot

data_path = '/dev' - path to track disk usage and alerts

timer = 10 - frequency of checking the stats node and for alerts (in seconds)

admin_id = 123 - Your unique account ID, you can get it from the bot https://t.me/my_id_bot (a person with a different ID will not be able to use your bot)

2.2) Preparing the environment

Install the library for bot https://github.com/eternnoir/pyTelegramBotAPI

apt install python3-pip
pip install pyTelegramBotAPI

Install environment for telegram-api-server (official guide)

cd ~

git clone --recursive https://github.com/tdlib/telegram-bot-api.git

cd telegram-bot-api

mkdir build && cd build/

apt-get install libssl-dev build-essential checkinstall zlib1g-dev cmake openssl ccache gperf -y

Build

cmake -DCMAKE_BUILD_TYPE=Release ..

cmake --build . --target install

Create a service for telegram-api-server

nano /etc/systemd/system/telegram-api.service

Write there:

[Unit]
Description=telegram-api-server
After=network.target
[Service]
Type=simple
ExecStart=telegram-bot-api --api-id=123 --api-hash=123 -p 8080
[Install]
WantedBy=multi-user.target

where:
api-id and api_hash
from your app configuration

and p = free port for your choice (default 8080)

To save and exit from editor: CTRL+O, CTRL+X

Save and run

Update the created services for the bot and api-server

source ~/.bash_profile

let's run

systemctl enable telegram-api.service

systemctl enable telegram-bot.service

systemctl restart telegram-api.service telegram-bot.service

Result:

You have a bot running on the server, to which only you have access, with which you can monitor the state of the node manually (the list of commands in /help)

or automatically (alerts on click /on или /off )

The main list of commands can be obtained from the bot in the chat:
/help - will show actual commands

You can also add your commands directly to bot.py and restart the service

(systemctl restart telegram-bot.service)

As soon as I'm added different features - the guide will be updated (I really want to make shorter installation script)

P.s. I'm not a professional programmer - adequate criticism is not forbidden (fork it ;)