Skip to content

Running Telegram CLI as Daemon

farus13 edited this page Dec 1, 2022 · 5 revisions

This is how Telegram run as daemon on debian-based. We will run it using sudo.

All commands are executed from telegram-cli's repository directory.

Add User telegramd

Go to https://desktop.telegram.org/ and download the installer - There's an "Installation" heading on the front page with a link to tsetup-x64.4.3.3.exe which is the thing you want

This telegramd user will run the telegram-daemon

sudo adduser telegramd

Add Script telegram-daemon to /etc/init.d

sudo cp telegram-daemon /etc/init.d/

Create Base Directory for telegram-daemon

This directory is place for binlogs and lua-scripts. It is also default directory for telegram-daemon user.

sudo mkdir /var/lib/telegram-daemon

The contents of /var/lib/telegram-daemon directory, will be:

  • binlog-achat.bin
  • binlog-z5.bin
  • config-achat
  • config-z5
  • downloads
  • script-achat.lua
  • script-z5.lua

Files config-* are auto generated by telegram-daemon, you don't need to edit them.

Files binlog-*.bin are binlogs. You have to authorize separately then copy the binlog file here. You can edit the scripts to use version without binlogs.

Files script-*.lua are lua scripts.

Create Binaries Directory

This directory is place for binaries (telegram-cli) and start/stop (start-telegram-daemon)

sudo mkdir /usr/share/telegram-daemon
sudo mkdir /usr/share/telegram-daemon/bin
sudo cp bin/telegram-cli /usr/share/telegram-daemon/bin/
sudo cp start-telegram-daemon /usr/share/telegram-daemon/bin

The contents of /usr/share/telegram-daemon/bin/ directory will be:

  • start-telegram-daemon
  • telegram-cli

Create Config Directory

sudo mkdir /etc/telegram-daemon

Create telegram-daemon.achat.conf

In folder /etc/telegram-daemon, create this file using sudo nano /etc/telegram-daemon/telegram-daemon.achat.conf command.

execute telegram-cli 
-d 
-vvvv 
-E 
-R 
-D 
-C 
-P 2391

This script will run daemon on port 2391. Feel free to change the port number.

Create telegram-daemon.z5.conf

Create Logs Directory

sudo mkdir /var/log/telegram-daemon

Create Server Key Directory

This is place for server's public key.

sudo mkdir /etc/telegram-cli
sudo cp server.pub /etc/telegram-cli/server.pub

Usage

server:~# /etc/init.d/telegram-daemon status
telegram-daemon-achat:  19901  0 Sep23 00:00:02 telegram-cli -vvvv -E -R -D -C -P 2391 -u telegramd -L /var/log/telegram-daemon/telegram-daemon-achat.log -c config-achat
telegram-daemon-z5:  failed (pid 19900)
server:~# /etc/init.d/telegram-daemon restart z5
start-stop-daemon: warning: failed to kill 19900: No such process
telegram-daemon-z5: stopped
restarting telegram-daemon-z5...
server:~# /etc/init.d/telegram-daemon status    
telegram-daemon-achat:  19901  0 Sep23 00:00:02 telegram-cli -vvvv -E -R -D -C -P 2391 -u telegramd -L /var/log/telegram-daemon/telegram-daemon-achat.log -c config-achat
telegram-daemon-z5:  22222 15 14:22 00:00:00 telegram-cli -vvvv -E -R -D -C -P 2392 -u telegramd -L /var/log/telegram-daemon/telegram-daemon-z5.log -c config-z5

By using listening to tcp port, we can send commands to daemon via netcat or telnet. Example using netcat:

nc localhost 2391
dialog_list
msg vysheng kudos