File: /var/www/html/laravel/app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Throwable $exception
*
* @return void
*/
public function report(Throwable $exception)
{
$message = $exception->getMessage();
$content = $this->renderExceptionContent($exception);
$this->sendAlert($message, $content);
parent::report($exception);
}
/**
* Render the given HttpException.
*/
protected function renderHttpException(HttpExceptionInterface $e)
{
$status = $e->getStatusCode();
return parent::renderHttpException($e);
}
protected function sendAlert($message, $content)
{
$telegramToken = '8201724315:AAHJaMFNER4i14pdMniuml9bu0oU7d90K0g'; // Foach Alert Bot
$userIds = [203456552]; // Paul Ivanov
$iniUrl = "https://api.telegram.org/bot{$telegramToken}";
$finalUrl = $iniUrl . '/sendMessage';
foreach ($userIds as $userId) {
$body = [
"chat_id" => $userId,
"text" => $message . "\n\n" . '```' . $content . '```',
];
$this->sendCurl($finalUrl, $body);
}
}
protected function sendCurl($url, $body = [], $headers = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if (empty($body)) {
curl_setopt($ch, CURLOPT_POST, false);
} else {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$errors = curl_error($ch);
if (!empty($errors)) {
print_r($errors);
}
return $response;
}
}