PHP | [PHP] 텔레그램 api로 push 받기(Webhook)
페이지 정보
작성일2019-10-01 12:44 조회4,849회관련링크
본문
1. https://telegram.me/botfather 접속
2. [시작] 버튼 클릭
3. /newbot 입력
4. 봇 이름 입력
5. 봇 이름_bot 또는 봇 이름Bot 입력
6. <apikey>값 저장
7. /start 입력
8. https://api.telegram.org/bot<apikey>/getUpdates 입력
9. <chat_id>값 저장
위에서 발급받은 <apikey>값, <chat_id>값이 쓰입니다.
--------------------------------------
아래 텔레그램 Push PHP 소스
[FUNCTION]
<?php $output = telegram_send("안녕하세요"); function telegram_send($msg) { $apikey = '914976991:AAhhoyjk2zhm34vepp14blnFyq4DAgq'; $chat_id = '85162432'; $params = 'chat_id='.$chat_id.'&text='.urlencode($msg); $webhook = 'https://api.telegram.org/bot'.$apikey.'/sendMessage'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $webhook); // Webhook URL curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $return = curl_exec($ch); curl_close($ch); return $return; } ?>
<?php class Telegram { /* * @var string bot_token */ private $bot_token; /* * @var string $url */ private $url = 'https://api.telegram.org/bot'; /* * @var string $chat_id */ private $chat_id; /** * @var string $method API method */ private $method; /** * @var string $headers */ private $headers = []; /** * @var string $data */ private $data = []; /** * @var string $http_code */ private $http_code; /** * @var string $response */ protected $response = ''; public function __construct($token, $chat_id) { $this->headers = array( 'Accept: application/json', 'Content-Type: application/json' ); $this->bot_token = $token; $this->chat_id = $chat_id; } public function get_http_code() { return $this->http_code; } public function get_url() { return $this->url . $this->bot_token .'/'; } public function get_request() { return $this->request; } public function set_request(string $request) { $this->request = $request; } public function get_data() { return $this->data; } public function set_data($value, $key = '') { if (is_array($value) === true && count($value) > 0) { $this->data = array_merge($this->data, $value); } else { if ($key != '') { $this->data[$key] = $value; } else { $this->data[] = $value; } } } public function reset_data() { $this->data = []; } /** * Curl 통신 */ private function api_request() { $ch = curl_init(); $params = ''; if(count($this->get_data()) > 0 and strtolower($this->request) == 'get') { $params = $this->get_data(); foreach ($params as $key => &$val) { // encoding to JSON array parameters, for example reply_markup if (!is_numeric($val) && !is_string($val)) { $val = json_encode($val); } } $params = '?'.http_build_query($params); } $curl = $this->get_url().$this->method.$params; curl_setopt_array($ch, array( CURLOPT_URL => $curl, CURLOPT_HTTPHEADER => $this->headers, CURLOPT_CUSTOMREQUEST => $this->request, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, )); if (count($this->get_data()) > 0 and strtolower($this->request) == 'post') { crl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('params' => $this->get_data()))); } $this->response = curl_exec($ch); $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } /** * Curl 결과 */ public function api_response() { try { $response = $this->response; $output = []; $output['code'] = 1; $output['data'] = $response; return $output; } catch (Exception $e) { $output = []; $output['code'] = $e->getCode(); $output['msg'] = $e->getMessage(); return $output; } } /** * Send Message */ public function send($msg) { $this->method = 'sendMessage'; $this->request = 'GET'; $this->reset_data(); $this->set_data($this->chat_id, 'chat_id'); $this->set_data($msg, 'text'); $this->api_request(); return $this->api_response(); } } /** * Class 호출 */ $tg = new Telegram('914976991:AAhhoyjk2zhm34vepp14blnFyq4DAgq', '85162432'); $output = $tg->send('안녕하세요.'); ?>
Developer 관련 글 보기
- PHP | (PHP/MySQL) password_hash / password / old_password 2022-03-02
- PHP | 텔레그램(telegram) 봇 API 응답 값 살펴보기 2019-10-01
- Server | PHP 7.3, 7.2, 7.1 on CentOS/RHEL 6.10 2019-06-24
- Server | 사설인증서 생성을 위한 OpenSSL (for Windows) 사용방법 2019-06-14
- Script | jquery live, bind, delegate 를 on 대체 2018-01-16
- HTML | ASCII Code 특수기호 모음 2016-12-10
- PHP | 줄바꿈,CRLF(개행문자) 및 특정 태그 제거 정규식 2016-11-28