37 lines
887 B
PHP
37 lines
887 B
PHP
<?php
|
|
|
|
/**
|
|
* Sends a message to a Telegram chat.
|
|
*
|
|
* @param string $message The message text.
|
|
* @return bool True on success, false on failure.
|
|
*/
|
|
function telegramSendMessage($message) {
|
|
$url = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/sendMessage";
|
|
|
|
$postData = [
|
|
'chat_id' => TELEGRAM_CHAT_ID,
|
|
'text' => $message
|
|
];
|
|
|
|
// Initialize cURL
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
error_log("Telegram cURL error: " . curl_error($ch));
|
|
curl_close($ch);
|
|
return false;
|
|
}
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
return $httpCode === 200;
|
|
}
|