Skip to content

CHICPRO

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

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

2017-12-08 by 편리

지난 포스트에서 telegram-cli를 데몬으로 설정해서 실행되도록 했다. 이 데몬을 이용해서 텔레그램에 메세지 등을 위한 PHP 클라이언트 프로그램을 작성하고 테스트한다. 이를 위해 telegram-cli 데몬 실행 스크립트를 약간 수정해서 리턴값을 JSON 형식으로 하고 tcp 포트와 unix 소켓을 이용해 통신이 가능하도록 해야 한다. 또한 telegram-cli 데몬의 소유권을 PHP 에서 접근할 수 있도록 웹서버의 소유권과 동일하게 설정했다. 사용자를 변경하게 되면 telegram-cli 데몬의 설정 등의 정보가 담겨있는 .telegram-cli 디렉토리를 웹서버 실행권한 소유권자의 홈디렉토리에 복사해줘야 한다. 예를 들어 www-data 사용자로 웹서버가 실행 될 때 www-data의 홈디렉토리가 /var/www 라면 /var/www/.telegram-cli 디렉토리에 설정 파일 등이 존재해야 한다.

sudo mv .telegram-cli /var/www

데몬 실행 스크립트는 다음의 코드로 수정한다. (참고 : https://github.com/vysheng/tg/wiki/Telegram-CLI-Arguments)

#! /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=www-data
GROUPNAME=www-data
LOGFILE=/var/log/telegramd.log
DAEMON=/usr/bin/telegram-cli
TGPORT=2391
TGSOCKET=/tmp/telegram-cli.sock

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

DAEMON_ARGS="--json -b -U $USERNAME -G $GROUPNAME -k $TelegramKeyFile -W -S $TGSOCKET -P $TGPORT -d -vvvRC"
#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
    rm -f $TGSOCKET
    echo "Done."

    ;;
    restart)
        echo -n "Restarting $DESC "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec $DAEMON || true
        rm -f $PIDFILE
        rm -f $TGSOCKET
        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

이제 PHP 에서 telegram-cli 데몬에 접근해 명령을 실행할 수 있는 코드이다.

<?php

/**
 * PHP Client for telegram-cli daemon
 */

class TELEGRAMCLI
{
    private $f;
    
    public function __construct($socket) {
        try {
            $this->f = stream_socket_client($socket, $errno, $errstr);

            if ($this->f === false) {
                throw new Exception('ERROR : '.$errstr, $errno);
            }
        } catch (Exception $e) {
            die($e->getMessage() . ' ' . $e->getCode());
        }
    }

    public function __call($method, $arguments)
    {
        return $this->run($method, $arguments);
    }

    private function run($command, $args = array())
    {
        if (!empty($args)) {
            $args = implode(' ', $args);
        } else {
            $args = '';
        }

        $command = str_replace("\n", '', $command);
        $command .= ' ' . $args;

        fwrite($this->f, $command . PHP_EOL);
        
        $fg = fgets($this->f);

        try {
            if (strpos($fg, 'ANSWER') !== false) {
                $responseLength = (int)str_replace('ANSWER ', '', $fg);
                
                $readLength = 0;
                $json = '';
                
                while ($readLength < $responseLength) {
                    $json .= fread($this->f, $responseLength - $readLength);
                    $readLength = strlen($json);
                }

                $result = json_decode($json, true);
                
                if (!is_array($result)) {
                    throw new Exception('ERROR : Invalid JSON Response');
                }

                return $result;
            } else {
                throw new Exception($fg);
            }
        } catch(Exception $e) {
            die($e->getMessage());
        }
    }
}

위 class를 이용하여 telegram-cli 명령을 실행하는 코드는 아래와 같다.

<?php

include_once('./telegramcli.class.php');

$telegram = new TELEGRAMCLI('unix:///tmp/telegram-cli.sock');

$result = $telegram->contact_list();

//$result = $telegram->msg('Seongho_Jang', 'Web 테스트');

print_r($result);

위 코드를 실행하면 텔레그램 주소록의 배열로 출력한다. 메세지를 전송하기 위해서는 주소록의 이름과 내용를 msg 명령을 통해 telegram-cli 데몬에 전달하면 된다. 이름을 작성할 때 first name 과 last name 사이의 공백은 _ 로 해야한다.

Post navigation

Previous Post:

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

Next Post:

블로그 테마 변경

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