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("شروع!")

@bot.on_command(["start", "شروع"], ChatType("user"))
async def start_user(bot, event):
    await event.reply("شروع فقط در پیوی!")

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("بات خاموش شد!")