Skip to content

Commit b369310

Browse files
committed
refactor(CopBot): Remove Catch decorator, update Bot constructor timeout, wrap webhookCallback in try-catch, and add return statement in message handling
1 parent 6b89efa commit b369310

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/bot/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Bot, webhookCallback } from 'grammy';
22
import type { Context } from 'grammy';
3-
import { Catch } from '../decorators/Catch';
43
import Config from '../config';
54
import { GenerateCommand } from './handlers/GenerateCommands';
65
import { GeneralCommands } from './commands/genearl/GeneralCommands';
@@ -15,7 +14,7 @@ export class CopBot {
1514
private static instance: CopBot;
1615
private _bot: Bot<Context>;
1716
private constructor() {
18-
this._bot = new Bot<Context>(Config.token, { client: { timeoutSeconds: 20 } });
17+
this._bot = new Bot<Context>(Config.token, { client: { timeoutSeconds: 10 } });
1918
}
2019
// Public method to get the singleton instance of CopBot
2120
public static getInstance(): CopBot {
@@ -53,7 +52,12 @@ export class CopBot {
5352
const app = express();
5453
app.use(express.json());
5554

56-
app.post(webhookPath, webhookCallback(this._bot, 'express'));
55+
app.post(webhookPath, (req, res) => {
56+
webhookCallback(this._bot, 'express')(req, res).catch((err) => {
57+
logger.error('Webhook processing error:', err);
58+
res.sendStatus(500);
59+
});
60+
});
5761

5862
const port = process.env.PORT || 3000;
5963
app.listen(port, async () => {
@@ -102,6 +106,7 @@ export class CopBot {
102106
const name = user.username ? `@${user.username}` : user.first_name;
103107
const responseMessage = `Dear ${name}, ask your question correctly.\nIf you want to know how to do this, read the article below:\ndontasktoask.ir`;
104108
await reply.textReply(responseMessage);
109+
return;
105110
}
106111
const command = messageText?.split(' ')[0]?.replace('/', '');
107112
if (command && ctx.message?.entities?.[0].type === 'bot_command') {

0 commit comments

Comments
 (0)