Skip to content

Commit 2b2214d

Browse files
committed
"Added webhook support to bot, updated config to include webhook URL, and refactored bot start logic to use webhook in production environment"
1 parent 7ac1b9c commit 2b2214d

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/bot/index.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,32 @@ export class CopBot {
2727
return CopBot.instance;
2828
}
2929
async start() {
30-
try {
31-
const port = Config.port || 3000;
30+
const port = Config.port || 3000;
31+
const web_hook = Config.web_hook;
32+
const isProduction = Config.environment === 'production';
33+
if (isProduction) {
3234
const server = http.createServer(webhookCallback(this._bot, 'http'));
3335
server.listen(port, '0.0.0.0', () => {
3436
console.log(`Bot started on port ${port}`);
3537
});
36-
await this._bot.start({
37-
onStart: (botInfo) => {
38-
console.log(`Bot started successfully! Username: ${botInfo.username}`);
39-
},
40-
});
41-
} catch (error) {
42-
console.error('Error starting the bot:', error);
43-
process.exit(1); // Exit the process if the bot fails to start
38+
try {
39+
await this._bot.api.setWebhook(`${web_hook}`);
40+
console.log(`Webhook set successfully to: ${web_hook}`);
41+
} catch (error) {
42+
console.error('Error setting webhook:', error);
43+
process.exit(1);
44+
}
45+
} else {
46+
try {
47+
await this._bot.start({
48+
onStart: (botInfo) => {
49+
console.log(`Bot started in long-polling mode! Username: ${botInfo.username}`);
50+
},
51+
});
52+
} catch (error) {
53+
console.error('Error starting bot in long-polling mode:', error);
54+
process.exit(1); // Exit if the bot fails to start
55+
}
4456
}
4557
}
4658
@Catch()

src/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ class Config {
66
public environment: 'development' | 'production';
77
public database: DatabaseConfig;
88
public port: number;
9+
public web_hook: string;
910

1011
private constructor() {
1112
// Ensure that the token is available in the environment
1213
const token = process.env.TELEGRAM_BOT_TOKEN;
14+
const web_hook = process.env.WEB_HOOK!;
1315
if (!token) {
1416
throw new Error('Telegram bot token is missing. Please set TELEGRAM_BOT_TOKEN in the environment.');
1517
}
@@ -27,6 +29,7 @@ class Config {
2729
this.token = token;
2830
this.environment = environment;
2931
this.port = Number(port);
32+
this.web_hook = web_hook;
3033
// Initialize the database configuration
3134
this.database = {
3235
user: dbUser,

src/types/ResponseTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface BotConfig {
1111
token: string;
1212
environment: 'development' | 'production';
1313
port: number;
14+
web_hook: string;
1415
database: DatabaseConfig;
1516
}
1617
export interface ErrorResponse {

0 commit comments

Comments
 (0)