Skip to content

Instantly share code, notes, and snippets.

@dhanar98
Last active January 13, 2024 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhanar98/17e1c554db6bb6bc9bcd265b4cb4386f to your computer and use it in GitHub Desktop.
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
<?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