mirror of
https://github.com/pyrogram/pyrogram
synced 2025-09-01 14:55:12 +00:00
Allow registering handlers using decorators
This commit is contained in:
@@ -36,6 +36,7 @@ from queue import Queue
|
||||
from signal import signal, SIGINT, SIGTERM, SIGABRT
|
||||
from threading import Event, Thread
|
||||
|
||||
import pyrogram
|
||||
from pyrogram.api import functions, types
|
||||
from pyrogram.api.core import Object
|
||||
from pyrogram.api.errors import (
|
||||
@@ -188,6 +189,25 @@ class Client:
|
||||
self.dispatcher = Dispatcher(self, workers)
|
||||
self.update_handler = None
|
||||
|
||||
def on(self, handler, group: int):
|
||||
def decorator(f):
|
||||
self.add_handler(handler(f), group)
|
||||
return f
|
||||
|
||||
return decorator
|
||||
|
||||
def on_message(self, group: int = 0):
|
||||
return self.on(pyrogram.MessageHandler, group)
|
||||
|
||||
def on_edited_message(self, group: int = 0):
|
||||
return self.on(pyrogram.EditedMessageHandler, group)
|
||||
|
||||
def on_channel_post(self, group: int = 0):
|
||||
return self.on(pyrogram.ChannelPostHandler, group)
|
||||
|
||||
def on_edited_channel_post(self, group: int = 0):
|
||||
return self.on(pyrogram.EditedChannelPostHandler, group)
|
||||
|
||||
def add_handler(self, handler: Handler, group: int = 0):
|
||||
self.dispatcher.add_handler(handler, group)
|
||||
|
||||
|
Reference in New Issue
Block a user