File tree Expand file tree Collapse file tree 3 files changed +26
-10
lines changed
Expand file tree Collapse file tree 3 files changed +26
-10
lines changed Original file line number Diff line number Diff 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 ( )
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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}
1617export interface ErrorResponse {
You can’t perform that action at this time.
0 commit comments