sindaniel/laravel-telegram-error-reporter
最新稳定版本:v1.0.0
Composer 安装命令:
composer require sindaniel/laravel-telegram-error-reporter
包简介
Laravel package to report application errors to Telegram
README 文档
README
A Laravel package that automatically reports your application's 500 errors to Telegram.
Installation
Install the package via Composer:
composer require sindaniel/laravel-telegram-error-reporter
Publish the configuration file:
php artisan vendor:publish --provider="Sindaniel\LaravelTelegramErrorReporter\TelegramErrorReporterServiceProvider" --tag="config"
Configuration
1. Create a Telegram Bot
- Open Telegram and search for
@BotFather - Send
/newbotand follow the instructions - Save the token provided by BotFather
2. Get the Chat ID
To get your personal Chat ID:
- Search for
@userinfobotin Telegram - Send
/startand you'll get your Chat ID
For a group:
- Add your bot to the group
- Send any message in the group
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Look for the
chat.idin the response
3. Environment Variables
Add these variables to your .env file:
TELEGRAM_ERROR_BOT_TOKEN=your_bot_token_here TELEGRAM_ERROR_CHAT_ID=your_chat_id_here TELEGRAM_ERROR_ENABLED=true
Usage
Basic Configuration in bootstrap/app.php
Replace your current code with this:
->withExceptions(function (Exceptions $exceptions) { $exceptions->report(function (Throwable $e) { // Report to Telegram app(\Sindaniel\LaravelTelegramErrorReporter\TelegramErrorReporter::class) ->report($e, $errorData); }); });
Test the Configuration
You can test if your configuration works correctly:
use Sindaniel\LaravelTelegramErrorReporter\TelegramErrorReporterFacade as TelegramError; $result = TelegramError::test(); dd($result);
Advanced Configuration
The configuration file config/telegram-error-reporter.php contains the following options:
Environment Filters
'environments' => ['production'], // Only report in production
Rate Limiting
'rate_limit_per_minute' => 5, // Maximum 5 errors per minute
Custom Message Template
'message_template' => "🚨 *Error in {app_name}*\n\n" . "**Environment:** {environment}\n" . "**Message:** {message}\n" . "**File:** {file}:{line}\n" . "**URL:** {url}\n" . "**IP:** {ip}\n" . "**Time:** {timestamp}",
Available variables:
{app_name}- Application name{environment}- Current environment (production, local, etc.){message}- Error message{file}- File where the error occurred{line}- Line where the error occurred{url}- URL where the error occurred{ip}- User's IP address{user_agent}- Browser's user agent{timestamp}- Date and time of the error{session_id}- Session ID
Example Artisan Command for Testing
Create an Artisan command to test the reporter:
php artisan make:command TestTelegramError
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Sindaniel\LaravelTelegramErrorReporter\TelegramErrorReporterFacade as TelegramError; class TestTelegramError extends Command { protected $signature = 'telegram:test-error'; protected $description = 'Test Telegram error reporting'; public function handle() { try { // Create a test error throw new \Exception('This is a test error for Telegram'); } catch (\Exception $e) { $result = TelegramError::report($e, [ 'test_context' => 'Test command executed', 'user' => 'System' ]); if ($result) { $this->info('✅ Error successfully reported to Telegram'); } else { $this->error('❌ Error reporting to Telegram failed'); } } } }
Features
- ✅ Automatic 500 error reporting
- ✅ Environment filters (production only, staging, etc.)
- ✅ Rate limiting to prevent spam
- ✅ Customizable message templates
- ✅ Additional context support
- ✅ Safe error handling (doesn't cause additional errors)
- ✅ Automatic escaping of Markdown special characters
- ✅ Configurable timeouts for HTTP requests
License
MIT License
Contributing
Contributions are welcome. Please open an issue or pull request.
Changelog
1.0.0
- Initial release
- Basic error reporting to Telegram
- Configuration via config file and environment variables
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-16