2.8.2 • Published 15 days ago

telegramsjs v2.8.2

Weekly downloads
-
License
MIT
Repository
github
Last release
15 days ago

⚙️ Introduction

Telegramsjs is a lightweight Node.js library for creating Telegram bots. It is designed to be a framework and does not rely on third-party libraries such as Telegraf.js or node-telegram-bot-api. The library has a syntax that is similar to Discord.js. This library provides a way to create Telegram bots with ease and flexibility.

⚒️ Installation

You can install the telegramsjs library using npm:

npm install telegramsjs

📙 Usage

First, you need to create a new instance of the TelegramBot class by providing your Telegram bot token:

const {
  TelegramBot,
  IntentsBitField,
  IntentBits,
  Events,
} = require("telegramsjs");
const intents = new IntentsBitField().add(
  IntentBits.Message,
  IntentBits.EditedMessage,
  IntentBits.EditedChannelPost,
  IntentBits.ChannelPost,
  IntentBits.InlineQuery,
  IntentBits.ChosenInlineResult,
  IntentBits.CallbackQuery,
  IntentBits.ShippingQuery,
  IntentBits.PreCheckoutQuery,
  IntentBits.Poll,
  IntentBits.PollAnswer,
  IntentBits.MyChatMember,
  IntentBits.ChatMember,
);

const bot = new TelegramBot("TELEGRAM_BOT_TOKEN", {
  intents: intents.toArray(),
});

The intents option is an array of Telegram bot API Update types, that the bot should receive.

After creating the bot instance, you can listen to events using the on method. The on method takes two arguments: the event name and the callback function.

bot.on('eventName', (arg1, arg2, ...) => {
  // your code here
});

The library provides several events that you can listen to, including:

  • ready: Fires when the bot is ready to start.
  • message: Fires when a new message is received.
  • callback_query: Fires when a callback query is received.

Here's an example of how to listen to the message event:

bot.on("message", message => {
  // your code here
});

To handle commands, you can use the commands property, which is a Collection object that stores the commands and their corresponding code.

const { Collection } = require("telegramsjs");

bot.commands = new Collection();

You can add commands to the commands collection using the set method.

bot.commands.set("/help", {
  description: "Displays help information.",
  code: (bot, message) => {
    message.chat.send({
      text: "Here are some helpful tips...",
    });
  },
});

The code property is a callback function that is called when the command is executed.

To handle multiple commands, you can create a separate file for each command and load them using a loader function.

async function loadCommands(dir) {
  const fs = require("fs");
  const path = require("path");

  const readDirRecursive = async directory => {
    const files = await fs.readdirSync(directory);
    for (const file of files) {
      const filePath = path.join(directory, file);
      if (fs.statSync(filePath).isDirectory()) {
        await readDirRecursive(filePath);
      } else if (file.endsWith(".js")) {
        const pull = require(filePath);
        bot.commands.set(pull.command, pull);
      }
    }
  };

  await readDirRecursive(path.join(process.cwd(), dir));
}

loadCommands("./commands/");

The loader function reads all the files in the ./commands/ directory and adds each command to the commands collection.

Now you can create a file with the .js extension for each command:

module.exports = {
  command: "/start",
  description: "Start command",
  code: (bot, message) => {
    message.chat.send({
      text: "Starting...",
    });
  },
};

You can set the bot's description and commands using the setMyDescription and setCommands methods respectively.

bot.once("ready", async client => {
  await client.setDescription({
    description: "My awesome Telegram bot",
  });

  await client.setCommands({
    commands: JSON.stringify(bot.commands.toArray()),
  });
});

The setMyDescription and setDescription methods set the bot's description, while the setMyCommands and setCommands methods set the bot's commands.

🎃 Conclusion

Telegramsjs provides a simple and flexible way to create Telegram bots using Node.js. With its easy-to-use syntax and event-driven architecture, it is a great choice for developers who want to build bots quickly and efficiently.

📖 Documentation

Telegramsjs Telegramsjs Documentation-1 Telegramsjs Documentation-2 Collection Formatters

🎒 Contributions

We welcome your contributions to the development of Telegramsjs! If you have any ideas or suggestions, please visit the Official support server or the Official Telegram channel.

📒 Example

To see the correct use of the library, see the page on GitHub.

🧾 License

Telegramsjs is available under the MIT license. For more information, please refer to the LICENSE file.

3.0.0-beta.24

15 days ago

3.0.0-beta.23

1 month ago

3.0.0-beta.1

1 month ago

3.0.0-beta.3

1 month ago

3.0.0-beta.2

1 month ago

3.0.0-beta.5

1 month ago

3.0.0-beta.4

1 month ago

3.0.0-beta.7

1 month ago

3.0.0-beta.6

1 month ago

3.0.0-beta.9

1 month ago

3.0.0-beta.20

1 month ago

3.0.0-beta.21

1 month ago

3.0.0-beta.22

1 month ago

3.0.0-beta.19

1 month ago

3.0.0-beta.10

1 month ago

3.0.0-beta.11

1 month ago

3.0.0-beta.12

1 month ago

3.0.0-beta.13

1 month ago

3.0.0-beta.14

1 month ago

3.0.0-beta.15

1 month ago

3.0.0-beta.16

1 month ago

3.0.0-beta.17

1 month ago

3.0.0-beta.18

1 month ago

2.8.1

2 months ago

2.8.0

2 months ago

2.8.2

2 months ago

2.7.0

3 months ago

2.6.0

4 months ago

2.4.1

6 months ago

2.4.0

6 months ago

2.3.5

6 months ago

2.5.1-beta.1

5 months ago

2.5.1-beta.2

5 months ago

2.5.0-beta.1

6 months ago

2.5.0-beta.2

6 months ago

2.5.0

6 months ago

2.5.1

5 months ago

2.3.0-beta.1

6 months ago

2.3.0

6 months ago

2.3.2

6 months ago

2.3.1

6 months ago

2.3.3

6 months ago

2.2.0

7 months ago

1.7.2-beta.1

9 months ago

1.0.0

10 months ago

0.8.5

11 months ago

0.8.4

11 months ago

1.7.0-beta.1

9 months ago

0.8.7

11 months ago

0.8.6

11 months ago

1.6.1

9 months ago

1.6.0

9 months ago

2.0.2

7 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.6.11

9 months ago

1.6.10

9 months ago

1.7.5

9 months ago

1.7.4

9 months ago

0.9.0

11 months ago

0.7.2

11 months ago

0.7.1

11 months ago

0.9.2

11 months ago

0.7.4

11 months ago

0.9.1

11 months ago

0.7.3

11 months ago

1.7.4-beta.1

9 months ago

0.7.0

11 months ago

1.7.4-beta.2

9 months ago

0.7.5

11 months ago

1.7.3

9 months ago

1.7.2

9 months ago

1.7.1

9 months ago

1.7.0

9 months ago

1.5.0

10 months ago

2.1.1

7 months ago

2.0.0-beta.2

8 months ago

1.5.0-beta.20230716

10 months ago

2.0.0-beta.1

8 months ago

1.5.0-beta.20230714

10 months ago

1.5.0-beta.20230712

10 months ago

1.5.0-beta.20230713

10 months ago

2.1.0

7 months ago

2.0.0-beta.3

8 months ago

2.1.0-beta.1

7 months ago

1.6.5-beta.1

9 months ago

1.0.0-beta.2

11 months ago

1.0.0-beta.3

11 months ago

1.0.0-beta.4

11 months ago

1.0.0-beta.5

11 months ago

1.0.0-beta.1

11 months ago

0.6.0-bata.3

11 months ago

1.6.9

9 months ago

1.6.8

9 months ago

1.0.0-beta.6

11 months ago

1.6.7

9 months ago

1.0.0-beta.7

10 months ago

0.6.0-bata.2

11 months ago

1.6.6

9 months ago

1.6.5

9 months ago

0.8.1

11 months ago

0.8.0

11 months ago

0.8.3

11 months ago

0.8.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.6.0-bata.1

11 months ago

0.5.1

12 months ago

0.5.0

12 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.3.0

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago