Skip to content

Commit 12b9d16

Browse files
committed
feat: add medishn/logger and remove gland-logger adnd add error handling with better logger
1 parent 08c7be0 commit 12b9d16

4 files changed

Lines changed: 44 additions & 45 deletions

File tree

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"build": "tsc",
2222
"clean": "rm -rf dist",
2323
"main": "npm run build && node --env-file=.env dist/app.js",
24+
"start:production": "NODE_ENV=production node --trace-uncaught -r ts-node/register --env-file=.env src/app.ts",
2425
"start": "NODE_ENV=development node --trace-uncaught -r ts-node/register --env-file=.env src/app.ts",
25-
"start:deploy": "NODE_ENV=production node --trace-uncaught -r ts-node/register src/app.ts"
26+
"start:deploy": "npm run start:production"
2627
},
2728
"repository": {
2829
"type": "git",
@@ -47,7 +48,7 @@
4748
},
4849
"homepage": "https://github.com/CodeModule-ir/cop#readme",
4950
"dependencies": {
50-
"@medishn/gland-logger": "^1.0.5",
51+
"@medishn/logger": "^2.0.1",
5152
"express": "^4.21.1",
5253
"grammy": "^1.27.0",
5354
"pg": "^8.13.1"

src/bot/index.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export class CopBot {
7878
app.listen(port, async () => {
7979
logger.info(`Webhook server running on port ${port}`);
8080
let webhookInfo = await this._bot.api.getWebhookInfo();
81-
console.log(`Webhook Info: `, webhookInfo);
8281
logger.info(`Current Webhook: ${webhookInfo.url}`);
8382
const MAX_RETRIES = 5;
8483
let retries = 0;
@@ -89,7 +88,6 @@ export class CopBot {
8988
logger.info('Webhook is not set. Trying to set the webhook...');
9089
await this._bot.api.setWebhook(webhookURL);
9190
webhookInfo = await this._bot.api.getWebhookInfo();
92-
console.log(`Updated Webhook Info: `, webhookInfo);
9391
logger.info(`Webhook set: ${webhookInfo.url}`);
9492
} else {
9593
logger.info('Webhook is already set.');
@@ -139,37 +137,39 @@ export class CopBot {
139137
@SaveUserData()
140138
@MessageValidator()
141139
async handleMessage(ctx: Context) {
142-
if (!ctx.message?.text) {
143-
console.warn('Message text is undefined');
144-
return;
145-
}
146-
console.log('ctx.message.text:', ctx.message?.text);
147-
console.log('ctx.chat', ctx.chat);
148-
console.log('ctx.message.from:', ctx.message?.from);
149-
const messageText = ctx.message?.text?.toLowerCase().trim();
150-
const reply = new BotReply(ctx);
151-
const user = ctx.message?.reply_to_message?.from;
152-
if (messageText === 'ask' && user) {
153-
const name = user.username ? `@${user.username}` : user.first_name;
154-
const responseMessage = `Dear ${name}, ask your question correctly.\nIf you want to know how to do this, read the article below:\ndontasktoask.ir`;
155-
await reply.textReply(responseMessage);
156-
return;
157-
}
158-
if (ctx.message?.entities) {
159-
const commandEntity = ctx.message.entities.find((entity) => entity.type === 'bot_command');
160-
console.log('commandEntity', commandEntity);
161-
if (commandEntity) {
162-
let command = messageText?.split(' ')[0]?.replace('/', '')!;
163-
command = command.includes('@') ? command.split('@')[0] : command;
164-
console.log('command', command);
165-
const handler = (GeneralCommands as any)[command] || (UserCommands as any)[command] || (AdminCommands as any)[command];
166-
if (typeof handler === 'function') {
167-
await handler(ctx);
168-
return;
140+
try {
141+
if (!ctx.message?.text) {
142+
logger.warn('Message text is undefined');
143+
return;
144+
}
145+
const messageText = ctx.message?.text?.toLowerCase().trim();
146+
const reply = new BotReply(ctx);
147+
const user = ctx.message?.reply_to_message?.from;
148+
149+
if (messageText === 'ask' && user) {
150+
const name = user.username ? `@${user.username}` : user.first_name;
151+
const responseMessage = `Dear ${name}, ask your question correctly.\nIf you want to know how to do this, read the article below:\ndontasktoask.ir`;
152+
await reply.textReply(responseMessage);
153+
}
154+
155+
if (ctx.message?.entities) {
156+
const commandEntity = ctx.message.entities.find((entity) => entity.type === 'bot_command');
157+
if (commandEntity) {
158+
let command = messageText?.split(' ')[0]?.replace('/', '')!;
159+
logger.debug(`Processing command: ${command} `);
160+
command = command.includes('@') ? command.split('@')[0] : command;
161+
const handler = (GeneralCommands as any)[command] || (UserCommands as any)[command] || (AdminCommands as any)[command];
162+
if (typeof handler === 'function') {
163+
await handler(ctx);
164+
return;
165+
}
169166
}
170167
}
168+
} catch (err: any) {
169+
logger.error('Error in handleMessage:', err.message);
170+
const reply = new BotReply(ctx);
171+
await reply.textReply('An unexpected error occurred.');
171172
}
172-
return;
173173
}
174174

175175
@SaveUserData()
@@ -184,7 +184,6 @@ export class CopBot {
184184
}
185185
const reply = new BotReply(ctx);
186186
const from = ctx.from;
187-
console.log(`Bot added to group ${chat.title} by ${from?.username}`);
188187
const message = `
189188
Hello ${ctx.chat?.title}!
190189
First of all, thanks to @${from?.username!} for inviting me to this awesome group!

src/utils/logger.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
import { Logger } from "@medishn/gland-logger";
1+
import { Logger } from '@medishn/logger';
22

33
class Log {
44
private logger: Logger;
55

66
constructor() {
77
// Initialize the logger with the specified configuration
88
this.logger = new Logger({
9-
transports: ["console"],
10-
format: "locale",
9+
transports: ['console'],
1110
});
1211
}
1312

1413
// Warn method to log warnings
1514
warn(message: string, category?: string) {
16-
this.logger.log({ message, category, level: "warn" });
15+
this.logger.log({ message, category, level: 'warn' });
1716
}
1817

1918
// Error method to log errors
2019
error(message: string, category?: string) {
21-
this.logger.log({ message, category, level: "error" });
20+
this.logger.log({ message, category, level: 'error' });
2221
}
2322

2423
// Info method to log informational messages
2524
info(message: string, category?: string) {
26-
this.logger.log({ message, category, level: "info" });
25+
this.logger.log({ message, category, level: 'info' });
2726
}
2827

2928
// Debug method to log debugging messages
3029
debug(message: string, category?: string) {
31-
this.logger.log({ message, category, level: "debug" });
30+
this.logger.log({ message, category, level: 'debug' });
3231
}
3332
}
3433

0 commit comments

Comments
 (0)