QR code login, user analytics, Telegram groups support, or even a chatbot with memory - Gianguyen1234/app-doc GitHub Wiki

Let’s implement these advanced features for your Laravel Telegram Bot πŸš€


1️⃣ QR Code Login (Laravel + Telegram) πŸ“²

βœ… How It Works:

  • A user scans a QR code β†’ Redirects them to Telegram
  • The bot sends them a one-time login link
  • Laravel verifies their identity

Step 1: Install QR Code Package

Run this command in your Laravel project:

composer require simplesoftwareio/simple-qrcode

Step 2: Generate QR Code in Laravel

Edit your LoginController.php:

use SimpleSoftwareIO\QrCode\Facades\QrCode;

public function generateQRCode()
{
    $loginUrl = "https://t.me/YOUR_BOT_USERNAME?start=login_" . uniqid();

    return QrCode::size(300)->generate($loginUrl);
}

βœ… Usage:

Call /api/generate-qr β†’ Returns a QR code image for users to scan πŸ“·


Step 3: Handle Login in Telegram

Modify routes/web.php:

Route::get('/telegram/webhook', function (Request $request, TelegramService $telegram) {
    $update = $request->all();

    if (isset($update['message']['text']) && str_contains($update['message']['text'], 'login_')) {
        $telegram->sendMessage("βœ… Login successful! You are now authenticated.");
    }

    return response()->json(['status' => 'ok']);
});

βœ… Now users can log in just by scanning a QR code! πŸŽ‰


2️⃣ User Analytics (Track Users & Messages) πŸ“Š

βœ… How It Works:

  • Track total users
  • Count messages sent per user
  • Store user activity logs

Step 1: Create Database Table

Run this command:

php artisan make:migration create_telegram_users_table

Then, edit database/migrations/xxxx_xx_xx_xxxxxx_create_telegram_users_table.php:

Schema::create('telegram_users', function (Blueprint $table) {
    $table->id();
    $table->bigInteger('telegram_id')->unique();
    $table->string('name');
    $table->integer('messages_sent')->default(0);
    $table->timestamps();
});

Run the migration:

php artisan migrate

Step 2: Track Users & Messages

Modify TelegramService.php:

use App\Models\TelegramUser;

public function trackUser($telegramData)
{
    $user = TelegramUser::updateOrCreate(
        ['telegram_id' => $telegramData['id']],
        ['name' => $telegramData['first_name']]
    );

    $user->increment('messages_sent');
}

βœ… Now, every user & message is tracked in your database! πŸ“Š


Step 3: Show Analytics Dashboard

Modify AdminController.php:

public function analytics()
{
    return response()->json([
        'total_users' => TelegramUser::count(),
        'total_messages' => TelegramUser::sum('messages_sent')
    ]);
}

βœ… Now, you can get user analytics in /admin/analytics


3️⃣ Telegram Groups Support πŸ‘₯

βœ… How It Works:

  • Your bot can join groups
  • Respond to commands in groups
  • Track group messages

Step 1: Allow Your Bot in Groups

1️⃣ Open [@BotFather](https://t.me/BotFather)
2️⃣ Send /setprivacy β†’ Select "Disabled"
3️⃣ Add your bot to a group


Step 2: Detect Group Messages

Modify TelegramService.php:

public function handleGroupMessage($update)
{
    if (isset($update['message']['chat']['type']) && $update['message']['chat']['type'] === 'group') {
        $this->sendMessage("πŸ‘‹ Hello group members! I'm here to assist.");
    }
}

βœ… Now, your bot can interact with groups! πŸŽ‰


4️⃣ AI Chatbot with Memory 🧠

βœ… How It Works:

  • Stores past conversations
  • Remembers user context
  • Uses GPT-4 for better responses

Step 1: Add Conversation Storage

Modify database/migrations:

Schema::create('telegram_conversations', function (Blueprint $table) {
    $table->id();
    $table->bigInteger('telegram_id');
    $table->text('conversation');
    $table->timestamps();
});

Run the migration:

php artisan migrate

Step 2: Store Conversations

Modify TelegramService.php:

use App\Models\TelegramConversation;

public function rememberChat($telegramData, $message)
{
    TelegramConversation::create([
        'telegram_id' => $telegramData['id'],
        'conversation' => $message
    ]);
}

βœ… Now, your bot remembers previous chats! 🧠


Step 3: Fetch Conversation History

Modify TelegramService.php:

public function getChatHistory($telegramId)
{
    return TelegramConversation::where('telegram_id', $telegramId)
        ->latest()
        ->limit(5)
        ->pluck('conversation')
        ->toArray();
}

βœ… Now, AI can respond with context! πŸš€


πŸ”₯ Final Test

βœ… Scan QR Code β†’ Instant Login
βœ… Admin Panel β†’ Track Users & Messages
βœ… Bot Joins Groups β†’ Responds in Chats
βœ… AI Bot with Memory β†’ Remembers Context