Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

Ubuntu 16.04 에 telegram-cli 설치 및 데몬실행 설정

2017-12-07 by 편리

텔레그램을 이용한 알림 전송은 이전 포스트와 같이 처리하면 가능하다. 다만 chat_id를 확인하고 등록해야만 하는데 이런 불편함을 없애보고자 telegram-cli 를 개발서버에 설치해 메세지 전송을 테스트하려고 telegram-cli를 아래와 같이 설치했다.

telegram-cli : https://github.com/vysheng/tg

sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make
git clone --recursive https://github.com/vysheng/tg.git && cd tg
./configure
make

위 내용대로 진행하면 이상없이 설치가 된다.. 아니 되어야 한다. 그런데 나의 경우 libssl-dev 설치 버전이 1.1 이이서 make 과정에서 openssl 관련 오류가 발생했다. openssl은 시스템 전반에 영향을 끼치는 것이라 버전을 바꾸기도 애매했는데 확인해보니 PHP를 설치하기 위해 ondrej/php ppa 를 이용한 경우에 1.1 버전이 설치되는 듯 했다. PHP를 제거하고 ppa를 제거한 후 우분투 16.04에서 기본 제공하는 php 7.0.x 버전을 설치하면 1.0 버전을 유지할 수 있어 컴파일 때 오류가 발생하지 않는다. 컴파일 완료 후 아래 명령을 통해 telegram-cli를 실행한다.

bin/telegram-cli

휴대폰 번호 인증 후 telegram-cli 를 통해 메세지를 전송할 수 있다. 간단한 사용법은 http://tech.whatap.io/2015/09/25/telegram-cli/ 참고.

원래 목적이 휴대폰 번호를 이용해 메세지를 전송하고 사내에서는 쉽게 이용할 수 있도록 하는 것이기 때문에 telegram-cli를 서버 데몬으로 실행할 수 있도록 추가 작업을 진행했다. 데몬으로 설정하는 것은 https://github.com/vysheng/tg/wiki/Running-Telegram-CLI-as-Daemon 를 참고해서 진행했다. 문서에 나온대로 진행할 경우 데몬 실행에 오류가 발생하기 때문에 마지막 과정에서 데몬실행 telegram-daemon 파일의 내용을 수정했다.

sudo adduser telegramd
sudo mkdir /var/lib/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
sudo mkdir /etc/telegram-cli
sudo cp server.pub /etc/telegram-cli/server.pub
sudo vim /etc/init.d/telegram-daemon

telegram-daemon 파일에 아래 내용을 추가한다.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          telegram-cli
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Commandline interface for Telegram chat program
# 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.
#
#                    Note #1: This version of the init script is developed for raspbian (rapberry PI port of Debian Wheezy).
#
#
#                    See: https://github.com/vysheng/tg for more information.
#                    Derived from https://github.com/vysheng/tg/issues/436 (updated 9th April 2015)
#                    Further derived from: https://www.domoticz.com/wiki/Installing_Telegram_Notification_System#.2Fetc.2Finit.d.2Ftelegram-cli
### END INIT INFO


# Set some variables
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Telegram Messaging System"
NAME=telegram-cli
USERNAME=telegramd
GROUPNAME=telegramd
LOGFILE=/var/log/telegramd.log
DAEMON=/usr/bin/telegram-cli
TGPORT=2391

TelegramKeyFile="/etc/telegram-cli/server.pub"

DAEMON_ARGS="-W -b -U $USERNAME -G $GROUPNAME -k $TelegramKeyFile -L $LOGFILE -P $TGPORT -d -vvvRC"
#DAEMON_ARGS="-W -U telegramd -G telegramd -k $TelegramKeyFile -L /var/log/telegramd.log -P $TGPORT -d -vvvRC"

PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# 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 $DAEMON -- $DAEMON_ARGS || true
        echo "Done."
    ;;

    stop)
        echo -n "Stopping $DESC ... "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec $DAEMON || true
    rm -f $PIDFILE
    echo "Done."

    ;;
    restart)
        echo -n "Restarting $DESC "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec $DAEMON || true
        rm -f $PIDFILE
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec $DAEMON -- $DAEMON_ARGS || true
        echo "Done."
    ;;

    status)
        if [ -f $PIDFILE ]; then
                PID=`cat $PIDFILE`
        else
                echo "No pid file, telegramd not running?"
                exit 1
        fi
        if [ "`ps -p $PID -o comm=`" = "telegram-cli" ]; then
                echo "telegramd running with pid $PID"
        else
                echo "telegramd not running. removing $PIDFILE"
                rm -f $PIDFILE
                exit 1
        fi
    ;;

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

esac

데몬을 서비스로 등록하기 위해 아래 명령을 실행한다. 데몬의 포트를 변경하려면 위 스크립트 내용 중 2391 을 다른 포트번호로 변경한다. 참고 : https://github.com/vysheng/tg/issues/373#issuecomment-165580387

sudo update-rc.d telegram-daemon defaults

telegram-cli 데몬을 실행하기 위해 아래 명령을 실행한다. start 와 restart, stop, status 도 사용할 수 있다.

sudo service telegram-daemon start

telegram-cli 데몬에 명령을 전달하기 위해 netcat 또는 telnet 을 이용할 수 있다. 아래 명령은 netcat을 이용한 것이다.

nc localhost 2391
contact_list

사용할 수 있는 명령은 https://github.com/vysheng/tg#messaging 를 참고한다.

Post navigation

Previous Post:

인텔 취약점 해결을 위한 GA-B250-HD3 메인보드 Bios 업데이트

Next Post:

[PHP] telegram-cli 데몬 이용을 위한 PHP client

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅
  • 우분투 시스템 터미널쉘 zsh 로 변경

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes