Skip to content

Commit 0b7488f

Browse files
committed
feat: Added HTTP server and port configuration to CopBot, updated Config class to include port property, and modified BotConfig interface to include port
1 parent 261c6ce commit 0b7488f

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/bot/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AdminCommands } from './commands/admin/AdminCommands';
1111
import { BotReply } from '../utils/chat/BotReply';
1212
import { BotMiddleware } from './middleware/BotMiddleware';
1313
import { MessagesService } from '../service/messages';
14+
import * as http from 'http';
1415
export class CopBot {
1516
private static instance: CopBot;
1617
private _bot: Bot<Context>;
@@ -27,6 +28,15 @@ export class CopBot {
2728
}
2829
async start() {
2930
try {
31+
// Ensure the bot is listening on the correct port (Render provides the PORT environment variable)
32+
const port = Config.port;
33+
34+
// Use long-polling
35+
const server = http.createServer(webhookCallback(this._bot, 'http'));
36+
37+
server.listen(port, () => {
38+
console.log(`Bot started on port ${port}`);
39+
});
3040
await this._bot.start({
3141
onStart: (botInfo) => {
3242
console.log(`Bot started successfully! Username: ${botInfo.username}`);

src/config/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ class Config {
55
public token: string;
66
public environment: 'development' | 'production';
77
public database: DatabaseConfig;
8+
public port: number;
89

910
private constructor() {
1011
// Ensure that the token is available in the environment
1112
const token = process.env.TELEGRAM_BOT_TOKEN;
1213
if (!token) {
1314
throw new Error('Telegram bot token is missing. Please set TELEGRAM_BOT_TOKEN in the environment.');
1415
}
15-
16+
const port = process.env.PORT!;
1617
// Set the environment, defaulting to development if not set
1718
const environment: 'development' | 'production' = process.env.NODE_ENV === 'production' ? 'production' : 'development';
1819

@@ -25,6 +26,7 @@ class Config {
2526
const dbUrl = environment === 'production' ? process.env.DATABASE_URL! : process.env.DB_URL!;
2627
this.token = token;
2728
this.environment = environment;
29+
this.port = Number(port);
2830
// Initialize the database configuration
2931
this.database = {
3032
user: dbUser,

src/types/ResponseTypes.ts

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

0 commit comments

Comments
 (0)