From 90a4e4c41131bae5025ccfe010da9228edd1bc28 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 6 Apr 2018 20:38:34 +0200 Subject: [PATCH] Allow registering handlers using decorators --- pyrogram/client/client.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 74c2d01c..cd7fe030 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -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)