Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-telegraf-decorators

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-telegraf-decorators - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

build/sample/controllers/WizardController.js

4

build/sample/app.js

@@ -5,10 +5,10 @@ "use strict";

var src_1 = require("../src");
//import {ControllerTest} from "./controllers/ControllerTest";
var typedi_1 = require("typedi");
var bot = src_1.buildBot({
token: '396643032:AAGt0MdWSMgupFXhmgudFHxAw2VdEX_r3c4',
token: process.env.BOT_TOKEN,
container: typedi_1.Container,
// bot: bot bot instance
// session: session() custom session
// stage: new Stage() your Stage
controllers: [__dirname + '/controllers/**.ts'],
}).startPolling();

@@ -61,3 +61,3 @@ "use strict";

// ctx.reply(await this.service.getBotName())
ctx.scene.enter('game');
ctx.scene.enter('steps');
return [2 /*return*/];

@@ -67,2 +67,5 @@ });

};
ControllerTest.prototype.enter = function (ctx) {
ctx.reply('Hello Scene game');
};
__decorate([

@@ -73,3 +76,4 @@ typedi_1.Inject(),

__decorate([
src_1.Hears('container'),
src_1.Hears('qwe'),
src_1.Command('qwe'),
__param(0, src_1.TFContext()), __param(1, src_1.TFMessage()), __param(2, src_1.TFChat()), __param(3, src_1.TFTelegram()),

@@ -80,2 +84,9 @@ __metadata("design:type", Function),

], ControllerTest.prototype, "testContainer", null);
__decorate([
src_1.Enter(),
__param(0, src_1.TFContext()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], ControllerTest.prototype, "enter", null);
ControllerTest = __decorate([

@@ -82,0 +93,0 @@ src_1.TFController()

@@ -35,2 +35,3 @@ "use strict";

decorators_1.Hears('ok'),
__param(0, src_1.TFContext()),
__metadata("design:type", Function),

@@ -41,3 +42,3 @@ __metadata("design:paramtypes", [Object]),

ControllerTestScene = __decorate([
decorators_1.TFController('game')
decorators_1.TFScene('game')
], ControllerTestScene);

@@ -44,0 +45,0 @@ return ControllerTestScene;

@@ -5,2 +5,4 @@ "use strict";

var container_1 = require("./container");
var telegraf_1 = require("telegraf");
var WizardScene = require('telegraf/scenes/wizard');
var Stage = require('telegraf/stage');

@@ -10,83 +12,81 @@ var Scene = require('telegraf/scenes/base');

function buildFromMetadata(bot, options) {
var helpMethods = [];
var startMethods = [];
var scenes = {};
var stage = options.stage || new Stage();
bot.use(options.session ? options.session : session());
bot.use(stage.middleware());
MetadataStorage_1.default
.controllerMetadata.forEach(function (controller) {
MetadataStorage_1.default.composerMetadata
.forEach(function (controller) {
var controllerInstance = container_1.getFromContainer(controller.target);
var handler = bot;
if (controller.scene) {
handler = scenes[controller.scene] || new Scene(controller.scene);
scenes[controller.scene] = handler;
}
MetadataStorage_1.default.helpMetadata
.filter(function (help) { return help.target == controller.target.prototype; })
.forEach(function (value) {
helpMethods.push(function (ctx) { controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName)); });
});
MetadataStorage_1.default.startMetadata
.filter(function (start) { return start.target == controller.target.prototype; })
.forEach(function (value) {
startMethods.push(function (ctx) { controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName)); });
});
MetadataStorage_1.default.actionMetadata
.filter(function (action) { return action.target == controller.target.prototype; })
.forEach(function (value) {
handler.action(value.action, function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
MetadataStorage_1.default.hearsMetadata
.filter(function (hear) { return hear.target == controller.target.prototype; })
.forEach(function (value) {
handler.hears(value.match, function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
MetadataStorage_1.default.onMetadata
.filter(function (on) { return on.target == controller.target.prototype; })
.forEach(function (value) {
handler.on(value.event, function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
MetadataStorage_1.default.commandMetadata
.filter(function (command) { return command.target == controller.target.prototype; })
.forEach(function (value) {
handler.command(value.command, function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
MetadataStorage_1.default.enterMetadata
.filter(function (command) { return command.target == controller.target.prototype; })
.forEach(function (value) {
handler.enter(function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
MetadataStorage_1.default.leaveMetadata
.filter(function (command) { return command.target == controller.target.prototype; })
.forEach(function (value) {
handler.leave(function (ctx) {
controllerInstance[value.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, value.propertyName));
});
});
if (controller.options.type == "controller")
buildController(bot, controller, controllerInstance);
else if (controller.options.type == "scene")
buildScene(stage, controller, controllerInstance);
else if (controller.options.type == "wizard")
buildWizard(bot, stage, controller, controllerInstance);
});
bot.help(function (ctx) {
helpMethods.forEach(function (method) { return method(ctx); });
return bot;
}
exports.buildFromMetadata = buildFromMetadata;
function buildScene(stage, controllerScene, controllerInstance) {
var scene = new Scene(controllerScene.options.data.scene);
MetadataStorage_1.default
.handlers
.filter(function (value) { return controllerScene.target.prototype == value.target; })
.forEach(function (handler) {
scene[handler.type].apply(scene, handler.data.concat([function (ctx) {
controllerInstance[handler.propertyName].apply(controllerInstance, getInjectParams(ctx, controllerScene.target, handler.propertyName));
}]));
});
bot.start(function (ctx) {
startMethods.forEach(function (method) { return method(ctx); });
stage.register(scene);
}
function buildController(bot, controller, controllerInstance) {
// @ts-ignore
var composer = new telegraf_1.Composer();
MetadataStorage_1.default
.handlers
.filter(function (value) { return controller.target.prototype == value.target && value.type != "enter" && value.type != 'leave'; })
.forEach(function (handler) {
composer[handler.type].apply(composer, handler.data.concat([function (ctx) {
controllerInstance[handler.propertyName].apply(controllerInstance, getInjectParams(ctx, controller.target, handler.propertyName));
}]));
});
if (Object.keys(scenes).length) {
for (var sc in scenes) {
stage.register(scenes[sc]);
}
}
return bot;
bot.use(controller.options.data.compose ? controller.options.data.compose(composer) : composer);
}
exports.buildFromMetadata = buildFromMetadata;
function buildWizard(bot, stage, wizard, controllerInstance) {
var group = MetadataStorage_1.default
.wizardStep
.sort(function (a, b) { return a.step - b.step; })
.reduce(function (r, a) {
r[a.step] = r[a.step] || [];
r[a.step].push(a);
return r;
}, Object.create(null));
var steps = Object.values(group).map(function (stepsMetadata, index) {
var composer = new telegraf_1.Composer();
var method;
stepsMetadata.forEach(function (stepMethod) {
var handlers = MetadataStorage_1.default.handlers.filter(function (value) { return value.target == wizard.target.prototype && value.propertyName == stepMethod.propertyName; });
if (handlers.length) {
handlers.forEach(function (handler) {
composer[handler.type].apply(composer, handler.data.concat([function (ctx) {
return controllerInstance[handler.propertyName].apply(controllerInstance, getInjectParams(ctx, wizard.target, handler.propertyName));
}]));
});
}
else {
method = function (ctx) {
return controllerInstance[stepMethod.propertyName].apply(controllerInstance, getInjectParams(ctx, wizard.target, stepMethod.propertyName));
};
}
});
return method || composer;
});
var wizardInstance = new (WizardScene.bind.apply(WizardScene, [void 0, wizard.options.data.name].concat(steps)))();
var handlers = MetadataStorage_1.default.handlers.filter(function (value) { return wizard.target.prototype == value.target && !MetadataStorage_1.default.wizardStep.find(function (value1) { return value1.propertyName == value.propertyName; }); });
handlers.forEach(function (handler) {
wizardInstance[handler.type].apply(wizardInstance, handler.data.concat([function (ctx) {
return controllerInstance[handler.propertyName].apply(controllerInstance, getInjectParams(ctx, wizard.target, handler.propertyName));
}]));
});
stage.register(wizardInstance);
}
function getInjectParams(ctx, target, methodName) {

@@ -93,0 +93,0 @@ return MetadataStorage_1.default

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MetadataStorage_1 = require("./MetadataStorage");
var StartMetadata_1 = require("./metadata/StartMetadata");
var CommandMetadata_1 = require("./metadata/CommandMetadata");
var HelpMetadata_1 = require("./metadata/HelpMetadata");
var OnMetadata_1 = require("./metadata/OnMetadata");
var HearsMetadata_1 = require("./metadata/HearsMetadata");
var ControllerMetadata_1 = require("./metadata/ControllerMetadata");
var LeaveMetadata_1 = require("./metadata/LeaveMetadata");
var EnterMetadata_1 = require("./metadata/EnterMetadata");
var ParamsMetadata_1 = require("./metadata/ParamsMetadata");
var metadata_1 = require("./metadata");
function TFController(scene) {
var ComposerMetadata_1 = require("./metadata/ComposerMetadata");
var WizardStepMetadata_1 = require("./metadata/WizardStepMetadata");
function TFController(compose) {
return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.controllerMetadata.push(new ControllerMetadata_1.ControllerMetadata(target, propertyKey, scene));
Composer({
type: "controller",
data: {
compose: compose
}
})(target, propertyKey, descriptor);
return descriptor;

@@ -21,5 +19,48 @@ };

exports.TFController = TFController;
function TFScene(scene) {
return function (target, propertyKey, descriptor) {
Composer({
type: "scene",
data: {
scene: scene
}
})(target, propertyKey, descriptor);
return descriptor;
};
}
exports.TFScene = TFScene;
function TFWizardStep(step) {
return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.wizardStep.push(new WizardStepMetadata_1.WizardStepMetadata(target, propertyKey, step));
return descriptor;
};
}
exports.TFWizardStep = TFWizardStep;
function TFWizard(name) {
return function (target, propertyKey, descriptor) {
Composer({
type: "wizard",
data: {
name: name
}
})(target, propertyKey, descriptor);
return descriptor;
};
}
exports.TFWizard = TFWizard;
function Composer(options) {
return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.composerMetadata.push(new ComposerMetadata_1.ComposerMetadata(target, options));
return descriptor;
};
}
exports.Composer = Composer;
function Start() {
return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.startMetadata.push(new StartMetadata_1.StartMetadata(target, propertyKey));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "start",
data: []
});
return descriptor;

@@ -31,3 +72,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.helpMetadata.push(new HelpMetadata_1.HelpMetadata(target, propertyKey));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "help",
data: []
});
return descriptor;

@@ -39,3 +85,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.onMetadata.push(new OnMetadata_1.OnMetadata(target, propertyKey, event));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "on",
data: [event]
});
return descriptor;

@@ -47,3 +98,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.hearsMetadata.push(new HearsMetadata_1.HearsMetadata(target, propertyKey, match));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "hears",
data: [match]
});
return descriptor;

@@ -55,3 +111,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.commandMetadata.push(new CommandMetadata_1.CommandMetadata(target, propertyKey, command));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "command",
data: [command]
});
return descriptor;

@@ -63,3 +124,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.enterMetadata.push(new EnterMetadata_1.EnterMetadata(target, propertyKey));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "enter",
data: []
});
return descriptor;

@@ -71,3 +137,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.actionMetadata.push(new metadata_1.ActionMetadata(target, propertyKey, action));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "action",
data: [action]
});
return descriptor;

@@ -79,3 +150,8 @@ };

return function (target, propertyKey, descriptor) {
MetadataStorage_1.default.leaveMetadata.push(new LeaveMetadata_1.LeaveMetadata(target, propertyKey));
MetadataStorage_1.default.handlers.push({
propertyName: propertyKey,
target: target,
type: "leave",
data: []
});
return descriptor;

@@ -82,0 +158,0 @@ };

@@ -11,4 +11,2 @@ "use strict";

var glob = require("glob");
__export(require("./decorators"));
__export(require("./metadata"));
function buildBot(options) {

@@ -26,1 +24,3 @@ if (options.container)

exports.buildBot = buildBot;
__export(require("./decorators"));
__export(require("./metadata"));

@@ -6,10 +6,5 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./EnterMetadata"));
__export(require("./LeaveMetadata"));
__export(require("./ControllerMetadata"));
__export(require("./HearsMetadata"));
__export(require("./OnMetadata"));
__export(require("./CommandMetadata"));
__export(require("./StartMetadata"));
__export(require("./HelpMetadata"));
__export(require("./ActionMetadata"));
__export(require("./ComposerMetadata"));
__export(require("./ParamsMetadata"));
__export(require("./WizardMetadata"));
__export(require("./WizardStepMetadata"));

@@ -5,12 +5,6 @@ "use strict";

function MetadataStorage() {
this.startMetadata = [];
this.commandMetadata = [];
this.helpMetadata = [];
this.hearsMetadata = [];
this.onMetadata = [];
this.leaveMetadata = [];
this.enterMetadata = [];
this.actionMetadata = [];
this.handlers = [];
this.paramMetadata = [];
this.controllerMetadata = [];
this.composerMetadata = [];
this.wizardStep = [];
}

@@ -17,0 +11,0 @@ return MetadataStorage;

@@ -7,2 +7,3 @@ import { TestService } from "../services/TestService";

testContainer(ctx: any, msg: IncomingMessage, chat: Chat, telegram: Telegram): Promise<void>;
enter(ctx: any): void;
}
import * as tt from "telegraf/typings/telegram-types";
export declare function TFController(scene?: string): Function;
import { ComposerOptions } from "./metadata/ComposerMetadata";
import { Composer as Comp } from "telegraf";
export declare function TFController(compose?: (composer: Comp<any>) => void): Function;
export declare function TFScene(scene: string): Function;
export declare function TFWizardStep(step: number): Function;
export declare function TFWizard(name?: string): Function;
export declare function Composer(options: ComposerOptions): Function;
export declare function Start(): Function;

@@ -4,0 +10,0 @@ export declare function Help(): Function;

import { IBotOptions } from "./interfaces/IBotOptions";
export declare function buildBot(options: IBotOptions): any;
export * from './decorators';
export * from './interfaces/IBotOptions';
export * from './metadata';
export declare function buildBot(options: IBotOptions): any;

@@ -1,9 +0,5 @@

export * from './EnterMetadata';
export * from './LeaveMetadata';
export * from './ControllerMetadata';
export * from './HearsMetadata';
export * from './OnMetadata';
export * from './CommandMetadata';
export * from './StartMetadata';
export * from './HelpMetadata';
export * from './ActionMetadata';
export * from './ComposerMetadata';
export * from './HandlerMetadata';
export * from './ParamsMetadata';
export * from './WizardMetadata';
export * from './WizardStepMetadata';

@@ -1,24 +0,12 @@

import { StartMetadata } from "./metadata/StartMetadata";
import { CommandMetadata } from "./metadata/CommandMetadata";
import { HelpMetadata } from "./metadata/HelpMetadata";
import { OnMetadata } from "./metadata/OnMetadata";
import { HearsMetadata } from "./metadata/HearsMetadata";
import { ControllerMetadata } from "./metadata/ControllerMetadata";
import { LeaveMetadata } from "./metadata/LeaveMetadata";
import { EnterMetadata } from "./metadata/EnterMetadata";
import { ParamsMetadata } from "./metadata/ParamsMetadata";
import { ActionMetadata } from "./metadata";
import { ComposerMetadata } from "./metadata/ComposerMetadata";
import { WizardStepMetadata } from "./metadata/WizardStepMetadata";
import { HandlerMetadata } from "./metadata/HandlerMetadata";
declare class MetadataStorage {
startMetadata: StartMetadata[];
commandMetadata: CommandMetadata[];
helpMetadata: HelpMetadata[];
hearsMetadata: HearsMetadata[];
onMetadata: OnMetadata[];
leaveMetadata: LeaveMetadata[];
enterMetadata: EnterMetadata[];
actionMetadata: ActionMetadata[];
handlers: HandlerMetadata[];
paramMetadata: ParamsMetadata[];
controllerMetadata: ControllerMetadata[];
composerMetadata: ComposerMetadata[];
wizardStep: WizardStepMetadata[];
}
declare const _default: MetadataStorage;
export default _default;
{
"name": "ts-telegraf-decorators",
"version": "0.1.3",
"version": "0.2.0",
"author": "Ivan Surkov <panarama360@gmail.com>",

@@ -5,0 +5,0 @@ "main": "build/src/index.js",

import 'reflect-metadata'
import {buildBot} from "../src";
//import {ControllerTest} from "./controllers/ControllerTest";
import {Container} from "typedi";
const bot = buildBot({
token: '396643032:AAGt0MdWSMgupFXhmgudFHxAw2VdEX_r3c4',
token: process.env.BOT_TOKEN,
container: Container,
// bot: bot bot instance
// session: session() custom session
// stage: new Stage() your Stage
controllers: [__dirname+'/controllers/**.ts'],

@@ -12,0 +12,0 @@ // or controllers: [ControllerTest],

@@ -9,3 +9,3 @@ import {

TFMessage,
TFChat, TFTelegram, TFContext
TFChat, TFTelegram, TFContext, Leave, Enter
} from '../../src/'

@@ -15,4 +15,5 @@ import {Inject} from "typedi";

import {Chat, IncomingMessage} from 'telegraf/typings/telegram-types';
import {Context, ContextMessageUpdate, Telegram} from "telegraf";
import {Telegram} from "telegraf";
@TFController()

@@ -25,8 +26,14 @@ export class ControllerTest {

@Hears('container')
@Hears('qwe')
@Command('qwe')
async testContainer(@TFContext()ctx, @TFMessage()msg:IncomingMessage, @TFChat()chat: Chat, @TFTelegram() telegram: Telegram){
// ctx.reply(await this.service.getBotName())
ctx.scene.enter('game')
ctx.scene.enter('steps')
// console.log('Hello');
}
}
@Enter()
enter(@TFContext()ctx){
ctx.reply('Hello Scene game')
}
}

@@ -1,5 +0,5 @@

import {TFController, Start, Command, On, Hears, Help, Enter} from '../../src/decorators'
import {TFController, Start, Command, On, Hears, Help, Enter, TFScene} from '../../src/decorators'
import {TFContext} from "../../src";
@TFController('game')
@TFScene('game')
export class ControllerTestScene {

@@ -13,5 +13,5 @@

@Hears('ok')
ok(ctx){
ok(@TFContext()ctx){
ctx.reply('OK!!!!')
}
}
}
import MetadataStorage from "./MetadataStorage";
import {IBotOptions} from "./interfaces/IBotOptions";
import {getFromContainer} from "./container";
import Telegraf from "telegraf";
import {Composer} from "telegraf";
import {ComposerMetadata} from "./metadata/ComposerMetadata";
import {WizardMetadata} from "./metadata/WizardMetadata";
const WizardScene = require('telegraf/scenes/wizard')
const Stage = require('telegraf/stage')

@@ -10,8 +16,2 @@ const Scene = require('telegraf/scenes/base')

export function buildFromMetadata(bot: any, options: IBotOptions): any {
const helpMethods: Function[] = []
const startMethods: Function[] = []
const scenes: {[key:string]:any} = {};
const stage = options.stage || new Stage();

@@ -23,94 +23,86 @@

MetadataStorage
.controllerMetadata.forEach(controller => {
let controllerInstance = getFromContainer(controller.target)
let handler = bot;
if(controller.scene){
handler = scenes[controller.scene]||new Scene(controller.scene)
scenes[controller.scene] = handler;
}
MetadataStorage.composerMetadata
.forEach(controller => {
let controllerInstance = getFromContainer(controller.target)
if (controller.options.type == "controller")
buildController(bot, controller, controllerInstance);
else if (controller.options.type == "scene")
buildScene(stage, controller, controllerInstance);
else if (controller.options.type == "wizard")
buildWizard(bot, stage, controller, controllerInstance)
});
MetadataStorage.helpMetadata
.filter(help => help.target == controller.target.prototype)
.forEach(value => {
helpMethods.push((ctx)=>{controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))})
})
return bot;
}
MetadataStorage.startMetadata
.filter(start => start.target == controller.target.prototype)
.forEach(value => {
startMethods.push((ctx)=>{controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))})
})
function buildScene(stage: any, controllerScene: ComposerMetadata, controllerInstance: any) {
const scene = new Scene(controllerScene.options.data.scene);
MetadataStorage
.handlers
.filter(value => controllerScene.target.prototype == value.target)
.forEach(handler => {
scene[handler.type](...[...handler.data, (ctx) => {
controllerInstance[handler.propertyName](...getInjectParams(ctx, controllerScene.target, handler.propertyName));
}])
});
stage.register(scene);
}
MetadataStorage.actionMetadata
.filter(action => action.target == controller.target.prototype)
.forEach(value => {
handler.action(value.action, function (ctx) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
})
})
function buildController(bot: any, controller: ComposerMetadata, controllerInstance: any) {
// @ts-ignore
const composer = new Composer();
MetadataStorage
.handlers
.filter(value => controller.target.prototype == value.target && value.type != "enter" && value.type != 'leave')
.forEach(handler => {
composer[handler.type](...[...handler.data, (ctx) => {
controllerInstance[handler.propertyName](...getInjectParams(ctx, controller.target, handler.propertyName));
}])
});
bot.use(controller.options.data.compose ? controller.options.data.compose(composer) : composer);
}
MetadataStorage.hearsMetadata
.filter(hear => hear.target == controller.target.prototype)
.forEach(value => {
handler.hears(value.match, function (ctx) {
function buildWizard(bot: any, stage: any, wizard: ComposerMetadata, controllerInstance: any) {
const group = MetadataStorage
.wizardStep
.sort((a, b) => a.step - b.step)
.reduce(function (r, a) {
r[a.step] = r[a.step] || [];
r[a.step].push(a);
return r;
}, Object.create(null));
const steps = Object.values(group).map((stepsMetadata: WizardMetadata[], index) => {
const composer = new Composer();
let method;
stepsMetadata.forEach(stepMethod => {
const handlers = MetadataStorage.handlers.filter(value => value.target == wizard.target.prototype && value.propertyName == stepMethod.propertyName);
if (handlers.length) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
handlers.forEach(handler => {
composer[handler.type](...[...handler.data, (ctx) => {
return controllerInstance[handler.propertyName](...getInjectParams(ctx, wizard.target, handler.propertyName));
}])
})
})
MetadataStorage.onMetadata
.filter(on => on.target == controller.target.prototype)
.forEach(value => {
handler.on(value.event, function (ctx) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
})
})
MetadataStorage.commandMetadata
.filter(command => command.target == controller.target.prototype)
.forEach(value => {
handler.command(value.command, function (ctx) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
})
})
MetadataStorage.enterMetadata
.filter(command => command.target == controller.target.prototype)
.forEach(value => {
handler.enter(function (ctx) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
})
})
MetadataStorage.leaveMetadata
.filter(command => command.target == controller.target.prototype)
.forEach(value => {
handler.leave(function (ctx) {
controllerInstance[value.propertyName](...getInjectParams(ctx, controller.target, value.propertyName))
})
})
} else {
method = (ctx) => {
return controllerInstance[stepMethod.propertyName](...getInjectParams(ctx, wizard.target, stepMethod.propertyName));
}
}
})
return method || composer;
})
bot.help(function (ctx) {
helpMethods.forEach(method => method(ctx))
})
bot.start(function (ctx) {
startMethods.forEach(method => method(ctx))
})
if(Object.keys(scenes).length){
for(let sc in scenes) {
stage.register(scenes[sc])
}
}
return bot;
const wizardInstance = new WizardScene(wizard.options.data.name, ...steps);
const handlers = MetadataStorage.handlers.filter(value => wizard.target.prototype == value.target && !MetadataStorage.wizardStep.find(value1 => value1.propertyName == value.propertyName))
handlers.forEach(handler => {
wizardInstance[handler.type](...[...handler.data, (ctx) => {
return controllerInstance[handler.propertyName](...getInjectParams(ctx, wizard.target, handler.propertyName));
}])
});
stage.register(wizardInstance);
}
function getInjectParams(ctx: any, target: Function, methodName: string): any[]{
function getInjectParams(ctx: any, target: Function, methodName: string): any[] {

@@ -120,5 +112,5 @@ return MetadataStorage

.filter(value => value.target == target.prototype && methodName === value.propertyName)
.sort((a, b) => a.index-b.index)
.sort((a, b) => a.index - b.index)
.map(value => {
if(value.type === 'ctx')
if (value.type === 'ctx')
return ctx;

@@ -125,0 +117,0 @@ return ctx[value.type];

import MetadataStorage from "./MetadataStorage";
import {StartMetadata} from "./metadata/StartMetadata";
import {CommandMetadata} from "./metadata/CommandMetadata";
import {HelpMetadata} from "./metadata/HelpMetadata";
import {OnMetadata} from "./metadata/OnMetadata";
import {HearsMetadata} from "./metadata/HearsMetadata";
import * as tt from "telegraf/typings/telegram-types";
import {ControllerMetadata} from "./metadata/ControllerMetadata";
import {LeaveMetadata} from "./metadata/LeaveMetadata";
import {EnterMetadata} from "./metadata/EnterMetadata";
import {ParamsMetadata} from "./metadata/ParamsMetadata";
import {ActionMetadata} from "./metadata";
import {ComposerMetadata, ComposerOptions} from "./metadata/ComposerMetadata";
import {WizardStepMetadata} from "./metadata/WizardStepMetadata";
import {Composer as Comp} from "telegraf";
export function TFController(scene?: string): Function {
export function TFController(compose?: (composer: Comp<any>) => void): Function {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.controllerMetadata.push(new ControllerMetadata(target,propertyKey, scene))
Composer({
type: "controller",
data: {
compose
}
})(target, propertyKey, descriptor);
return descriptor;

@@ -21,24 +21,87 @@ };

export function Start():Function {
export function TFScene(scene: string): Function {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
Composer({
type: "scene",
data: {
scene
}
})(target, propertyKey, descriptor);
return descriptor;
};
}
export function TFWizardStep(step: number): Function {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.wizardStep.push(new WizardStepMetadata(target, propertyKey, step));
return descriptor;
}
}
export function TFWizard(name?: string): Function {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
Composer({
type: "wizard",
data: {
name
}
})(target, propertyKey, descriptor);
return descriptor;
};
}
export function Composer(options: ComposerOptions): Function {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.composerMetadata.push(new ComposerMetadata(target, options))
return descriptor;
};
}
export function Start(): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.startMetadata.push(new StartMetadata(target,propertyKey))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "start",
data: []
});
return descriptor;
}
}
export function Help():Function {
export function Help(): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.helpMetadata.push(new HelpMetadata(target,propertyKey))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "help",
data: []
});
return descriptor;
}
}
export function On(event:tt.UpdateType | tt.UpdateType[] | tt.MessageSubTypes | tt.MessageSubTypes[]):Function {
export function On(event: tt.UpdateType | tt.UpdateType[] | tt.MessageSubTypes | tt.MessageSubTypes[]): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.onMetadata.push(new OnMetadata(target,propertyKey,event))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "on",
data: [event]
});
return descriptor;
}
}
export function Hears(match:string | RegExp):Function {
export function Hears(match: string | RegExp): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.hearsMetadata.push(new HearsMetadata(target,propertyKey,match))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "hears",
data: [match]
});
return descriptor;

@@ -48,5 +111,10 @@ }

export function Command(command:string):Function {
export function Command(command: string): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.commandMetadata.push(new CommandMetadata(target,propertyKey, command))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "command",
data: [command]
});
return descriptor;

@@ -56,5 +124,10 @@ }

export function Enter():Function {
export function Enter(): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.enterMetadata.push(new EnterMetadata(target,propertyKey))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "enter",
data: []
});
return descriptor;

@@ -64,5 +137,11 @@ }

export function Action(action:string | RegExp):Function {
export function Action(action: string | RegExp): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.actionMetadata.push(new ActionMetadata(target,propertyKey, action))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "action",
data: [action]
});
return descriptor;

@@ -72,5 +151,10 @@ }

export function Leave():Function {
export function Leave(): Function {
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
MetadataStorage.leaveMetadata.push(new LeaveMetadata(target,propertyKey))
MetadataStorage.handlers.push({
propertyName: propertyKey,
target,
type: "leave",
data: []
});
return descriptor;

@@ -80,22 +164,25 @@ }

export function TFContext():Function {
export function TFContext(): Function {
return function (target: Function, propertyKey: string, parameterIndex: number) {
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'ctx'))
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'ctx'));
}
}
export function TFTelegram():Function {
export function TFTelegram(): Function {
return function (target: Function, propertyKey: string, parameterIndex: number) {
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'telegram'))
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'telegram'));
}
}
export function TFChat():Function {
export function TFChat(): Function {
return function (target: Function, propertyKey: string, parameterIndex: number) {
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'chat'))
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'chat'));
}
}
export function TFMessage():Function {
export function TFMessage(): Function {
return function (target: Function, propertyKey: string, parameterIndex: number) {
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'message'))
MetadataStorage.paramMetadata.push(new ParamsMetadata(target, propertyKey, parameterIndex, 'message'));
}
}

@@ -1,2 +0,2 @@

import Telegraf, {ContextMessageUpdate} from 'telegraf'
import Telegraf from 'telegraf'
import * as path from "path";

@@ -9,8 +9,4 @@ import {buildFromMetadata} from "./builder";

export * from './decorators'
export * from './interfaces/IBotOptions'
export * from './metadata'
export function buildBot(options: IBotOptions) {
if(options.container) useContainer(options.container)
if (options.container) useContainer(options.container)
let bot = options.bot || new Telegraf(options.token)

@@ -26,1 +22,4 @@ getContainer().set(Telegraf, bot);

export * from './decorators'
export * from './interfaces/IBotOptions'
export * from './metadata'

@@ -8,4 +8,4 @@ import {Container} from "../container";

session?: any,
controllers: Function[]|string[],
controllers: Function[] | string[],
stage?: any
}

@@ -1,9 +0,5 @@

export * from './EnterMetadata'
export * from './LeaveMetadata'
export * from './ControllerMetadata'
export * from './HearsMetadata'
export * from './OnMetadata'
export * from './CommandMetadata'
export * from './StartMetadata'
export * from './HelpMetadata'
export * from './ActionMetadata'
export * from './ComposerMetadata'
export * from './HandlerMetadata'
export * from './ParamsMetadata'
export * from './WizardMetadata'
export * from './WizardStepMetadata'

@@ -1,26 +0,17 @@

import {StartMetadata} from "./metadata/StartMetadata";
import {CommandMetadata} from "./metadata/CommandMetadata";
import {HelpMetadata} from "./metadata/HelpMetadata";
import {OnMetadata} from "./metadata/OnMetadata";
import {HearsMetadata} from "./metadata/HearsMetadata";
import {ControllerMetadata} from "./metadata/ControllerMetadata";
import {LeaveMetadata} from "./metadata/LeaveMetadata";
import {EnterMetadata} from "./metadata/EnterMetadata";
import {ParamsMetadata} from "./metadata/ParamsMetadata";
import {ActionMetadata} from "./metadata";
import {ComposerMetadata} from "./metadata/ComposerMetadata";
import {WizardStepMetadata} from "./metadata/WizardStepMetadata";
import {HandlerMetadata} from "./metadata/HandlerMetadata";
class MetadataStorage {
public startMetadata: StartMetadata[] = [];
public commandMetadata: CommandMetadata[] = [];
public helpMetadata: HelpMetadata[] = [];
public hearsMetadata: HearsMetadata[] = [];
public onMetadata: OnMetadata[] = [];
public leaveMetadata: LeaveMetadata[] = [];
public enterMetadata: EnterMetadata[] = [];
public actionMetadata: ActionMetadata[] = [];
public handlers: HandlerMetadata[] = [];
public paramMetadata: ParamsMetadata[] = [];
public controllerMetadata: ControllerMetadata[] = [];
public composerMetadata: ComposerMetadata[] = [];
public wizardStep: WizardStepMetadata[] = []
}
export default new MetadataStorage();
export default new MetadataStorage();
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc