diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index e366ae20..18813d55 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -34,4 +34,4 @@ from .client.input_media_photo import InputMediaPhoto from .client.input_media_video import InputMediaVideo from .client.input_phone_contact import InputPhoneContact from .client import Emoji -from .client.handler import MessageHandler +from .client.handler import MessageHandler, RawUpdateHandler diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index e67876d9..12030a9c 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -189,13 +189,19 @@ class Client: self.dispatcher = Dispatcher(self, workers) self.update_handler = None - def on_message(self, group: int = 0): + def on(self, handler, group): def decorator(f): - self.add_handler(pyrogram.MessageHandler(f), group) + 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_raw_update(self, group: int = 0): + return self.on(pyrogram.RawUpdateHandler, group) + def add_handler(self, handler: Handler, group: int = 0): self.dispatcher.add_handler(handler, group) diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index a5ae9049..fb4e46a8 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -26,7 +26,7 @@ import pyrogram from pyrogram.api import types from . import message_parser from ..handler import ( - Handler, MessageHandler + Handler, MessageHandler, RawUpdateHandler ) log = logging.getLogger(__name__) @@ -79,23 +79,28 @@ class Dispatcher: ) ) - def dispatch(self, update): - message = (update.message - or update.channel_post - or update.edited_message - or update.edited_channel_post) - - if message: - key = MessageHandler - value = message + def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool = False): + if is_raw: + key = RawUpdateHandler + value = update else: - return + message = (update.message + or update.channel_post + or update.edited_message + or update.edited_channel_post) + + if message: + key = MessageHandler + value = message + else: + return for group in self.handlers.values(): handler = group.get(key, None) if handler is not None: - handler.callback(self.client, value) + args = (self, value, users, chats) if is_raw else (self.client, value) + handler.callback(*args) def update_worker(self): name = threading.current_thread().name @@ -112,6 +117,8 @@ class Dispatcher: chats = {i.id: i for i in update[2]} update = update[0] + self.dispatch(update, users=users, chats=chats, is_raw=True) + if isinstance(update, Dispatcher.ALLOWED_UPDATES): if isinstance(update.message, types.Message): parser = message_parser.parse_message diff --git a/pyrogram/client/handler/__init__.py b/pyrogram/client/handler/__init__.py index c86ca6ec..be0bfb56 100644 --- a/pyrogram/client/handler/__init__.py +++ b/pyrogram/client/handler/__init__.py @@ -18,3 +18,4 @@ from .handler import Handler from .message_handler import MessageHandler +from .raw_update_handler import RawUpdateHandler diff --git a/pyrogram/client/handler/raw_update_handler.py b/pyrogram/client/handler/raw_update_handler.py new file mode 100644 index 00000000..758606db --- /dev/null +++ b/pyrogram/client/handler/raw_update_handler.py @@ -0,0 +1,24 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .handler import Handler + + +class RawUpdateHandler(Handler): + def __init__(self, callback: callable): + super().__init__(callback)