baks-dev/users-table-telegram
Installation
composer require baks-dev/users-table-telegram
php bin/console baks:assets:install
First Use Case: Track User Work Hours
php artisan vendor:publish --provider="BaksDev\UsersTableTelegram\UsersTableTelegramServiceProvider" --tag="config"
config/users-table-telegram.php with your Telegram bot token and database settings.php artisan migrate
Key Classes to Explore
BaksDev\UsersTableTelegram\Models\UserWorkTime – Core model for tracking hours.BaksDev\UsersTableTelegram\Services\TelegramBotService – Handles Telegram integration.BaksDev\UsersTableTelegram\Http\Controllers\WorkTimeController – API endpoints for CRUD operations.Telegram Bot Integration
TelegramBotService to handle custom commands (e.g., /start, /clockin, /clockout).$bot->command('clockin', function ($message) {
$user = User::findOrFail($message->from->id);
$user->workTime()->create(['started_at' => now()]);
return "⏳ Clocked in!";
});
Laravel API Endpoints
WorkTimeController for RESTful operations:
Route::apiResource('work-times', \BaksDev\UsersTableTelegram\Http\Controllers\WorkTimeController::class);
app/Http/Requests/StoreWorkTimeRequest (published via --tag="requests").Scheduling Automatic Reports
$schedule->command('baks:generate-work-reports')->dailyAt('18:00');
BaksDev\UsersTableTelegram\Contracts\ReportGenerator for custom logic.Blade Integration (Admin Panel)
php artisan vendor:publish --provider="BaksDev\UsersTableTelegram\UsersTableTelegramServiceProvider" --tag="views"
resources/views/users-table-telegram/reports.blade.php to display data in your admin dashboard.Telegram Bot Token Leaks
config/users-table-telegram.php. Use Laravel’s .env:
TELEGRAM_BOT_TOKEN=your_token_here
Timezone Mismatches
config/app.php and config/database.php use the same timezone as your Telegram bot’s server.\Carbon\Carbon::now()->timezone('Europe/Moscow')->format('Y-m-d H:i:s');
Migration Conflicts
user_work_times table manually, reset migrations:
php artisan migrate:fresh --env=testing
id, created_at) in database/migrations/.Rate Limiting
TelegramBotService:
$cacheKey = "telegram_{$message->chat->id}_{$command}";
return Cache::remember($cacheKey, now()->addMinutes(5), fn() => $this->processCommand($message));
Log Telegram Webhook Payloads
// app/Http/Middleware/LogTelegramWebhook.php
public function handle($request, Closure $next) {
\Log::debug('Telegram Webhook', ['payload' => $request->all()]);
return $next($request);
}
app/Http/Kernel.php.Test Locally with ngrok
ngrok http 8000
WEBHOOK_URL in config to https://your-ngrok-url.ngrok.io/telegram-webhook.Common SQL Queries
$activeUsers = UserWorkTime::whereDate('started_at', today())
->whereNull('ended_at')
->with('user')
->get();
$hours = UserWorkTime::whereBetween('started_at', [now()->startOfWeek(), now()->endOfWeek()])
->sum(\DB::raw('TIMESTAMPDIFF(MINUTE, started_at, ended_at) / 60'));
Custom Fields
user_work_times via migration, then extend the model:
// app/Models/UserWorkTime.php
protected $casts = [
'is_remote' => 'boolean',
'project_id' => 'integer',
];
Webhook Validation
TelegramBotService::validateWebhook() to add custom checks:
public function validateWebhook(array $payload): bool {
return parent::validateWebhook($payload) &&
$payload['message']['chat']['id'] === config('users-table-telegram.allowed_chat_id');
}
Export Formats
BaksDev\UsersTableTelegram\Contracts\ExporterInterface for CSV/Excel:
class CustomExporter implements ExporterInterface {
public function export(UserWorkTime $workTime): string {
return "Custom format: {$workTime->started_at} - {$workTime->ended_at}";
}
}
config/users-table-telegram.php:
'exporter' => \App\Services\CustomExporter::class,
Event Listeners
WorkTimeStarted/WorkTimeEnded events:
// app/Listeners/SendSlackNotification.php
public function handle(WorkTimeStarted $event) {
Slack::send("User {$event->user->name} started work at {$event->workTime->started_at}");
}
EventServiceProvider:
protected $listen = [
\BaksDev\UsersTableTelegram\Events\WorkTimeStarted::class => [
SendSlackNotification::class,
],
];
How can I help you explore Laravel packages today?