테스트 사이트 - 개발 중인 베타 버전입니다

텔레그램 API 연동/그누보드 게시물·댓글 등록 시 PUSH 보내기

· 6년 전 · 8612 · 17

◆ 관리자 화면 - 좌측 사이드바 서브 메뉴 추가

 

1982703122_1568486306.7687.png

 

  • 폴더 위치: /adm/
  • 대상 파일: admin.menu100.php

 

[code]

$menu['menu100'][] = array('100800', '텔레그램 플러그인 설정', G5_ADMIN_URL.'/telegram_setting.php', 'cf_service');

[/code]

 

◆ 관리자 화면 - 텔레그램 설정 페이지 구축하기

 

 

 

① DB 구축하기(아래 쿼리 DB INSERT)

 

[code]

CREATE TABLE IF NOT EXISTS `telegram_setting` (
  `idx` int(255) NOT NULL,
  `bot_api_code` varchar(255) NOT NULL,
  `bot_chat_id` varchar(255) NOT NULL,
  KEY `bot_api_code` (`bot_api_code`),
  KEY `bot_chat_id` (`bot_chat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 테이블의 덤프 데이터 `telegram_setting`
--

INSERT INTO `telegram_setting` (`idx`, `bot_api_code`, `bot_chat_id`) VALUES
(1, '', '');

[/code]

 

특이사항

  • INDEX: bot_api_code, bot_chat_id

 

 

 

② 텔레그램 설정 페이지(PHP)

  • 폴더 위치: /adm/
  • 대상 파일: telegram_setting.php ( 신규 생성 필요 )
     

1982703122_1568486387.4691.png

 

[code]
<?php
$sub_menu = '100800';
include_once('./_common.php');
auth_check($auth[$sub_menu], 'r');

$g5['title'] = '텔레그램 플러그인 기본설정';
include_once('./admin.head.php');

$row = sql_fetch(" select bot_api_code, bot_chat_id from telegram_setting where idx = 1 ");

?>

    <?php
        if($_GET['mode'] == 'update'){
        
                $bot_api_code = trim($_POST['bot_api_code']);
                $bot_chat_id = trim($_POST['bot_chat_id']);
            
                if(!$bot_chat_id && !$bot_chat_id){
                    echo "<script>alert('누락된 정보가 있습니다.'); location.href = '$PHP_SELF?mode=select'; </script> ";
                    exit();
                }//
            
                sql_query(" update telegram_setting set bot_api_code = '$bot_api_code', bot_chat_id = '$bot_chat_id' ");
                echo "<script>alert('정상적으로 DB에 반영됐습니다.'); location.href = '$PHP_SELF?mode=select'; </script> ";
                exit();         
            
        }elseif($_GET['mode'] == 'reset'){
        
            sql_query(" truncate table telegram_setting ");
            sql_query(" insert into telegram_setting VALUES (1, '','') ");

            echo "<script>alert('정상적으로 초기화가 완료됐습니다.'); location.href = '$PHP_SELF?mode=select'; </script> ";            
            
        } // end
    ?>

<form action='<?=$PHP_SELF;?>?mode=update' method=post>
    <div class="local_desc02 local_desc" style="background:#f3f4f3;">
        <p>이 서비스는 'SIR.CO.KR' 에서 활동하는 '유진201907' 이 개발한 텔레그램 플러그인의 기본설정을 하는 데 목적이 있습니다. </p>
    </div>

    <div class="tbl_frm01 tbl_wrap">
            <table>
            <caption>홈페이지 기본환경 설정</caption>
            <colgroup>
                <col class="grid_4">
                <col>
                <col class="grid_4">
                <col>
            </colgroup>
            <tbody>
                
            <tr>
                <th scope="row"><label for="cf_title">수신 BOT API CODE<strong class="sound_only">필수</strong></label></th>
                <td colspan="3"><input type="text" name="bot_api_code" value="<?php if($row['bot_api_code']){ echo $row['bot_api_code']; } ?>" placeholder='435070531:AFFCwB4JfvU6hai1u_Osal3dYcERx4F67wQ' id="cf_title" required="" class="required frm_input" size="80"></td>
            </tr>
            <tr>
                <th scope="row"><label for="cf_admin">수신 BOT CHAT ID<strong class="sound_only">필수</strong></label></th>
                <td colspan="3"><input type="text" name="bot_chat_id" value="<?php if($row['bot_chat_id']){ echo $row['bot_chat_id']; } ?>" placeholder="354402611" id="cf_title" required="" class="required frm_input" size="80"></td>
            </tr>

            <tr>
                <th scope='row'>관리하기</th>
                <td colspan=3>
                    <input type="submit" value="DB에 반영 요청하기" class="btn_submit btn" accesskey="s">
                    <input type="button" onclick="location.href = '<?=$PHP_SELF;?>?mode=reset'; " value="DB 리셋" style="background:#3f51b5;" class="btn_submit btn" accesskey="s">
                
                </td>
            </tr>

            </tbody>
            </table>
        </div>

    <div class="local_desc02 local_desc" style="background:#f3f4f3;">
        <p><strong>◆ 텔레그램 봇 생성방법:</strong> <a href='https://hatpub.tistory.com/48' target=_blank>https://hatpub.tistory.com/48</a></p>
        <p><strong>◆ Bot 생성 후 Chat_id 구하기:</strong> <a href='https://blog.acidpop.kr/216' target=_blank>https://blog.acidpop.kr/216</a></p>
    </div>
</form>
<?php
include_once('./admin.tail.php');
?>
[/code]

 

특이사항

  • 'DB 리셋' 버튼 클릭 시 당해 테이블 데이터 초기화 & 기본 DB 삽입처리

 

 

 

◆ CURL 활용하여 텔레그램에 메세지 보내기 API

  • 대상 폴더: /lib/
  • 대상 파일: telegram_api.php

 

[code]

<?php
if (!defined('_GNUBOARD_')) exit;

function curl_tele_sent($text){
   
 if(!$text){
        exit("No Data!!");
    } // end
    
    $row = sql_fetch(" select bot_api_code, bot_chat_id from telegram_setting where idx = 1 ");
    
    if(!$row['bot_api_code'] && !$row['bot_chat_id']){
        exit();
    }
    
    $ch = curl_init();
    $api_code = $row['bot_api_code']; // 900~
    $chat_id = $row['bot_chat_id']; // 822~~
    $curl_url = "https://api.telegram.org/bot{$api_code}/sendMessage?chat_id={$chat_id}&text={$text}";
    @curl_setopt($ch, CURLOPT_URL, $curl_url);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
    @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);      
    @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   
    $exec = curl_exec($ch);
    
    
} // curl_tele_sent end 
?>

[/code]

 

 

 

◆ 게시물(답변 포함) 등록 시 텔레그램으로 등록 메세지 보내기

  • 대상 폴더: /bbs/
  • 대상 파일: write_update.php

[code]include_once(G5_LIB_PATH.'/telegram_api.php');[/code]

[code]

// 텔레그램으로 게시물 등록 알림 보내기 
    curl_tele_sent('[System] '.$bo_table.' 게시판에 새로운 게시물(답변 포함)이 등록됐습니다.');
// 텔레그램으로 게시물 등록 알림 보내기

[/code]

 

특이 사항

  • INSERT 영역인 '// 게시글 1 증가' 바로 아래 등록하기
  • 현재 게시물 등록 시에만 작동토록 설정 돼 있는데, UPDATE 문에도 추가하면 수정 시에도 PUSH 발송 가능합니다.

 

 

 

◆ 게시물 댓글 등록 시 텔레그램으로 등록 메세지 보내기

  • 대상 폴더: /bbs/
  • 대상 파일: write_comment_update.php

[code]include_once(G5_LIB_PATH.'/telegram_api.php');[/code]

[code]curl_tele_sent('[System] '.$bo_table.' 게시판에 새로운 댓글이 등록됐습니다.');[/code]

 

특이 사항

  • INSERT 영역인 '// 포인트 부여' 바로 아래 등록하기
  • 현재 게시물 등록 시에만 작동토록 설정 돼 있는데, UPDATE 문에도 추가하면 수정 시에도 PUSH 발송 가능합니다.

 

※ 본 글과 관련하여 개인적인 유지보수는 지원하지 않습니다.

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 17개

좋은 내용 감사합니다.
추천해요
오~ 멋지십니다. 감사합니다.
정보감사합니다.
정보 감사 합니다,
관련글: https://sir.kr/qa/487152
감사합니다. ㅎㅎ
적용해봐야겠습니다.

게시글 목록

번호 제목
24149
24140
24133
24125
24119
24109
24105
24101
24093
24089
24077
24074
24071
24070
24067
24056
24050
24046
24043
24040
24037
24036
24035
24034
24021
24017
24005
24002
23990
23980