1.11.10 • Published 2 years ago

tg-io v1.11.10

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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();
1.11.4

2 years ago

1.11.3

2 years ago

1.11.2

2 years ago

1.10.3

2 years ago

1.11.1

2 years ago

1.10.2

2 years ago

1.11.8

2 years ago

1.11.7

2 years ago

1.11.6

2 years ago

1.11.5

2 years ago

1.9.1

2 years ago

1.11.9

2 years ago

1.11.10

2 years ago

1.9.7

2 years ago

1.9.6

2 years ago

1.9.5

2 years ago

1.9.4

2 years ago

1.9.3

2 years ago

1.9.2

2 years ago

1.11.0

2 years ago

1.10.1

2 years ago

1.10.0

2 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago