Skip to content

دکوراتورها


on_new_message

فقط پیام‌های جدید را دریافت می‌کند.

Python
@bot.on_new_message()
async def handler(bot, event):
    await event.reply("پیام جدید دریافت شد!")

on_edit_message

فقط پیام‌های ویرایش شده را دریافت می‌کند.

Python
@bot.on_edit_message()
async def handler(bot, event):
    await event.reply("پیام ویرایش شد!")

on_delete_message

فقط پیام‌های حذف شده را دریافت می‌کند.

Python
@bot.on_delete_message()
async def handler(bot, event):
    print(f"پیام {event.deleted_message_id} حذف شد.")

on_message

همه نوع پیام (جدید، ویرایش شده، حذف شده، callback) را دریافت می‌کند.

Python
@bot.on_message()
async def handler(bot, event):
    print(f"رویداد: {event.update_type}")

on_callback

فقط کلیک روی دکمه‌های اینلاین (شیشه‌ای) را دریافت می‌کند.

Python
@bot.on_callback()
async def handler(bot, event):
    await event.reply(f"دکمه {event.text} کلیک شد!")

on_command

میانبر برای دریافت دستورات. معادل @bot.on_new_message(Command("name")).

Python
@bot.on_command("start")
async def start(bot, event):
    await event.reply("شروع!")

on_update

همه نوع آپدیت‌ها را دریافت می‌کند.

Python
@bot.on_update()
async def update(bot, event):
    print(event)

on_event_data

رویدادهای مربوط به بات را دریافت می‌کند.

Python
@bot.on_event_data()
async def handler(bot, event):
    print(event)

on_bot_joined

رویداد مربوط به اضافه شدنِ بات در چت را دریافت می‌کند.

Python
@bot.on_bot_joined()
async def handler(bot, event):
    print(f"بات به چت {event.chat_id} اضافه شد.")

on_bot_removed

رویداد مربوط به حذف شدنِ بات از چت را دریافت می‌کند.

Python
@bot.on_bot_removed()
async def handler(bot, event):
    print(f"بات از چت {event.chat_id} حذف شد.")

on_bot_permissions_changed

رویداد مربوط به تغییر دسترسی بات در چت را دریافت می‌کند.

Python
@bot.on_bot_permissions_changed()
async def handler(bot, event):
    print(f"دسترسی بات در چت {event.chat_id} تغییر کرد.")

on_started_bot

رویداد مربوط به شروع بات را دریافت می‌کند.

Python
@bot.on_started_bot()
async def handler(bot, event):
    print("بات شروع شد.")

on_stopped_bot

رویداد مربوط به توقف بات را دریافت می‌کند.

Python
@bot.on_stopped_bot()
async def handler(bot, event):
    print("بات متوقف شد.")

middleware

لایه میانی - قبل و بعد از همه هندلرها اجرا می‌شود. باید await call_next() صدا زده شود.

Python
@bot.middleware()
async def logger(bot, event, call_next):
    print(f"[{event.update_type}] {event.chat_id}: {event.text}")
    await call_next()

on_start

هنگام شروع بات اجرا می‌شود.

Python
@bot.on_start()
async def startup(bot):
    print("بات روشن شد!")

on_shutdown

هنگام خاموشی بات اجرا می‌شود.

Python
@bot.on_shutdown()
async def shutdown(bot):
    print("بات خاموش شد!")