User Tools

Site Tools


linux:telegram

Installing telegram to send notification messages

Intro

If you look on internet, you will found thousands of recipes like this one: install telegram in order to send notification messages from one computer directly to your phone.

My goal with this recipe is to create a telegram_msg command that just send a text message, nothing more.

Although it might seem a simple goal, you will discover that it is not a simple one: the Telegram command line application doesn't have a “send message” combination of arguments to do so. Moreover, a combination of pipes –to simulate a writing from the console– doesn't achieve the desired result because the telegram-cli program doesn't end: it keeps cyling over and over reading nothing from the pipe.

To put things worst, the first command you have to issue in ''telegram-cli'' is contact_list. It's a known error, at least on June, 2015.

Said that, the only approximation to my goal I've seen so far today is:

  • Run telegram-cli as a daemon, and when it starts, issue a contact_list command the first time
  • After that, send the command through netcat (nc) command

Step 1: install necesary packages

$ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev 
$ sudo apt-get install libevent-1.4-2 libevent1-dev
$ sudo apt-get install libpython3.4 libpython3.4-dev 
$ sudo apt-get install libjansson4 libjansson-dev

Step 2: download the project

It is not compulsory to have git installed in the destination machine. You can have git in another machine and use it to download the project:

git clone https://github.com/vysheng/tg.git

The project will appear in a directory called “tg”.

Afterwards you can move the project to the destination machine.

<WRAP center round important 60%> Up to date (01/07/2015), this current version does not work. I've downloaded version 1.0.5.1 instead, which works correctly. </WRAP>

Step 3: configure and build the project

Nothing special here. Go to the directory where you have downloaded the project and:

$ ./configure
$ make 

Step 4: a basic test

Make a simple confirmation that the program is working. You can skip this test because we will have to create another user –telegramd– to run the service and we will have to do it again.

The program is called telegram-cli and it's under the bin directory. Go there, and execute:

$ ./telegram-cli -k ../tg-server.pub

Something similar to this will appear:

$:~/tg-1.0.5.1/bin$ ./telegram-cli ../tg-server.pub -k ../tg-server.pub 
Telegram-cli version 1.0.5, Copyright (C) 2013-2014 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telephone number (with '+' sign): 

You have to provide the telephone number of this account –normally your telephone number– but with your national prefix.

Then the sms will be requested, so it is advisable to have this phone near when doing this.

You can issue a contact_list command to check thet that this client is working:

> contact_list
....
....

Step 5: install the client

I've opted for manually install the client. I've copied the telegram-cli program under /usr/local/share/telegram and the public key of the server under /etc, with very restricted permissions for reading.

# mkdir /usr/local/share/telegram
# cp telegram-cli /usr/local/share/telegram

I've also created a directory called /etc/telegram that will hold all the configuration and the public key:

</code> # mkdir /etc/telegram # chmod u=rwx,g=rx,o=rx /etc/telegram # cp tg-server.pub /etc/telegram # chmod g=r,o=r,u=r /etc/telegram/tg-server.pub </code>

Step 6: create the system user telegramd

In order to run the telegram-daemon as root, you will need to create the user telegramd. I've opted to create this user as a system user:

# useradd -M --user-group --system --shell=/bin/false telegramd

Impersonate this user and run the telegram-cli program again, to create a proper configuration file:

telegramd@:~$ /usr/local/share/telegram/telegram-cli -k /etc/telegram/tg-server.pub 
Telegram-cli version 1.0.5, Copyright (C) 2013-2014 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telephone number (with '+' sign): 

Step 7: create a daemon and install it

Telegram-cli has a bug that provokes that, unless contact_list is the first command issued, no message can be sent through the program. Moreover, there is no way to create a program that just sends a message and ends: the program waits forever even if you run through a pipe. A more simple version to make automatic notifications from a machine would be desirable.

Let's go for it. The code for the daemon is this (customize the adequate variables):

#!/bin/bash
#
# telegram.sh  - to start / stop a telegram server 
#
#

### BEGIN INIT INFO
# Provides:		telegram
# Required-Start:	$local_fs $network $syslog
# Required-Stop:	$local_fs $network $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Telegram server
### END INIT INFO

TELEGRAM_BIN="/usr/local/share/telegram/telegram-cli"
TELEGRAM_PORT=1234
TELEGRAM_ARGS="-p /home/telegramd/.telegram-cli -k /etc/telegram/tg-server.pub -d -P $TELEGRAM_PORT"
PID_FILE=/var/run/telegram.pidfile

umask 022

test -f "$TELEGRAM_BIN" || exit 0 


case $1 in
start)
    echo "Starting telegram server"
    $TELEGRAM_BIN $TELEGRAM_ARGS &
    echo $! > "$PID_FILE"
    # issue the command "contact_list" because it's needed
    # for sending messages
    sleep 5
    echo "contact_list" | nc 127.0.0.1 $TELEGRAM_PORT
    echo "done"    
    ;;
stop)
    echo "Stopping telegram server"
    # first make a gentle kill 
    kill $(cat "$PID_FILE")
    # then sleep and make a hard kill 
    sleep 4s
    kill -9 $(cat "$PID_FILE") 2> /dev/null
    rm "$PID_FILE"
    echo "done"
    ;;
reload)
    $0 stop
    $0 start
    ;;
status)
    if kill -0 $(cat "$PID_FILE") ; then 
    	echo "Telegram process is running"
    else
    	echo "Telegram process has stopped"
    fi
    ;;
*)
    echo "Usage: $0 start | stop | reload | status"
    ;;
esac 

exit 0 

Install it under /etc/init.d (in ubuntu):

sudo mv telegram-daemon /etc/init.d
sudo chown root:root /etc/init.d/telegram-daemon

Step 8: install the telegram-msg program

After that I've created a telegram-msg program to just send a message through telegram. The program is pretty simple:

#!/bin/bash
#
# telegram-msg - sends a telegram message 
#
#


echo "msg $1 $2" | nc 127.0.0.1 1234

Step 8: first test

$ sudo /etc/init.d/telegram-daemon start 
$ telegram-msg somebody "some message you want to send"
linux/telegram.txt · Last modified: 2022/12/02 21:02 by 127.0.0.1