User Tools

Site Tools


linux:telegram

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:telegram [2015/07/01 20:47] – [Step 2: download the project] rlunarolinux:telegram [2022/12/02 22:02] (current) – external edit 127.0.0.1
Line 21: Line 21:
 $ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev  $ 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 libevent-1.4-2 libevent1-dev
 +$ sudo apt-get install libpython3.4 libpython3.4-dev 
 +$ sudo apt-get install libjansson4 libjansson-dev
 </code> </code>
  
Line 40: Line 42:
 which works correctly.  which works correctly. 
 </WRAP> </WRAP>
 +
 +===== Step 3: configure and build the project =====
 +
 +Nothing special here. Go to the directory where you have downloaded the project and:
 +
 +<code>
 +$ ./configure
 +$ make 
 +</code>
 +
 +===== 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: 
 +
 +<code>
 +$ ./telegram-cli -k ../tg-server.pub
 +</code>
 +
 +Something similar to this will appear: 
 +
 +<code>
 +$:~/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): 
 +</code>
 +
 +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:
 +
 +<code>
 +> contact_list
 +....
 +....
 +</code>
 +
 +===== 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. 
 +
 +<code>
 +# mkdir /usr/local/share/telegram
 +# cp telegram-cli /usr/local/share/telegram
 +</code>
 +
 +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: 
 +
 +<code>
 +# useradd -M --user-group --system --shell=/bin/false telegramd
 +</code>
 +
 +
 +Impersonate this user and run the telegram-cli program again, to create a proper configuration file: 
 +
 +<code>
 +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): 
 +
 +</code>
 +
 +
 +
 +===== 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):
 +
 +
 +<code>
 +#!/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 
 +
 +
 +</code>
 +
 +
 +Install it under ''/etc/init.d'' (in ubuntu): 
 +
 +<code>
 +sudo mv telegram-daemon /etc/init.d
 +sudo chown root:root /etc/init.d/telegram-daemon
 +</code>
 +
 +
 +===== 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: 
 +
 +<code>
 +#!/bin/bash
 +#
 +# telegram-msg - sends a telegram message 
 +#
 +#
 +
 +
 +echo "msg $1 $2" | nc 127.0.0.1 1234
 +
 +</code>
 +
 +
 +===== Step 8: first test =====
 +
 +<code>
 +$ sudo /etc/init.d/telegram-daemon start 
 +$ telegram-msg somebody "some message you want to send"
 +</code>
 +
  
  
linux/telegram.1435776477.txt.gz · Last modified: 2022/12/02 22:02 (external edit)