Cara Membuat Bot Telegram Pengenal Wajah: Panduan Lengkap + AI!

Face Recognition Telegram Bot
Bot Telegram untuk deteksi dan pengenalan wajah menggunakan API Face Recognition.
🚀 Fitur Utama
1. Deteksi Wajah
- Upload foto untuk mendeteksi wajah
- Identifikasi wajah yang sudah terdaftar
- Confidence score untuk setiap deteksi
- Analisis AI dengan Gemini (optional)
2. Manajemen Database Wajah
- Tambah wajah baru ke database
- Update informasi wajah
- Lihat daftar semua wajah
- Lihat detail informasi wajah
3. Video Processing
- Upload video untuk deteksi wajah
- Async processing dengan progress tracking
- Cancel processing yang sedang berjalan
- Download hasil processing
4. Stream Processing
- Proses webcam (lokal/remote)
- Support RTSP stream
- Support HTTP/HTTPS stream
- Adjustable duration dan FPS
📦 Instalasi
Prerequisites
# Node.js 14+ node --version # NPM packages npm install node-telegram-bot-api form-data
Setup
# Clone atau copy script git clone <repository-url> cd face-recognition-bot # Install dependencies npm install # Set environment variables export TELEGRAM_BOT_TOKEN="your-bot-token" # Run bot node bot.js
⚙️ Konfigurasi
Environment Variables
# Required export TELEGRAM_BOT_TOKEN="7587121696:AAHwkAhyiNI5c5Y62zFAMLtRLmBGULZ8BWw" # API Server (default sudah di-set) # Tidak perlu diubah jika server di 10.122.25.138:8192
Config dalam Script
const API_BASE_URL = 'http://10.122.25.138:8192' // Face Recognition API const BOT_NAME = 'FaceRecBot' // Nama bot const TYPING_DELAY = 1000 // Delay typing indicator
📖 Panduan Penggunaan
Perintah Dasar
/start
Memulai bot dan menampilkan welcome message
/start
/help
atau /menu
Menampilkan panduan lengkap
/help
/status
Cek status API server
/status
Deteksi Wajah
Deteksi Sederhana
- Kirim foto ke bot
- Bot akan memproses dan mendeteksi wajah
- Hasil berisi: nama, confidence, info (jika ada)
Deteksi dengan AI Analysis
- Kirim foto dengan caption:
analyze
- Bot akan menggunakan Gemini AI untuk analisis tambahan
[Kirim foto] Caption: analyze
Deteksi dengan Pertanyaan Custom
[Kirim foto] Caption: analyze - Apa yang dilakukan orang ini?
Manajemen Wajah
Tambah Wajah Baru
# Method 1: Tanpa info /addface John Doe [Kirim foto wajah John Doe] # Method 2: Dengan info /addface John Doe | Manager IT Department [Kirim foto wajah John Doe]
Update Info Wajah
/updateinfo John Doe | Senior Manager IT Department
Lihat Semua Wajah
/listfaces
Info Detail Wajah
/faceinfo John Doe
Video Processing
Upload dan Proses Video
- Kirim file video ke bot
- Bot akan upload dan memproses secara async
- Dapatkan session ID untuk tracking
Monitor Progress
/videostatus <session_id>
Cancel Processing
/videocancel <session_id>
Dapatkan Hasil
/videoresults <session_id>
Stream Processing
Webcam Lokal
# Default: 60 detik, 1 FPS /stream 0 # Custom: 30 detik, 2 FPS /stream 0 30 2
RTSP Stream
/stream rtsp://192.168.1.100:554/stream # Dengan custom duration dan FPS /stream rtsp://192.168.1.100:554/stream 120 2
HTTP Stream
/stream http://camera-ip/stream.mjpeg
Maintenance
Regenerate Embeddings
/regenerate
🔧 API Integration
Endpoints yang Digunakan
Endpoint Method Fungsi /health
GET Health check /faces
GET List wajah /faces/add
POST Tambah wajah /faces/update/<name>
POST Update info /process
POST Proses gambar /video/upload
POST Upload video /video/status/<id>
GET Status video /video/cancel/<id>
POST Cancel video /video/results/<id>
GET Hasil video /stream/process
POST Proses stream /regenerate
POST Regenerate embeddings Request Format
Tambah Wajah
FormData: - image: file (jpg/png) - name: string - info: string (optional)
Proses Gambar
FormData: - image: file (jpg/png) - analyze_with_gemini: boolean - custom_question: string (optional)
Upload Video
FormData: - video: file (mp4/avi/mov/mkv) - fps_process: integer (default: 1) - process_async: boolean (default: true)
Proses Stream
JSON: { "stream_url": "0" | "rtsp://..." | "http://...", "duration": 60, "fps_process": 1 }
Response Format
Deteksi Wajah
{ "face_count": 2, "faces": [ { "name": "John Doe", "confidence": 0.95, "bbox": [100, 50, 200, 150], "info": "Manager IT" } ], "result_url": "/images/results/result_xxx.jpg", "gemini_analysis": "..." }
Video Upload
{ "status": "success", "message": "Video berhasil diupload", "session_id": "abc-123-def-456" }
Video Status
{ "status": "processing", "progress": 45, "message": "Memproses frame 450/1000", "frame_number": 450, "processed_count": 45 }
📝 Error Handling
Rate Limiting
- User dibatasi 1 pesan per 2 detik
- Mencegah spam dan overload
API Errors
- Connection timeout → Retry otomatis
- Invalid response → Error message ke user
- Server down → Status warning
File Handling
- Max file size: 100MB (Telegram limit)
- Supported formats:
- Images: JPG, PNG, JPEG
- Videos: MP4, AVI, MOV, MKV
🐛 Troubleshooting
Bot Tidak Respon
# Cek status bot ps aux | grep node # Cek log tail -f logs/bot.log # Restart bot pkill -f "node bot.js" node bot.js
API Connection Failed
# Test API curl http://10.122.25.138:8192/health # Cek network ping 10.122.25.138 # Test port nc -zv 10.122.25.138 8192
Video Processing Stuck
# Gunakan cancel command /videocancel <session_id> # Atau restart API server jika perlu
📊 Logging
Log Location
./logs/bot.log
Log Format
[2025-10-05T10:30:45.123Z] 🖼️ Photo from John [2025-10-05T10:30:46.456Z] ✅ Response sent to John
Log Monitoring
# Real-time monitoring tail -f logs/bot.log # Search errors grep "❌" logs/bot.log # Count messages grep "Photo from" logs/bot.log | wc -l
🔐 Security
API Security
- API hanya di internal network (10.122.25.138)
- Tidak expose ke public internet
- Rate limiting untuk mencegah abuse
Bot Security
- Token disimpan di environment variable
- Tidak di-hardcode di script
- Log tidak menyimpan token
Data Privacy
- Gambar hasil disimpan sementara
- Video sessions dibersihkan otomatis
- User data tidak disimpan permanen
🚀 Production Deployment
PM2 (Recommended)
# Install PM2 npm install -g pm2 # Start bot pm2 start bot.js --name face-recognition-bot # Auto restart on reboot pm2 startup pm2 save # Monitor pm2 monit # Logs pm2 logs face-recognition-bot
Systemd Service
[Unit] Description=Face Recognition Telegram Bot After=network.target [Service] Type=simple User=youruser WorkingDirectory=/path/to/bot Environment="TELEGRAM_BOT_TOKEN=your-token" ExecStart=/usr/bin/node bot.js Restart=always [Install] WantedBy=multi-user.target
Docker
FROM node:16-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . ENV TELEGRAM_BOT_TOKEN="" CMD ["node", "bot.js"]
📈 Monitoring
Health Check
# Bot health curl http://10.122.25.138:8192/health # Check response time time curl http://10.122.25.138:8192/health
Performance Metrics
- Response time: < 2 detik (image)
- Video processing: ~1 menit per 100 frames
- Stream processing: Real-time
🔄 Updates & Maintenance
Update Dependencies
npm update npm audit fix
Backup
# Backup logs cp -r logs logs_backup_$(date +%Y%m%d) # Backup script cp bot.js bot.js.backup
Clear Cache
# Clear old sessions rm -rf video_sessions/* # Clear old logs (older than 7 days) find logs/ -name "*.log" -mtime +7 -delete
📞 Support
Debugging Mode
Enable verbose logging di script:
const DEBUG = true
Version: 1.0.0
Last Updated: October 2025
Status: Production Ready ✅