Deploy Bot commited on
Commit
6fbf164
Β·
1 Parent(s): 4ab4872

Feat: Smart Launch Strategy (Webhook)

Browse files
Files changed (1) hide show
  1. src/main.js +42 -14
src/main.js CHANGED
@@ -547,23 +547,53 @@ bot.on('text', async (ctx, next) => {
547
  // Launch with Retry Logic and DNS Check
548
  // const dns = require('dns'); // Already imported at the top
549
 
 
550
  const launchBot = async () => {
551
  try {
552
- // Clear any stuck webhooks
553
- await bot.telegram.deleteWebhook({ drop_pending_updates: true });
554
- console.log('πŸ”„ Webhook tozalandi...');
555
-
556
- await bot.launch({
557
- dropPendingUpdates: true
558
- });
559
- console.log('βœ… Bot muvaffaqiyatli ishga tushdi!');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  } catch (err) {
561
- console.error('Botni ishga tushirishda xatolik:', err);
 
562
  if (err.description && err.description.includes('Conflict')) {
563
- console.warn("⚠️ DIQQAT: Botni boshqa joyda (masalan, kompyuteringizda terminalda) o'chirishingiz kerak! Serverda ikkita bot bitta token bilan ishlayapti.");
 
 
564
  }
565
- console.log('5 soniyadan keyin qayta urunib ko\'ramiz...');
566
- setTimeout(launchBot, 5000); // Retry after 5 seconds
 
 
567
  }
568
  };
569
 
@@ -573,7 +603,5 @@ launchBot();
573
  process.once('SIGINT', () => bot.stop('SIGINT'));
574
  process.once('SIGTERM', () => bot.stop('SIGTERM'));
575
 
576
- // Graceful stop handled above
577
-
578
  module.exports = bot;
579
 
 
547
  // Launch with Retry Logic and DNS Check
548
  // const dns = require('dns'); // Already imported at the top
549
 
550
+ // Launch Strategy (Polling vs Webhook)
551
  const launchBot = async () => {
552
  try {
553
+ const PORT = process.env.PORT || 7860; // Render/HF default
554
+ const DOMAIN = process.env.RENDER_EXTERNAL_URL || process.env.RAILWAY_PUBLIC_DOMAIN; // Auto-detect
555
+
556
+ if (DOMAIN) {
557
+ // WEBHOOK MODE (Production)
558
+ console.log(`πŸš€ Starting in WEBHOOK mode on port ${PORT}`);
559
+ console.log(`πŸ”— Domain: ${DOMAIN}`);
560
+
561
+ // Delete any existing webhook first (optional, but good for restart)
562
+ // await bot.telegram.deleteWebhook();
563
+
564
+ await bot.launch({
565
+ webhook: {
566
+ domain: DOMAIN,
567
+ port: PORT
568
+ },
569
+ dropPendingUpdates: true
570
+ });
571
+ console.log(`βœ… Webhook Active: ${DOMAIN}/telegraf/<token>`);
572
+ } else {
573
+ // POLLING MODE (Local / Dev)
574
+ console.log(`πŸš€ Starting in POLLING mode`);
575
+
576
+ // Clear any stuck webhooks
577
+ await bot.telegram.deleteWebhook({ drop_pending_updates: true });
578
+ console.log('πŸ”„ Webhook tozalandi (Polling uchun)...');
579
+
580
+ await bot.launch({
581
+ dropPendingUpdates: true
582
+ });
583
+ console.log('βœ… Bot (Polling) ishga tushdi!');
584
+ }
585
  } catch (err) {
586
+ console.error('❌ Botni ishga tushirishda xatolik:', err.message);
587
+
588
  if (err.description && err.description.includes('Conflict')) {
589
+ console.warn("⚠️ MUAZZAM DIQQAT: Bot allaqachon boshqa joyda ishlamoqda!");
590
+ console.warn("Agar bu server bo'lsa, demak lokal (kompyuterda) botni o'chirish kerak.");
591
+ console.warn("Admin panelda 'Stop' tugmasini bosib, qayta yuklang.");
592
  }
593
+
594
+ // Retry logic for Polling stability
595
+ console.log('⏳ 10 soniyadan keyin qayta urunib ko\'ramiz...');
596
+ setTimeout(launchBot, 10000); // 10s retry
597
  }
598
  };
599
 
 
603
  process.once('SIGINT', () => bot.stop('SIGINT'));
604
  process.once('SIGTERM', () => bot.stop('SIGTERM'));
605
 
 
 
606
  module.exports = bot;
607