Skip to content

Greyewi/tg-builder

Repository files navigation

🤖TG Builder

tg-builder provides an intuitive builder pattern that allows developers to:

  • Combine Multiple Bots: Run and manage several bots from a single codebase, each with their individual or shared functionalities.
  • Shared Logic: Easily reuse logic across different bots, making your code more DRY (Don't Repeat Yourself).
  • Modularity: Break down complex bot functionalities into manageable pieces, ensuring clean and readable code.
  • Scalability: As your bot ecosystem grows, tg-builder scales with you, making it effortless to add or modify existing bots.

By adopting the builder pattern, tg-builder ensures your bots are more maintainable, organized, and efficient.

📦 Installation

Using npm:

npm install tg-builder

Using yarn:

yarn add tg-builder

⚙️ Configuration

Before you start, you need to create a configuration file named tg.json in your project's root. This file should contain the token and name of the bot you want to set up.

{
  "bots": [
    {
      "token": "xxxxxxxxxxxxxxxxxxxx",
      "name": "GreyewiRogueBot"
    },
    {
      "token": "xxxxxxxxxxxxxxxxxxxx",
      "name": "GreyewiRogueBot"
    }
  ]
}

Replace xxxxxxxxxxxxxxxxxxxx with your actual bot tokens.

🛑 Remember, always keep your bot tokens secret and never expose them in publicly accessible places or repositories.

📖 Getting Started

1. Importing Modules

Begin by importing the essential modules:

import { BotsLoader, CommandBuilder, ActionBuilder } from 'tg-builder'

2. Defining Commands

Leverage the CommandBuilder to define the commands for your bot:

const gameCommands = (botName: string) =>
new CommandBuilder(botName).setNewCommand('start', async (ctx) => {
    //... command logic here ...
});

3. Defining Actions

Utilize the ActionBuilder to define telegram callback actions:

const gameActions = (botName: string) =>
    new ActionBuilder(botName)
    .setNewAction('new-game', async (ctx: any) => {
        //... action logic here ...
});

4. Loading and Launching the Bot

Make use of BotsLoader to add your bot and get it running:

const loader = new BotsLoader();
loader.addBot(name, gameCommands, gameActions);

loader.launch((err) => {
    if (!err) {
        console.log(`${name} is working`);
    } else {
        console.error(err);
    }
});

🚀 Example

Here's a glimpse of a bot providing a text-based RPG game experience:

import { BotsLoader, CommandBuilder, ActionBuilder } from 'tg-builder'

const name = "GreyewiRogueBot"
const gameCommands = (botName: string) =>
  new CommandBuilder(botName).setNewCommand('start', async (ctx) => {
      await ctx.reply(`You are welcome in super text tg rpg game!:`, {
        parse_mode: 'HTML',
        reply_markup: {
          inline_keyboard: [
            [
              {
                text: 'Start',
                callback_data: 'new-game',
              },
              {
                text: 'Continue',
                callback_data: 'loading',
              },
              {
                text: 'Options',
                callback_data: 'settings',
              },
            ],
          ],
        },
      })
  })

const gameActions = (botName: string) =>
  new ActionBuilder(botName)
    .setNewAction('new-game', async (ctx: any) => {
      return ctx.reply('Coming soon')
    })
    .setNewAction('loading', async (ctx: any) => {
      return ctx.reply('Coming soon')
    })
    .setNewAction('settings', async (ctx: any) => {
      return ctx.reply('Coming soon')
    })

const loader = new BotsLoader()
loader.addBot(name, gameCommands, gameActions)

loader.launch((err) => {
  if (!err) {
    console.log(`${name} is working`)
  } else {
    console.error(err)
  }
})

📄 Definitions

1. Command

PropertyTypeDescription
commandstringThe command name to be recognized by the bot.
callback(ctx: Context) => voidThe callback function executed when the command is called.
botNamestringName of the bot the command belongs to.

2. Action

PropertyTypeDescription
actionstringThe action identifier.
callback(ctx: Context) => voidThe callback function executed when the action is triggered.
botNamestringName of the bot the action belongs to.

With tg-builder, bot development becomes a structured, readable, and maintainable endeavor. Dive in to explore more functionalities and elevate your bot development journey!

About

Builder library for the organisation of a lot of telegram bots.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published