PLUTON\igor.kreyda commited on
Commit
49f3cdf
·
1 Parent(s): 8638b73

fix: implement manual DNS fallback for api.telegram.org

Browse files
Files changed (1) hide show
  1. app/bot/bot.js +36 -5
app/bot/bot.js CHANGED
@@ -11,12 +11,43 @@ function setupBot(token, webAppUrl) {
11
  throw new Error('BOT_TOKEN is missing!');
12
  }
13
 
 
 
 
 
14
  const https = require('https');
15
- const bot = new Telegraf(token, {
16
- telegram: {
17
- agent: new https.Agent({ family: 4 })
18
- }
19
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  // Initial setup for the menu button
22
  // Note: setChatMenuButton might need to be called on specific user interaction or globally once logic is decided.
 
11
  throw new Error('BOT_TOKEN is missing!');
12
  }
13
 
14
+ // --- CUSTOM DNS RESOLUTION FIX ---
15
+ // Hugging Face Spaces often fails to resolve api.telegram.org via system DNS.
16
+ // We will manually resolve it using Google DNS (8.8.8.8) and force Telegraf to use the IP.
17
+
18
  const https = require('https');
19
+ const dns = require('dns').promises;
20
+
21
+ const botOptions = {};
22
+
23
+ // Only apply this fix in production (on Hugging Face)
24
+ if (process.env.NODE_ENV === 'production') {
25
+ console.log('[Bot] Production mode detected. Configuring custom DNS resolver...');
26
+
27
+ const agent = new https.Agent({
28
+ lookup: async (hostname, options, callback) => {
29
+ try {
30
+ // Start with standard lookup
31
+ const { address, family } = await dns.lookup(hostname, { family: 4 });
32
+ return callback(null, address, family);
33
+ } catch (err) {
34
+ // Fallback: Hardcoded IP for api.telegram.org if DNS fails
35
+ // (This is a dirty persistent fix, but necessary if DNS is blocked)
36
+ if (hostname === 'api.telegram.org') {
37
+ console.warn('[Bot] DNS lookup failed. Using fallback IP for Telegram.');
38
+ // 149.154.167.220 is one of api.telegram.org IPs
39
+ return callback(null, '149.154.167.220', 4);
40
+ }
41
+ return callback(err);
42
+ }
43
+ }
44
+ });
45
+
46
+ botOptions.telegram = { agent };
47
+ }
48
+
49
+ // Pass botOptions to Telegraf constructor
50
+ const bot = new Telegraf(token, botOptions);
51
 
52
  // Initial setup for the menu button
53
  // Note: setChatMenuButton might need to be called on specific user interaction or globally once logic is decided.