Last active
January 13, 2024 10:08
-
-
Save dhanar98/17e1c554db6bb6bc9bcd265b4cb4386f to your computer and use it in GitHub Desktop.
Send Telegram Notification or Message to Telegram Channel in Laravel Application in Custom Helper Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Client; | |
use Illuminate\Support\Facades\Log; | |
function sendTelegramNotification($botKey,$chatId, $message) { | |
try { | |
$botToken = $botKey; | |
$apiEndpoint = "https://api.telegram.org/bot{$botToken}/sendMessage"; | |
$client = new Client(); | |
$response = $client->post($apiEndpoint, [ | |
'json' => [ | |
'chat_id' => $chatId, | |
'text' => $message, | |
], | |
]); | |
// Check for success and handle errors if needed | |
$statusCode = $response->getStatusCode(); | |
if ($statusCode !== 200) { | |
// Handle error here | |
} | |
$responseBody = $response->getBody()->getContents(); | |
// Process the response if needed | |
$responseData = json_decode($responseBody, true); | |
} catch (\Exception $e) { | |
Log::error("sendTelegramNotification ===> ".$e->getMessage()); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment