دکوراتورها
on_new_message
فقط پیامهای جدید را دریافت میکند.
Python
@bot.on_new_message()
async def handler(bot, event):
await event.reply("پیام جدید دریافت شد!")
on_edit_message
فقط پیامهای ویرایش شده را دریافت میکند.
on_delete_message
فقط پیامهای پاک شده را دریافت میکند.
Python
@bot.on_delete_message()
async def handler(bot, event):
print(f"پیام {event.deleted_message_id} پاک شد")
on_message
همه نوع پیام (جدید، ویرایش، پاک، callback، شروع بات، توقف بات) را دریافت میکند.
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
هنگام شروع بات اجرا میشود.
on_shutdown
هنگام خاموشی بات اجرا میشود.