rubigraf
v1.3.0
Published
A Powerful and Modern Rubika Bot Framework
Maintainers
Readme
✨ Overview
Rubigraf is designed to simplify Rubika bot development using a clean, event-driven, and type-safe structure inspired by Telegraf.
📘 Full Documentation → (coming soon)
🚀 Installation
npm install rubigraf
# or
yarn add rubigraf
# or
pnpm add rubigraf🧩 Example Usage
Below are a few examples demonstrating Rubigraf's event-based structure.
- NewMessage Event Example:
const { Rubigraf, RubigrafEvents } = require("rubigraf");
const bot = new Rubigraf(process.env.BOT_TOKEN, { polling: true });
bot.launch();
bot.on(RubigrafEvents.NewMessage, async (ctx, payload, next) => {
await ctx.reply("Hello! 👋 This is a Rubigraf bot.");
return next();
});- Command Event Example with Event Hooks:
const { Rubigraf, RubigrafEvents } = require("rubigraf");
const bot = new Rubigraf(process.env.BOT_TOKEN, { polling: true });
bot.launch();
// onBefore: inspect the ctx and attach shared data to payload
// If user not registered, set payload.registered = false so main handler & after hook see it.
bot.onBefore(RubigrafEvents.Command, async (ctx, payload, next) => {
const userId = ctx.senderId;
// lightweight DB check (example)
const isRegistered = await db.users.exists(userId);
payload.registered = isRegistered; // shared with later hooks
payload.userId = userId;
// allow main handler to continue
return next();
});
// main handler: do work and augment payload if needed
bot.on(RubigrafEvents.Command, async (ctx, payload, next) => {
const { command } = ctx;
if (command === "start") {
if (!payload.registered) {
// register user and record result for after-hook
await db.users.create(payload.userId);
payload.registered = true;
payload.justRegistered = true;
}
}
// continue to after hook
return next();
});
// onAfter: run follow-up tasks using the same payload object
bot.onAfter(RubigrafEvents.Command, async (ctx, payload) => {
// payload.registered / payload.justRegistered are available here
if (ctx.command === "start" && payload.justRegistered) {
await ctx.reply("Welcome to Rubigraf!");
} else if (ctx.command === "start") {
await ctx.reply("Welcome back!");
}
// you can also use payload for telemetry/logging
globalMetrics.increment("commands_handled", { cmd: ctx.command, registered: !!payload.registered });
});
// You can also get Rubigraf errors from version 1.2.8 onwards
bot.on(RubigrafEvents.Error, (err, logger) => logger.error(err));🧩 TypeScript Usage
Rubigraf includes full type definitions, so you can use it seamlessly with TypeScript.
📦 Features
- ⚡ Fast and modular event system
- 💬 Supports messages, files, stickers, polls, and more
- 🧠 Middleware-based architecture
- 🧱 TypeScript support out of the box
- 🔁 Command and context handling similar to Telegraf
- 🧍 User-friendly API design
🧠 Concepts
Rubigraf provides contexts to interact with updates. Each context gives you access to specific update data and helper methods such as ctx.reply(), ctx.forwardMessage(), and ctx.editMessageText().
Example:
bot.on(RubigrafEvents.Command, async (ctx) => {
if (ctx.command === "start") {
await ctx.reply("Welcome to Rubigraf!");
}
});📜 License
Rubigraf is licensed under the MIT License.
💬 Contributing
Contributions, issues, and feature requests are welcome!
Feel free to check the issues page.
