Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
admin.macaroon
tls.cert
dist/
.history/
.history/
commands.log
71 changes: 71 additions & 0 deletions bot/middleware/commandlogging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { MiddlewareFn } from 'telegraf';
import { CommunityContext } from '../modules/community/communityContext';
import winston from 'winston';

const logFile = process.env.COMMAND_LOG_FILE || 'commands.log';

const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DDTHH:mm:ss.SSSZ',
}),
winston.format.printf(info => {
return `[${info.timestamp}] ${info.level}: ${info.message} ${
info.stack ? info.stack : ''
}`;
}),
),
levels: winston.config.syslog.levels,
level: 'debug',
transports: [
new winston.transports.File({
filename: logFile,
maxsize: 5 * 1024 * 1024 * 1000, // 5GB
}),
],
exitOnError: false,
});

export function commandLogger(): MiddlewareFn<CommunityContext> {
return async (ctx, next) => {
try {
if (ctx.message && 'text' in ctx.message) {
const msg = ctx.message;
const text = msg.text.trim();
const userId = msg.from?.id ?? 'unknown';

let command: string | null = null;
let args: string[] = [];
let isCommand: boolean;

if (text.startsWith('/')) {
const parts = text.split(/\s+/);
command = parts[0];
args = parts.slice(1);
isCommand = true;
} else {
isCommand = false;
command = text;
}

const userName = msg.from?.username ?? '';

logger.info(`User @${userName} [${userId}] ${isCommand? 'executed command:' : 'sent message:'} ${command} with args: [${args.join(', ')}]`);
} else if (ctx.callbackQuery && 'data' in ctx.callbackQuery) {
let msgText: string;
// Safely attempt to get message text
msgText = (ctx.callbackQuery?.message as any)?.text ?? '';

Check failure on line 57 in bot/middleware/commandlogging.ts

View workflow job for this annotation

GitHub Actions / Lint

'msgText' is never reassigned. Use 'const' instead
const callbackData = ctx.callbackQuery.data;
const userName = ctx.callbackQuery.from?.username ?? '';
const userId = ctx.callbackQuery.from?.id ?? '';
logger.info(`User @${userName} [${userId}] sent callback query with data: ${callbackData}. Message text: '${msgText}'`);
} else {
logger.info(`Received non-command message or update from user.`);
}
} catch (err) {
logger.error('logging middleware failed', err);
}

return next();
};
}
2 changes: 2 additions & 0 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
import { logger } from '../logger';
import { IUsernameId } from '../models/community';
import { CommunityContext } from './modules/community/communityContext';
import { commandLogger } from './middleware/commandlogging';

export interface MainContext extends Context {
match: Array<string> | null;
Expand Down Expand Up @@ -192,6 +193,7 @@ const initialize = (
logger.error(err);
});

bot.use(commandLogger());
bot.use(session());
bot.use(limit());
bot.use(i18n.middleware());
Expand Down
Loading