پرش به مطلب اصلی

on

متدی برای گوش دادن به رویدادهای مختلف از سمت سرور روبیکا (update, inline, error).

توضیح​

با استفاده از متد on، می‌تونی برای رویدادهای خاص یک هندلر ثبت کنی. این متد از overload پشتیبانی می‌کنه و می‌تونه فیلتر هم بگیره.

ورودی ها​

نامنوعتوضیح
typekeyof typeof bot.handlersنوع رویداد ( "update"، "inline" , "events")
filtersNestedFilter<ContextMap[T]> (اختیاری)آرایه‌ای از فیلترها برای محدود کردن شرط اجرای هندلر
handler(ctx: ContextMap[T]) => Promise<void>تابعی که هنگام وقوع رویداد اجرا می‌شود

مثال ها​

⚠ توجه: بسته به نوع رویداد ("update" , "inline" , "events")، آرگومان ctx می‌تونه یک Update , Inline , یا Event باشه

استفاده ساده​

import Bot from "rubika/bot";

const bot = new Bot("YOUR_TOKEN");

bot.on("update", async (ctx) => {
await ctx.reply("شما یک پیام فرستادید!");
});

bot.on("events", async (ctx) => {
if (ctx.event_data.type === EventTypeEnum.BotPermissionsChanged)
await ctx.reply("Bot permissions changed");
});

bot.run();

استفاده با فیلتر​

import Bot, { Filters } from "rubika/bot";

const bot = new Bot("YOUR_TOKEN");

bot.on("update", [Filters.isText], async (ctx) => {
await ctx.reply("شما یک پیام متنی فرستادید!");
});

bot.run();

استفاده پیشرفته​

import Bot from "rubika/bot"

const bot = new Bot("YOUR_TOKEN");

type StoreType = {
user: string;
group: string;
};

bot.on<StoreType, "update">("update", async (ctx) => {
console.log(ctx.store.group);
});

bot.run();