Skip to content

Commit 2f38306

Browse files
committed
refactor(CopBot): remove timeoutSeconds option, add retry mechanism for setting webhook, and update command parsing logic
1 parent 4eb9b47 commit 2f38306

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/bot/index.ts

Lines changed: 34 additions & 12 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: 30 } });
17+
this._bot = new Bot<Context>(Config.token);
1818
}
1919
// Public method to get the singleton instance of CopBot
2020
public static getInstance(): CopBot {
@@ -80,22 +80,44 @@ export class CopBot {
8080
let webhookInfo = await this._bot.api.getWebhookInfo();
8181
console.log(`Webhook Info: `, webhookInfo);
8282
logger.info(`Current Webhook: ${webhookInfo.url}`);
83-
if (!webhookInfo.url) {
84-
logger.info('Setting webhook...');
85-
await this._bot.api.setWebhook(webhookURL);
86-
webhookInfo = await this._bot.api.getWebhookInfo();
87-
console.log(`Updated Webhook Info: `, webhookInfo);
88-
logger.info(`Webhook set: ${webhookInfo.url}`);
89-
}
83+
const MAX_RETRIES = 5;
84+
let retries = 0;
85+
86+
const trySetWebhook = async () => {
87+
try {
88+
if (!webhookInfo.url) {
89+
logger.info('Webhook is not set. Trying to set the webhook...');
90+
await this._bot.api.setWebhook(webhookURL);
91+
webhookInfo = await this._bot.api.getWebhookInfo();
92+
console.log(`Updated Webhook Info: `, webhookInfo);
93+
logger.info(`Webhook set: ${webhookInfo.url}`);
94+
} else {
95+
logger.info('Webhook is already set.');
96+
}
97+
} catch (error: any) {
98+
retries++;
99+
logger.error(`Error setting webhook (Attempt ${retries}): ${error.message}`);
100+
if (retries < MAX_RETRIES) {
101+
const delay = Math.min(1000 * 2 ** retries, 30000); // Exponential backoff
102+
logger.info(`Retrying in ${delay / 1000} seconds...`);
103+
await new Promise((resolve) => setTimeout(resolve, delay));
104+
await trySetWebhook();
105+
} else {
106+
logger.error('Max retries reached. Could not set webhook.');
107+
process.exit(1); // Exit after maximum retries reached
108+
}
109+
}
110+
};
111+
112+
await trySetWebhook();
113+
await startBot(mode);
90114
});
91-
await startBot(mode);
92115
} catch (err: any) {
93116
console.error('Error setting up webhook:', err);
94117
process.exit(1);
95118
}
96119
} else {
97120
try {
98-
await this._bot.api.deleteWebhook();
99121
await startBot(mode);
100122
} catch (err: any) {
101123
console.error('Error during long-polling mode:', err);
@@ -136,9 +158,9 @@ export class CopBot {
136158
if (ctx.message?.entities) {
137159
const commandEntity = ctx.message.entities.find((entity) => entity.type === 'bot_command');
138160
console.log('commandEntity', commandEntity);
139-
140161
if (commandEntity) {
141-
const command = messageText?.split(' ')[0]?.replace('/', '')!;
162+
let command = messageText?.split(' ')[0]?.replace('/', '')!;
163+
command = command.includes('@') ? command.split('@')[0] : command;
142164
console.log('command', command);
143165
const handler = (GeneralCommands as any)[command] || (UserCommands as any)[command] || (AdminCommands as any)[command];
144166
if (typeof handler === 'function') {

0 commit comments

Comments
 (0)