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

tg-io

v1.11.10

Published

Useful and fast library for Telegram Bots

Downloads

160

Readme

TG-IO

⚠️CAUTION

Library is still under development

Installation

npm install tg-io

Basic usage

import { Tg } from "tg-io";

const tg = new Tg(TOKEN);
tg.updates.hearCommand(/^\/echo (.+)/i, ctx => ctx.replyMessage(ctx.match[1]));

async function run() {
  await tg.startPolling();
}
run().catch(console.error);

Flexible way to interract with buttons

import { TgKeyboard } from "tg-io";

tg.updates.hearCommand(/^\/i wanna some buttons/i, async ctx => {
  let keyboard = tg.createKeyboard();
  let reqBtn = new TgKeyboard.Button("and me too..", "location");
  keyboard
    .createButton("click me NOW!")
    .add(reqBtn)
    .setOneTime()
    .setSelective("YES");

  return await ctx.replyMessage(keyboard.build());
});
tg.updates.hearCommand(/^\/i wanna some INLINE buttons/i, async ctx => {
  let keyboard = tg.createInlineKeyboard();
  keyboard
    .createButton({ text: "im a cb?", payload: "yes." })
    .createButton({
      text: "or maybe.. a link??",
      url: "https://www.youtube.com/watch?v=oHg5SJYRHA0",
    })
    .createButton({ text: "yep!", switchCurrentChatQuery: "nope..." });

  return await ctx.replyMessage({
    text: "ive some buttons :) ",
    ...keyboard.build(),
  });
});

Expandable basic entities

import { TgContext } from "tg-io";
class CustomMessageContext extends TgContext.Message {
  public isAdmin = () => this.sender.id === 1;
  public answer = (text: string) =>
    this.sendMessage(`${this.sender.appeal}, ${text}`);
}

tg.updates.setContext("message", CustomMessageContext);
tg.updates.hearCommand<CustomMessageContext>("/durovtest", ctx =>
  ctx.answer("u are... " + ctx.isAdmin ? "durov!" : "nobody..")
);

New elegant way to handle your commands

import { TgContext, TgCommand, TgEvent, TgEntity, TgUse } from "tg-io";

class UpdateHandler {
  public botname: string;

  @TgUse()
  public log(upd: TgEntity.IUpdateResult, next: () => void) {
    console.log(`received upd #${upd.update_id}`);
    next();
  }

  @TgEvent("photo")
  public async thankForPic(ctx: TgContext.Message, next: () => void) {
    await ctx.replyMessage("thx for a pic!");
    next();
  }

  @TgCommand(/info$/i)
  public sendInfo(ctx: TgContext.Message) {
    return ctx.sendMessage(`hello, i'm ${this.botname}`);
  }
}

let updHandler = new UpdateHandler();
updHandler.botname = "sample bot";
tg.updates.implementDecorators(updHandler);

Describe your commands comfortably

import { TgCommand, TgCommandInfo, TgContext } from "tg-io";

tg.commands
  .add("sample", "a sample command")
  .setScope("chat_administrators")
  .add("adm", "a secret command for admins")
  .resetScope()
  .setLanguage("in")
  .add("india", "खैर, आपने अनुवाद क्यों किया?")
  .resetLanguage();

class AdminCommandsHandler {
  constructor(private readonly botname: string) {}
  @TgCommandInfo<AdminCommandsHandler>("ping", h => `get ${h.botname}' ping`)
  @TgCommand(/^\/ping$/)
  public sendPing(ctx: TgContext.Message) {
    // some code here..
  }
}
tg.commands.implementDecorators(new AdminCommandsHandler("sample-bot"));
tg.uploadCommands();