Files
2026-09-01 07:49:48 -03:00

124 lines
4.0 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\TelegramUser;
use App\Services\TelegramService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
/**
* Controller for managing Telegram bot integration.
* Each user can configure their own Telegram bot for receiving insights.
*/
class TelegramController extends Controller
{
/**
* Display the Telegram configuration page.
*/
public function index(): View
{
/** @var \App\Models\User $user */
$user = Auth::user();
/** @var TelegramUser|null $telegramUser */
$telegramUser = $user->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.'));
}
}