Spaces:
Paused
Paused
Deploy Bot commited on
Commit Β·
6fbf164
1
Parent(s): 4ab4872
Feat: Smart Launch Strategy (Webhook)
Browse files- 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 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
} catch (err) {
|
| 561 |
-
console.error('Botni ishga tushirishda xatolik:', err);
|
|
|
|
| 562 |
if (err.description && err.description.includes('Conflict')) {
|
| 563 |
-
console.warn("β οΈ DIQQAT:
|
|
|
|
|
|
|
| 564 |
}
|
| 565 |
-
|
| 566 |
-
|
|
|
|
|
|
|
| 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 |
|