rud99/laravel-telegram-cleanup

Auto-delete Telegram bot messages in Laravel applications

Installs: 11

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/rud99/laravel-telegram-cleanup

v0.0.5 2026-01-26 15:48 UTC

This package is auto-updated.

Last update: 2026-01-26 15:48:37 UTC


README

Русская документация / Russian Documentation

Auto-delete Telegram bot messages in Laravel applications.

Installation

composer require rud99/laravel-telegram-cleanup
php artisan migrate

Requirements

This package extends laravel-notification-channels/telegram and uses the same bot token configuration.

Make sure you have configured the token in config/services.php:

'telegram-bot-api' => [
    'token' => env('TELEGRAM_BOT_TOKEN'),
],

Configuration

Publish the configuration file to customize settings:

php artisan vendor:publish --tag=telegram-cleanup-config

Available options in config/telegram-cleanup.php:

Option Default Description
default_delete_after_minutes 1440 Default TTL for messages (24 hours)
delay_between_requests_ms 150 Delay between API calls (rate limit)
max_retry_attempts 3 Retries on 429 errors
prune_after_months 1 Keep deleted records for N months

Usage

1. Create a notification extending TelegramCleanupNotification

use Rud99\TelegramCleanup\Notifications\TelegramCleanupNotification;
use NotificationChannels\Telegram\TelegramMessage;

class VerificationCodeNotification extends TelegramCleanupNotification
{
    public function __construct(
        protected string $code,
        ?int $deleteAfterMinutes = 5
    ) {
        $this->deleteAfterMinutes = $deleteAfterMinutes;
        $this->message = "Your code: {$code}";
        $this->meta = ['type' => 'verification'];
    }

    public function toTelegram(object $notifiable): TelegramMessage
    {
        return TelegramMessage::create()
            ->content($this->message);
    }
}

2. Send notification

Via queue (default):

use Illuminate\Support\Facades\Notification;

Notification::route('telegram', $chatId)
    ->notify(new VerificationCodeNotification('1234', 5));

The notification will be queued and processed by your queue worker.

Without queue (synchronous):

Notification::route('telegram', $chatId)
    ->notifyNow(new VerificationCodeNotification('1234', 5));

The notification will be sent immediately.

The message will be automatically deleted after 5 minutes.

Delete time options

Value Behavior
not set (null) Uses default from config (24 hours)
false Disable auto-delete for this notification
N (integer) Delete after N minutes

Example with disabled auto-delete:

class ImportantNotification extends TelegramCleanupNotification
{
    public function __construct(protected string $text)
    {
        $this->deleteAfterMinutes = false; // Never delete
        $this->message = $text;
    }

    public function toTelegram(object $notifiable): TelegramMessage
    {
        return TelegramMessage::create()->content($this->text);
    }
}

3. Run cleanup command

Add to your scheduler (routes/console.php):

use Illuminate\Support\Facades\Schedule;

Schedule::command('telegram:messages:delete-expired')
    ->everyFiveMinutes()
    ->withoutOverlapping();

Manual registration

If sending messages without notifications:

use Rud99\TelegramCleanup\TelegramMessageCleanupService;

$service = app(TelegramMessageCleanupService::class);

$service->registerForDeletion(
    chatId: $chatId,
    messageId: $messageId,
    deleteAfterMinutes: 30,
    meta: ['type' => 'manual']
);

Commands

Delete expired messages

# Delete expired messages
php artisan telegram:messages:delete-expired

# Preview without deleting
php artisan telegram:messages:delete-expired --dry-run

# Limit number of deletions
php artisan telegram:messages:delete-expired --limit=100

# Delete only from specific chat
php artisan telegram:messages:delete-expired --chat-id=123456789

Prune old records

Remove old deleted message records from the database:

php artisan telegram:messages:prune

You can schedule it in routes/console.php:

Schedule::command('telegram:messages:prune')
    ->weekly()
    ->withoutOverlapping();

License

MIT