telegramUsers()->first(); return view('admin.telegram.index', compact('telegramUser')); } /** * Connect/configure the user's Telegram bot. */ public function connect(Request $request): RedirectResponse { $validated = $request->validate([ 'bot_token' => ['required', 'string', 'max:255'], 'bot_username' => ['nullable', 'string', 'max:255'], 'chat_id' => ['required', 'string', 'max:255'], 'telegram_username' => ['nullable', 'string', 'max:255'], ]); /** @var \App\Models\User $user */ $user = Auth::user(); // Test the bot connection before saving $telegramService = new TelegramService($validated['bot_token']); $testResult = $telegramService->sendMessage( $validated['chat_id'], '๐Ÿค– *Annunk Bot Connected!*' . "\n\n" . 'โœ… Your Telegram bot has been successfully connected to Annunk.' . "\n\n" . 'You will now receive learning insights here.' ); if (!$testResult) { return back()->with('error', __( 'Failed to connect. Please check your Bot Token and Chat ID. ' . 'Make sure you have started a conversation with your bot first.' ))->withInput(); } TelegramUser::updateOrCreate( ['user_id' => $user->id], [ 'telegram_user_id' => $validated['chat_id'], 'bot_token' => $validated['bot_token'], 'bot_username' => $validated['bot_username'] ?? null, 'chat_id' => $validated['chat_id'], 'telegram_username' => $validated['telegram_username'] ?? null, 'is_active' => true, ] ); return back()->with('success', __('Telegram bot connected successfully! A test message was sent.')); } /** * Send a test message to verify the bot is working. */ public function test(): RedirectResponse { /** @var \App\Models\User $user */ $user = Auth::user(); /** @var TelegramUser|null $telegramUser */ $telegramUser = $user->telegramUsers()->first(); if (!$telegramUser || !$telegramUser->isFullyConfigured()) { return back()->with('error', __('Please configure your Telegram bot first.')); } $telegramService = new TelegramService($telegramUser->bot_token); $result = $telegramService->sendMessage( $telegramUser->chat_id, '๐Ÿงช *Annunk Test Message*' . "\n\n" . 'โœ… Your bot is working correctly!' . "\n\n" . 'You will receive learning insights according to your schedule.' ); if ($result) { return back()->with('success', __('Test message sent successfully! Check your Telegram.')); } return back()->with('error', __('Failed to send test message. Please check your bot configuration.')); } /** * Disconnect the user's Telegram bot. */ public function disconnect(): RedirectResponse { /** @var \App\Models\User $user */ $user = Auth::user(); $user->telegramUsers()->delete(); return back()->with('success', __('Telegram bot disconnected successfully.')); } }