npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

telegraf-controllers

v1.1.0

Published

1. Add ```"experimentalDecorators": true``` to tsconfig. 2. Create a class that will inherit from the controller. 3. Add decorator ```@controller(name)``` to the created class. 4. Add methods in created class with decorator ```@action(name)```. 5. Ini

Downloads

2

Readme

Using

  1. Add "experimentalDecorators": true to tsconfig.
  2. Create a class that will inherit from the controller.
  3. Add decorator @controller(name) to the created class.
  4. Add methods in created class with decorator @action(name).
  5. Initialize class Router and pass created class to contructor.
  6. Call method go from Router
  • Router.go() - take userId and route. Route must be separated with "/" (controller/action). If passed without action then action sets to default. You can pass addition params after route (Ex. router.go(userId, route, ctx, user, other)).
  • @controller(name)
  • @action(name) // default name is "default"
  • ActionProps - object
    • route - Current route
    • isRedirect
    • changeRoute(route: string, routeData?: any) => Promise

Simple example:

import Router, { controller, action, ActionProps, Controller } from "telegraf-controllers";
import Telegraf, { Markup } from 'telegraf';
import { TelegrafContext } from "telegraf/typings/context";

@controller('main')
class MainController extends Controller {
    @action()
    async actionDefault({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Me':
                    return await changeRoute('main/me');
            }
        }

        ctx.reply('Hello', Markup.keyboard(['Me']).resize().extra());
    }

    @action('me')
    async actionMe({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Back':
                    return await changeRoute('main');
            }
        }

        ctx.reply(`Id: ${ctx.from.id}\nUsername: ${ctx.from.username}`, 
            Markup.keyboard(['Back']).resize().extra()
        );
    }
}

let users = {};

async function updateUserRoute(route: string, routeData: any, userId: any) {
    users[userId].route = route;
    users[userId].routeData = routeData;
}

let router = new Router([
    new MainController()
], updateUserRoute);

let telegraf = new Telegraf('<token>');

telegraf.on('text', async (ctx) => {
    if (!(ctx.from.id in users)) {
        users[ctx.from.id] = {
            id: ctx.from.id,
            route: 'main',
            routeData: null,
            money: 0
        };
    }

    router.go(ctx.from.id, users[ctx.from.id].route, ctx, users[ctx.from.id]);
});

telegraf.launch();

Advanced Example

import Router, { controller, action, ActionProps, Controller } from "telegraf-controllers";
import Telegraf, { Markup } from 'telegraf';
import { TelegrafContext } from "telegraf/typings/context";

@controller('main')
class MainController extends Controller {
    @action()
    async actionDefault({ isRedirect, changeRoute }: ActionProps, ctx: TelegrafContext) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Balance':
                    return await changeRoute('balance');
            }
        }

        ctx.reply('Hi\nRedirect: ' + (isRedirect ? 1 : 0), Markup.keyboard(['Balance']).resize().extra());
    }
}

@controller('balance')
class BalanceController extends Controller {
    addBalance: (userId: number, sum: number) => any;

    constructor(addBalance: (userId: number, sum: number) => any) {
        super();

        this.addBalance = addBalance;
    }

    @action()
    async actionBalance({ changeRoute, isRedirect }: ActionProps, ctx: any, user: any) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Back':
                    return await changeRoute('main');
                case 'Add':
                    return await changeRoute('balance/add');
            }
        }

        ctx.reply(user.money + '\nRedirect: ' + (isRedirect ? 1 : 0), Markup.keyboard(['Add', 'Back']).resize().extra());
    }

    @action('add')
    async actionAdd({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext, user: any) {
        if (!isRedirect) {
            if (!isRedirect) {
                switch (ctx.message.text) {
                    case 'Back':
                        return await changeRoute('balance');
                }
            }
            let sum = parseInt(ctx.message.text);
            if (!Number.isNaN(sum)) {
                await this.addBalance(user.id, sum);
                return await changeRoute('balance');
            }
        }

        ctx.reply('Enter sum:', Markup.keyboard(['Back']).resize().extra());
    }
}




async function updateUserBalance(userId: number, sum: number) {
    users[userId].money += sum;
}


let users = {};

async function updateUserRoute(route: string, routeData: any, userId: any) {
    users[userId].route = route;
    users[userId].routeData = routeData;
}

let router = new Router([
    new MainController(),
    new BalanceController(updateUserBalance)
], updateUserRoute);

let telegraf = new Telegraf('<token>');

telegraf.on('text', async (ctx) => {
    if (!(ctx.from.id in users)) {
        users[ctx.from.id] = {
            id: ctx.from.id,
            route: 'main',
            routeData: null,
            money: 0
        };
    }

    router.go(ctx.from.id, users[ctx.from.id].route, ctx, users[ctx.from.id]);
});

telegraf.launch();

Rules

import Router, { controller, action, ActionProps, Controller } from "telegraf-controllers";
import Telegraf, { Markup } from 'telegraf';
import { TelegrafContext } from "telegraf/typings/context";

@controller('main')
class MainController extends Controller {
    @action()
    async actionDefault({ isRedirect, changeRoute }: ActionProps, ctx: TelegrafContext) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Balance':
                    return await changeRoute('balance');
            }
        }

        ctx.reply('Hi\nRedirect: ' + (isRedirect ? 1 : 0), Markup.keyboard(['Balance']).resize().extra());
    }
}

@controller('balance')
class BalanceController extends Controller {
    addBalance: (userId: number, sum: number) => any;
    
    rulesController = [
        {
            rule: async ({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext, user: any) => {
                return user.isUser;
            },
            onRuleError: async ({ changeRoute }: ActionProps, ctx: TelegrafContext, user: any) => {
                await ctx.reply('Permission denied');
                return await changeRoute('main');
            }
        }
    ];

    rulesActions = {
        'add': {
            rule: async ({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext, user: any) => {
                return user.isAdmin;
            },
            onRuleError: async ({ changeRoute }: ActionProps, ctx: TelegrafContext, user: any) => {
                await ctx.reply('Permission denied');
                return await changeRoute('balance');
            }
        }
    };

    constructor(addBalance: (userId: number, sum: number) => any) {
        super();

        this.addBalance = addBalance;
    }

    @action()
    async actionBalance({ changeRoute, isRedirect }: ActionProps, ctx: any, user: any) {
        if (!isRedirect) {
            switch (ctx.message.text) {
                case 'Back':
                    return await changeRoute('main');
                case 'Add':
                    return await changeRoute('balance/add');
            }
        }

        ctx.reply(user.money + '\nRedirect: ' + (isRedirect ? 1 : 0), Markup.keyboard(['Add', 'Back']).resize().extra());
    }

    @action('add')
    async actionAdd({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext, user: any) {
        if (!isRedirect) {
            if (!isRedirect) {
                switch (ctx.message.text) {
                    case 'Back':
                        return await changeRoute('balance');
                }
            }
            let sum = parseInt(ctx.message.text);
            if (!Number.isNaN(sum)) {
                await this.addBalance(user.id, sum);
                return await changeRoute('balance');
            }
        }

        ctx.reply('Enter sum:', Markup.keyboard(['Back']).resize().extra());
    }
}


async function updateUserBalance(userId: number, sum: number) {
    users[userId].money += sum;
}


let users = {};

async function updateUserRoute(route: string, routeData: any, userId: any) {
    users[userId].route = route;
    users[userId].routeData = routeData;
}

let router = new Router(
    [
        new MainController(),
        new BalanceController(updateUserBalance)
    ],
    updateUserRoute,
    [{
        rule: async ({ changeRoute, isRedirect }: ActionProps, ctx: TelegrafContext, user: any) => {
            return !user.isBanned;
        },
        onRuleError: async ({ changeRoute }: ActionProps, ctx: TelegrafContext, user: any) => {
            await ctx.reply('You are banned');
        }
    }],
    async ({ changeRoute }: ActionProps, ctx: TelegrafContext, user: any) => {
            await ctx.reply('Permission denied on router');
    }
]);

let telegraf = new Telegraf('<token>');

telegraf.on('text', async (ctx) => {
    if (!(ctx.from.id in users)) {
        users[ctx.from.id] = {
            id: ctx.from.id,
            isBanned: false,
            isUser: true,
            isAdmin: true,
            route: 'main',
            routeData: null,
            money: 0
        };
    }

    router.go(ctx.from.id, users[ctx.from.id].route, ctx, users[ctx.from.id]);
});

telegraf.launch();