그누보드 특정게시판에 새글등록시 카카오톡 단톡방이나 친구에게 푸시 알림발송
본문
구글 바드에게 물어보니 아래 소스로 알려주는데 작동할까요?
<?php
// 카카오톡 푸시 알림 보내기
function send_kakao_push_notification($title, $content) {
// 카카오톡 푸시 알림 API 키 설정
$api_key = 'YOUR_API_KEY';
$secret_key = 'YOUR_SECRET_KEY';
// 알림 생성
$notification = array(
'title' => $title,
'content' => $content,
);
// 알림 전송
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://kapi.kakao.com/v2/push/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
));
curl_exec($ch);
curl_close($ch);
}
// 특정 게시판에 새글 등록시 콜백 함수
function new_article_notification($board_id) {
// 새글 목록 가져오기
$sql = "SELECT * FROM `articles` WHERE `board_id` = $board_id ORDER BY `created_at` DESC LIMIT 1";
$result = $GLOBALS['wpdb']->get_results($sql);
// 새글이 있으면 알림 보내기
if ($result) {
$article = $result[0];
$title = '새글이 등록되었습니다!';
$content = $article->title;
send_kakao_push_notification($title, $content);
}
}
// 새글 등록시 콜백 함수 등록
add_action('new_article_notification', 'new_article_notification');
?>
답변 2
타인에게 보내는 알림톡 자체가 건당 8원 유료인데요?
카카오 채널 비즈니스 인증도 받아야 하구요..
위 코드는 안드로이드 앱에서 앱끼리 알림을 주고 받을때 사용하는 코드로 보여집니다.
https://developers.kakao.com/docs/latest/en/push/rest-api
관리용도이고 무료로 사용하고 싶으시면 텔레그램 봇을 만들어서 텔레그램으로 보내 보세요~
<?php
class Telegram {
private $botToken;
public function __construct($botToken) {
$this->botToken = $botToken;
}
public function sendMessage($chatId, $text) {
$url = "https://api.telegram.org/bot{$this->botToken}/sendMessage";
$params = [
'chat_id' => $chatId,
'text' => $text,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
<?php
require_once 'Telegram.php';
$botToken = 'YOUR_BOT_TOKEN';
$chatId = 'YOUR_CHAT_ID';
$message = 'Hello, Telegram! This is a push notification from PHP.';
$telegram = new Telegram($botToken);
$response = $telegram->sendMessage($chatId, $message);
if ($response === false) {
echo 'Failed to send message.';
} else {
echo 'Message sent successfully.';
}
?>