mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 12:57:52 +00:00
Add handler for raw mtproto updates
This commit is contained in:
parent
66d03848de
commit
1a7ab62ed9
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -18,3 +18,4 @@
|
||||
|
||||
from .handler import Handler
|
||||
from .message_handler import MessageHandler
|
||||
from .raw_update_handler import RawUpdateHandler
|
||||
|
24
pyrogram/client/handler/raw_update_handler.py
Normal file
24
pyrogram/client/handler/raw_update_handler.py
Normal file
@ -0,0 +1,24 @@
|
||||
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
from .handler import Handler
|
||||
|
||||
|
||||
class RawUpdateHandler(Handler):
|
||||
def __init__(self, callback: callable):
|
||||
super().__init__(callback)
|
Loading…
x
Reference in New Issue
Block a user