mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
fix(sample): remove relation path import
This commit is contained in:
@@ -9,7 +9,6 @@ import { GreeterBotName } from './app.constants';
|
||||
imports: [
|
||||
TelegrafModule.forRoot({
|
||||
token: process.env.ECHO_BOT_TOKEN,
|
||||
middlewares: [sessionMiddleware],
|
||||
include: [EchoModule],
|
||||
}),
|
||||
TelegrafModule.forRootAsync({
|
||||
|
@@ -0,0 +1,7 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext } from 'nestjs-telegraf';
|
||||
|
||||
export const From = createParamDecorator(
|
||||
(_, ctx: ExecutionContext) =>
|
||||
TelegrafExecutionContext.create(ctx).getContext().from,
|
||||
);
|
@@ -0,0 +1,11 @@
|
||||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
|
||||
import { TelegrafArgumentsHost } from 'nestjs-telegraf';
|
||||
|
||||
@Catch()
|
||||
export class TelegrafExceptionFilter<T> implements ExceptionFilter {
|
||||
catch(exception: T, host: ArgumentsHost) {
|
||||
const tgHost = TelegrafArgumentsHost.create(host);
|
||||
console.log(tgHost);
|
||||
return exception;
|
||||
}
|
||||
}
|
20
sample/01-complete-app/src/common/guards/admin.guard.ts
Normal file
20
sample/01-complete-app/src/common/guards/admin.guard.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext, TelegrafException } from 'nestjs-telegraf';
|
||||
import { Context } from '../../interfaces/context.interface';
|
||||
|
||||
@Injectable()
|
||||
export class AdminGuard implements CanActivate {
|
||||
private readonly ADMIN_IDS = [];
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const ctx = TelegrafExecutionContext.create(context);
|
||||
const { from } = ctx.getContext<Context>();
|
||||
|
||||
const isAdmin = this.ADMIN_IDS.includes(from.id);
|
||||
if (!isAdmin) {
|
||||
throw new TelegrafException('You are not admin >:(');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class ResponseTimeInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
console.log('Before...');
|
||||
|
||||
const start = Date.now();
|
||||
return next
|
||||
.handle()
|
||||
.pipe(tap(() => console.log(`Response time: ${Date.now() - start}ms`)));
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
import { Injectable, PipeTransform } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class ReverseTextPipe implements PipeTransform {
|
||||
transform(value: string): string {
|
||||
return value.split('').reverse().join('');
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { EchoUpdate } from './echo.update';
|
||||
import { EchoService } from './echo.service';
|
||||
import { HelloScene } from './scenes/hello.scene';
|
||||
import { HelloScene } from '../greeter/scenes/hello.scene';
|
||||
|
||||
@Module({
|
||||
providers: [EchoUpdate, EchoService, HelloScene],
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { Command, Help, InjectBot, On, Start, Update } from 'nestjs-telegraf';
|
||||
import { Help, InjectBot, On, Start, Update } from 'nestjs-telegraf';
|
||||
import { EchoService } from './echo.service';
|
||||
import { GreeterBotName, HELLO_SCENE_ID } from '../app.constants';
|
||||
import { GreeterBotName } from '../app.constants';
|
||||
import { Context } from '../interfaces/context.interface';
|
||||
|
||||
@Update()
|
||||
@@ -23,11 +23,6 @@ export class EchoUpdate {
|
||||
await ctx.reply('Send me any text');
|
||||
}
|
||||
|
||||
@Command('scene')
|
||||
async onSceneCommand(ctx: Context): Promise<void> {
|
||||
await ctx.scene.enter(HELLO_SCENE_ID);
|
||||
}
|
||||
|
||||
@On('message')
|
||||
async onMessage(ctx: Context): Promise<void> {
|
||||
console.log('New message received');
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import { Hears, Start, Update } from 'nestjs-telegraf';
|
||||
import { Command, Hears, Start, Update } from 'nestjs-telegraf';
|
||||
import { Context } from '../interfaces/context.interface';
|
||||
import { HELLO_SCENE_ID } from '../app.constants';
|
||||
|
||||
@Update()
|
||||
export class GreeterUpdate {
|
||||
@@ -13,4 +14,9 @@ export class GreeterUpdate {
|
||||
const { first_name } = ctx.from;
|
||||
await ctx.reply(`Hey ${first_name}`);
|
||||
}
|
||||
|
||||
@Command('scene')
|
||||
async onSceneCommand(ctx: Context): Promise<void> {
|
||||
await ctx.scene.enter(HELLO_SCENE_ID);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user