telegram_logo

Set telegram-cli as daemon in debian based distro

Create a bash script in /etc/init.d with name telegramd

/etc/init.d/telegramd

#! /bin/sh
### BEGIN INIT INFO
# Provides:          telegram
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Commandline interface for Telegram
# Description:       Telegram-cli is a (unofficial) cli version of Telegram to chat from your console.
#                    This is an init script do make it a daemon.
#                    When used as daemon in conjuction  with LUA (scripting) you can use it to send your system
#                    commands to execute via other Telegram apps (PC - Phone - Web or other) while not
#                    logged in to the system.
#                    See: https://github.com/vysheng/tg for more information.
### END INIT INFO

DESC="Telegram Cli Daemon"
NAME=telegramd
PIDFILE=/var/run/$NAME.pid

. /lib/lsb/init-functions

# Carry out specific functions when asked to by the system
case "$1" in

    start)
        echo -n "Starting $DESC ... "
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec /usr/bin/telegram -- -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -s /etc/telegram/action.lua -d -vvvv -E -R -D -C -P 2301 || true
        echo "Done."
    ;;

    stop)
        echo -n "Stopping $DESC ... "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec /usr/bin/telegram || true
    echo "Done."

    ;;
    restart)
        echo -n "Restarting $DESC "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec /usr/bin/telegram || true
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec /usr/bin/telegram -- -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -s /etc/telegram/action.lua -d -vvvv -E -R -D -C -P 2301 || true
        echo "Done."
    ;;

    status)
        status_of_proc -p $PIDFILE telegram $NAME && exit 0 || exit $?
    ;;

    *)
        N=/etc/init.d/$NAME
        echo "Usage $NAME: /etc/init.d/telegramd {start|stop|restart|status}"
        exit 1
    ;;

esac

set correct permission:
chmod 755 telegramd

set the service for automatic start
update-rc.d telegramd default

Use:
service telegramd start
service telegramd stop
service telegramd status

for send message from shell or another script use
echo "msg CONTACT_NAME text" | nc -w 5 localhost 2301

lua script for telegram-cli, process incoming messages

Install library:
apt-get install lua-lgi libjson-glib-dev

process.lua:

function on_msg_receive (msg)
  if started == 0 then
    return
  end
  if msg.out then
    return
  end


  if (msg.text == 'ping') then
    if (msg.to.id == our_id) then
      send_msg (msg.from.print_name, 'pong', ok_cb, false)
    else
      send_msg (msg.to.print_name, 'pong', ok_cb, false)
    end
    return
  end
  if (msg.text == 'PING') then
    if (msg.to.id == our_id) then
      fwd_msg (msg.from.print_name, msg.id, ok_cb, false)
    else
      fwd_msg (msg.to.print_name, msg.id, ok_cb, false)
    end
    return
  end
end

function on_our_id (id)
  our_id = id
end

function on_user_update (user, what)
  --vardump (user)
end

function on_chat_update (chat, what)
  --vardump (chat)
end

function on_secret_chat_update (schat, what)
  --vardump (schat)
end

function on_get_difference_end ()
end

function cron()
  -- do something
  postpone (cron, false, 1.0)
end

function on_binlog_replay_end ()
  started = 1
  postpone (cron, false, 1.0)
end

Use:
telegram -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -s /etc/telegram/process.lua

script shell send telegram msg with expect

First read this post for install and configure telegram

Create a new script named sendtg:

#!/usr/bin/expect
set timeout 2
set msg [lindex $argv 1]
set dest [lindex $argv 0]
spawn telegram -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -W
expect "> "
sleep 10
send "\rmsg $dest $msg\r"
expect "> "
expect eof

Config

move the script in /usr/bin:
mv sendtg /usr/bin/sendtg

and set correct permission:
chmod +x /usr/bin/sendtg

Usage

sendtg User “message to send”