Replies: 1 comment
-
create new guard class rateLimit.guard.ts import {CanActivate, ExecutionContext, HttpException, Injectable} from '@nestjs/common';
import { TelegrafExecutionContext} from 'nestjs-telegraf';
import {Context} from '../../interfaces/context.interface';
@Injectable()
export class RateLimitGuard implements CanActivate {
private readonly timeout = 1000;
private timestamps:any = []
canActivate(context: ExecutionContext): boolean {
const ctx = TelegrafExecutionContext.create(context);
const {from} = ctx.getContext<Context>();
const prevTimestamp = this.timestamps[from.id];
const timestamp = new Date().getTime();
if(prevTimestamp && prevTimestamp>timestamp-this.timeout) throw new HttpException('Slowly bro... 😡', 429);
this.timestamps[from.id]=timestamp;
return true;
}
} And just connect the guard to your bot.update.ts bot.update.ts @Update()
// @UseInterceptors(ResponseTimeInterceptor)
// @UseFilters(TelegrafExceptionFilter)
@UseGuards(RateLimitGuard)
export class BotUpdate {
.... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone!
How can I add a guard from a spam protection which will be like Rate Limiting @nestjs/throttler ?
@nestjs/throttler above the nestjs-telegraf decorators throws me an error.
Beta Was this translation helpful? Give feedback.
All reactions