mirror of
				https://github.com/Maks1mS/nestjs-telegraf.git
				synced 2025-10-31 05:52:17 +03:00 
			
		
		
		
	feat(decorators): TelegrafAction added
This commit is contained in:
		| @@ -10,3 +10,4 @@ export * from './telegraf-mention.decorator'; | ||||
| export * from './telegraf-phone.decorator'; | ||||
| export * from './telegraf-hashtag.decorator'; | ||||
| export * from './telegraf-cashtag.decorator'; | ||||
| export * from './telegraf-action.decorator'; | ||||
|   | ||||
							
								
								
									
										13
									
								
								lib/decorators/telegraf-action.decorator.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								lib/decorators/telegraf-action.decorator.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| import { SetMetadata } from '@nestjs/common'; | ||||
| import { DECORATORS } from '../telegraf.constants'; | ||||
| import { HearsTriggers } from 'telegraf'; | ||||
|  | ||||
| /** | ||||
|  * Registers middleware for handling callback_data actions with regular expressions. | ||||
|  * @param triggers Triggers | ||||
|  * | ||||
|  * https://telegraf.js.org/#/?id=action | ||||
|  */ | ||||
| export function TelegrafAction(triggers: HearsTriggers): MethodDecorator { | ||||
|   return SetMetadata(DECORATORS.ACTION, { triggers }); | ||||
| } | ||||
| @@ -122,4 +122,15 @@ export class TelegrafMetadataAccessor { | ||||
|   getTelegrafCashtagMetadata(target: Type<any> | Function) { | ||||
|     return this.reflector.get(DECORATORS.CASHTAG, target); | ||||
|   } | ||||
|  | ||||
|   isTelegrafAction(target: Type<any> | Function): boolean { | ||||
|     if (!target) { | ||||
|       return false; | ||||
|     } | ||||
|     return !!this.reflector.get(DECORATORS.ACTION, target); | ||||
|   } | ||||
|  | ||||
|   getTelegrafActionMetadata(target: Type<any> | Function) { | ||||
|     return this.reflector.get(DECORATORS.ACTION, target); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -15,4 +15,5 @@ export const DECORATORS = { | ||||
|   PHONE: `${DECORATORS_PREFIX}/PHONE`, | ||||
|   HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, | ||||
|   CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, | ||||
|   ACTION: `${DECORATORS_PREFIX}/ACTION`, | ||||
| }; | ||||
|   | ||||
| @@ -87,6 +87,11 @@ export class TelegrafExplorer implements OnModuleInit { | ||||
|               instance[key], | ||||
|             ); | ||||
|             this.handleTelegrafCashtag(instance, key, telegraf, metadata); | ||||
|           } else if (this.metadataAccessor.isTelegrafAction(instance[key])) { | ||||
|             const metadata = this.metadataAccessor.getTelegrafActionMetadata( | ||||
|               instance[key], | ||||
|             ); | ||||
|             this.handleTelegrafAction(instance, key, telegraf, metadata); | ||||
|           } | ||||
|         }, | ||||
|       ); | ||||
| @@ -202,4 +207,14 @@ export class TelegrafExplorer implements OnModuleInit { | ||||
|     // @ts-ignore | ||||
|     telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); | ||||
|   } | ||||
|  | ||||
|   handleTelegrafAction( | ||||
|     instance: object, | ||||
|     key: string, | ||||
|     telegraf: Telegraf<ContextMessageUpdate>, | ||||
|     metadata: any, | ||||
|   ) { | ||||
|     // @ts-ignore | ||||
|     telegraf.cashtag(metadata.triggers, instance[key].bind(instance)); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user