[PHP] 텔레그램 봇 메시지 전송 함수

function telegram_send($msg)
{
      // https://api.telegram.org/bot[apiKey를 여기에]/getUpdates 여기에서 채널ID나 봇채팅ID 확인
      $apikey = [apiKey를 여기에];
      $chat_id = [채널이나 봇채팅 ID];
      $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;
}

Leave a Comment