Skip to content

Commit d4d09eb

Browse files
committed
feat: Updated CopBot instance timeout, webhook URL, and error handling; added retries for webhook processing and logging for undefined message text
1 parent c2bcd65 commit d4d09eb

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/bot/index.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CopBot {
1414
private static instance: CopBot;
1515
private _bot: Bot<Context>;
1616
private constructor() {
17-
this._bot = new Bot<Context>(Config.token, { client: { timeoutSeconds: 10 } });
17+
this._bot = new Bot<Context>(Config.token, { client: { timeoutSeconds: 30 } });
1818
}
1919
// Public method to get the singleton instance of CopBot
2020
public static getInstance(): CopBot {
@@ -40,7 +40,7 @@ export class CopBot {
4040

4141
const isProduction = Config.environment === 'production';
4242
const webhookPath = `/bot/${Config.token}`;
43-
const webhookURL = `${Config.web_hook}${webhookPath}`;
43+
const webhookURL = `https://51a3-20-208-130-133.ngrok-free.app${webhookPath}`;
4444
const mode = isProduction ? 'webhook' : 'long-polling';
4545

4646
logger.info(`Environment: ${Config.environment}`);
@@ -49,14 +49,31 @@ export class CopBot {
4949

5050
if (isProduction) {
5151
try {
52+
await this._bot.api.deleteWebhook();
53+
this._bot.api.getUpdates({ offset: -1 });
5254
const app = express();
5355
app.use(express.json());
56+
app.post(webhookPath, async (req, res) => {
57+
res.sendStatus(200);
58+
const MAX_RETRIES = 3;
59+
let retryCount = 0;
5460

55-
app.post(webhookPath, (req, res) => {
56-
webhookCallback(this._bot, 'express')(req, res).catch((err) => {
57-
console.error('Webhook processing error:', err);
58-
res.sendStatus(500);
59-
});
61+
const processWebhook = async () => {
62+
try {
63+
// Process the webhook
64+
await webhookCallback(this._bot, 'express')(req, res);
65+
} catch (err) {
66+
if (retryCount < MAX_RETRIES) {
67+
retryCount++;
68+
const delay = Math.min(1000 * 2 ** retryCount, 30000);
69+
await new Promise((resolve) => setTimeout(resolve, delay));
70+
await processWebhook();
71+
} else {
72+
console.error('Max retries reached. Webhook processing failed.');
73+
}
74+
}
75+
};
76+
await processWebhook();
6077
});
6178

6279
const port = process.env.PORT || 3000;
@@ -84,7 +101,6 @@ export class CopBot {
84101
new GenerateCommand(this._bot).generate();
85102
this._bot.on('my_chat_member', (ctx) => this.handleJoinNewChat(ctx));
86103
this._bot.on('message', (ctx) => this.handleMessage(ctx));
87-
88104
this._bot.catch((err) => {
89105
console.error('Middleware error:', err);
90106
});
@@ -95,6 +111,10 @@ export class CopBot {
95111
@SaveUserData()
96112
@MessageValidator()
97113
async handleMessage(ctx: Context) {
114+
if (!ctx.message?.text) {
115+
console.warn('Message text is undefined');
116+
return;
117+
}
98118
console.log('ctx.message.text:', ctx.message?.text);
99119
console.log('ctx.chat', ctx.chat);
100120
console.log('ctx.message.from:', ctx.message?.from);
@@ -109,8 +129,11 @@ export class CopBot {
109129
}
110130
if (ctx.message?.entities) {
111131
const commandEntity = ctx.message.entities.find((entity) => entity.type === 'bot_command');
132+
console.log('commandEntity', commandEntity);
133+
112134
if (commandEntity) {
113135
const command = messageText?.split(' ')[0]?.replace('/', '')!;
136+
console.log('command', command);
114137
const handler = (GeneralCommands as any)[command] || (UserCommands as any)[command] || (AdminCommands as any)[command];
115138
if (typeof handler === 'function') {
116139
await handler(ctx);
@@ -123,6 +146,10 @@ export class CopBot {
123146

124147
@SaveUserData()
125148
async handleJoinNewChat(ctx: Context) {
149+
if (!ctx.message?.text) {
150+
console.warn('Message text is undefined');
151+
return;
152+
}
126153
const chat = ctx.chat!;
127154
if (!chat) {
128155
return;

0 commit comments

Comments
 (0)