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