forked from lnp2pBot/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
91 lines (82 loc) · 2.87 KB
/
Copy pathapp.ts
File metadata and controls
91 lines (82 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import 'dotenv/config';
import dns from 'node:dns';
import https from 'node:https';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { start } from './bot/start';
import { connect as mongoConnect } from './db_connect';
import { resubscribeInvoices } from './ln';
import { logger } from './logger';
import { Telegraf } from 'telegraf';
import { delay } from './util';
import { imageCache } from './util/imageCache';
import { createIndexes } from './models/indexes';
import { CommunityContext } from './bot/modules/community/communityContext';
import { startMonitoring } from './monitoring';
(async () => {
dns.setDefaultResultOrder('ipv4first');
process.on('unhandledRejection', e => {
if (e) {
logger.error(`Unhandled Rejection: ${e}`);
}
});
process.on('uncaughtException', e => {
if (e) {
logger.error(`Uncaught Exception: ${e}`);
}
});
const mongoose = mongoConnect();
mongoose.connection
.once('open', async () => {
try {
logger.info('Connected to Mongo instance.');
// Create database indexes for optimized queries
await createIndexes();
// Initialize image cache for faster order creation
await imageCache.initialize();
// Use configurable bot handler timeout, default to 60 seconds
const handlerTimeout = parseInt(
process.env.BOT_HANDLER_TIMEOUT || '60000',
);
let options: Partial<Telegraf.Options<CommunityContext>> = {
handlerTimeout,
};
const socksProxyHost = process.env.SOCKS_PROXY_HOST?.trim();
const telegramAgent = socksProxyHost
? (() => {
const proxyUrl = /^socks[45]?:\/\//i.test(socksProxyHost)
? socksProxyHost
: `socks5://${socksProxyHost}`;
logger.info(`Using SOCKS proxy for Telegram API: ${proxyUrl}`);
return new SocksProxyAgent(proxyUrl) as any;
})()
: (() => {
logger.info(
'Using direct HTTPS agent for Telegram API (IPv4 forced)',
);
return new https.Agent({
family: 4,
keepAlive: true,
timeout: 60000,
}) as any;
})();
options = {
...options,
telegram: {
agent: telegramAgent,
},
};
const bot = await start(String(process.env.BOT_TOKEN), options);
// Wait 1 seconds before try to resubscribe hold invoices
await delay(1000);
await resubscribeInvoices(bot);
// Start external monitoring heartbeats (non-blocking, fails gracefully)
startMonitoring();
} catch (error) {
logger.error(`Startup failed: ${error}`);
process.exit(1);
}
})
.on('error', (error: Error) =>
logger.error(`Error connecting to Mongo: ${error}`),
);
})();