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-extended

v1.0.6

Published

Created with the aim of simplifying scene management and extracting repetitive logic into separate modules

Downloads

2

Readme

ActionMachine :sparkles:

Created with the aim of simplifying scene management and extracting repetitive logic into separate modules

Installation

NPM

npm i telegraf-extended

BUN

bun add telegraf-extended

Example

import { Context, Telegraf } from "telegraf";
import { ActionMachine, ActionMachineContext } from "telegraf-extended";

interface MyContext extends Context {
  action: ActionMachineContext;
}

const actionMachine = new ActionMachine<MyContext>();

const bot = new Telegraf<MyContext>(process.env.BOT_TOKEN);
bot.use(actionMachine.middleware());

bot.launch();

Create action

const getUserNameAction = new Action<MyContext>();

You can also specify the type state and payload

type User = {
  name: string;
};

type GetUserNameActionPayload = {
  update: boolean;
};

const getUserNameAction = new Action<
  MyContext,
  User,
  GetUserNameActionPayload
>();

Action API

send(ctx, payload?) - Called immediately after calling the enter function

filter(ctx) - Called after the user's response, the function should return true to continue execution and end the action

result(ctx) - Called immediately after the filter function ends, created to separate validation logic from database writing or any other business logic

getUserNameAction.send((ctx, payload) => {
  if (payload.update) {
    return ctx.reply("Your new name ?");
  }

  ctx.reply("What's your name?");
});

getUserNameAction.filter((ctx) => {
  if (!ctx.message.text) {
    ctx.reply("Enter text");
    return false;
  }

  if (ctx.message.text.length < 2 && ctx.message.text.length > 50) {
    ctx.reply("Nickname must be between 2 and 50 characters");
    return false;
  }

  return true;
});

getUserNameAction.result((ctx) => {
  ctx.action.state.user.name = ctx.message.text;
  // OR SAVE DB
});

You can omit creating filter and result functions as they are optional

Connect action to actionMachine

actionMachine.connect("getUserName", getUserNameAction);

Enter the action

ctx.action.enter("getUserName");

You can also specify an optional payload parameter

ctx.action.enter<GetUserNameActionPayload>("getUserName", { update: true });

Example

Update MyContext

interface SessionData extends WizardSessionData {
  user: UserEntity;
}

interface Session extends Scenes.WizardSession<SessionData> {}

interface MyContext extends Context {
  session: Session;
  scene: Scenes.SceneContextScene<MyContext, SessionData>;
  wizard: Scenes.WizardContextWizard<MyContext>;
  message: Update.New & Update.NonChannel & Message.TextMessage;
  action: ActionMachineContext;
}

Create scene

const createAccountScene = new Scenes.WizardScene<MyContext>(
  SceneTypes.CREATE_ACCOUNT,
  (ctx) => {
    ctx.action.state = {
      user: {
        ID: ctx.from?.id,
      },
    };

    ctx.action.enter(ActionTypes.GET_USER_NAME);
    ctx.action.enter(ActionTypes.GET_USER_GENDER);
    ctx.action.enter(ActionTypes.GET_USER_BIRTH_YEAR);
    ctx.action.enter(ActionTypes.GET_USER_LOCATION);

    ctx.wizard.next();
  },
  (ctx) => {
    const user = ctx.action.state.user;
    db.user.create(user);

    ctx.reply("Your account has been created successfully");
  }
);

You can call as many actions as you want at once, the actions will be executed sequentially, and after completing the entire action stack, the scene will return ctx