-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from Crudzaso/develop
Develop
- Loading branch information
Showing
14 changed files
with
268 additions
and
87 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Throwable; | ||
use App\Helpers\DiscordNotifier; | ||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | ||
|
||
class Handler extends ExceptionHandler | ||
{ | ||
protected $dontReport = [ | ||
\Illuminate\Validation\ValidationException::class, | ||
\Illuminate\Session\TokenMismatchException::class, | ||
]; | ||
|
||
public function report(Throwable $exception) | ||
{ | ||
// Notifica a Discord si no está en la lista de exclusión | ||
if ($this->shouldReport($exception)) { | ||
try { | ||
$this->notifyDiscord($exception); | ||
} catch (Throwable $e) { | ||
// Evitar que fallos en la notificación rompan el flujo | ||
logger()->error("Error al enviar notificación a Discord: " . $e->getMessage()); | ||
} | ||
} | ||
|
||
parent::report($exception); | ||
} | ||
|
||
protected function notifyDiscord(Throwable $exception) | ||
{ | ||
$details = [ | ||
'message' => $exception->getMessage(), | ||
'file' => "{$exception->getFile()}:{$exception->getLine()}", | ||
'trace' => substr($exception->getTraceAsString(), 0, 1800), | ||
]; | ||
|
||
// Logea el error antes de enviarlo a Discord | ||
logger()->error('Excepción capturada', $details); | ||
|
||
// Envía la notificación a Discord | ||
DiscordNotifier::notifyEvent( | ||
'Excepción en el sistema 🚨', | ||
$details, | ||
asset('images/logo.png') | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
|
||
class DiscordNotifier | ||
{ | ||
public static function send($message, $embed = []) | ||
{ | ||
$webhookUrl = env('DISCORD_WEBHOOK_URL'); | ||
|
||
if ($webhookUrl) { | ||
$payload = [ | ||
'content' => $message, | ||
]; | ||
|
||
if (!empty($embed)) { | ||
$payload['embeds'] = [$embed]; | ||
} | ||
|
||
Http::post($webhookUrl, $payload); | ||
} | ||
} | ||
|
||
public static function notifyException(\Throwable $exception) | ||
{ | ||
$trace = substr($exception->getTraceAsString(), 0, 1800); // Discord tiene límites. | ||
$message = "**🚨 Exception Alert**\n" | ||
. "**Mensaje:** {$exception->getMessage()}\n" | ||
. "**Archivo:** {$exception->getFile()}:{$exception->getLine()}\n" | ||
. "**Trace:** ```{$trace}```"; | ||
|
||
self::send($message); | ||
} | ||
|
||
public static function notifyEvent($eventType, $details = [], $imageUrl = null) | ||
{ | ||
$webhookUrl = env('DISCORD_WEBHOOK_URL'); | ||
|
||
if ($webhookUrl) { | ||
$logoUrl = $imageUrl ?? asset('logo.png'); // Imagen por defecto | ||
$embed = [ | ||
'title' => '🔔 Notificación del Sistema', | ||
'description' => "Se ha detectado un evento: **{$eventType}**.", | ||
'color' => 7506394, // Color (hex: #72A0C1) | ||
'fields' => [], | ||
'footer' => [ | ||
'text' => 'Notificaciones del Sistema', | ||
'icon_url' => $logoUrl, | ||
], | ||
'timestamp' => now()->toIso8601String(), | ||
]; | ||
|
||
// Agregar detalles al mensaje. | ||
foreach ($details as $key => $value) { | ||
$embed['fields'][] = [ | ||
'name' => ucfirst($key), | ||
'value' => $value, | ||
'inline' => true, | ||
]; | ||
} | ||
|
||
self::send('', $embed); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Throwable; | ||
use App\Helpers\DiscordNotifier; | ||
|
||
class HandleExceptions | ||
{ | ||
public function handle($request, Closure $next) | ||
{ | ||
try { | ||
return $next($request); | ||
} catch (Throwable $e) { | ||
$this->reportToDiscord($e); // Enviar notificación a Discord | ||
throw $e; // Rethrow para permitir que el sistema maneje la excepción | ||
} | ||
} | ||
|
||
protected function reportToDiscord(Throwable $exception) | ||
{ | ||
$message = "**🚨 Excepción Crítica**\n" | ||
. "**Mensaje:** {$exception->getMessage()}\n" | ||
. "**Archivo:** {$exception->getFile()}:{$exception->getLine()}\n" | ||
. "**Trace:** ```" . substr($exception->getTraceAsString(), 0, 1800) . "```"; | ||
|
||
DiscordNotifier::send($message); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use Illuminate\Auth\Events\Login; | ||
use App\Helpers\DiscordNotifier; | ||
|
||
class SendLoginNotification | ||
{ | ||
/** | ||
* Handle the event. | ||
* | ||
* @param Login $event | ||
* @return void | ||
*/ | ||
public function handle(Login $event) | ||
{ | ||
DiscordNotifier::notifyEvent('User Logged In', [ | ||
'user_id' => $event->user->id, | ||
'email' => $event->user->email, | ||
]); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use Illuminate\Auth\Events\Login; | ||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | ||
use App\Listeners\SendLoginNotification; | ||
|
||
class EventServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* The event listener mappings for the application. | ||
* | ||
* @var array | ||
*/ | ||
protected $listen = [ | ||
Login::class => [ | ||
SendLoginNotification::class, | ||
], | ||
]; | ||
|
||
/** | ||
* Register any events for your application. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
parent::boot(); | ||
} | ||
} |
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
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
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
Oops, something went wrong.