Add DisconnectHandler
This commit is contained in:
parent
8a5743ef0c
commit
9001ccd11f
@ -39,5 +39,5 @@ from .client.types.reply_markup import (
|
||||
from .client import (
|
||||
Client, ChatAction, ParseMode, Emoji,
|
||||
MessageHandler, CallbackQueryHandler, RawUpdateHandler,
|
||||
Filters
|
||||
DisconnectHandler, Filters
|
||||
)
|
||||
|
@ -19,4 +19,7 @@
|
||||
from .client import Client
|
||||
from .ext import BaseClient, ChatAction, Emoji, ParseMode
|
||||
from .filters import Filters
|
||||
from .handlers import MessageHandler, CallbackQueryHandler, RawUpdateHandler
|
||||
from .handlers import (
|
||||
MessageHandler, CallbackQueryHandler,
|
||||
RawUpdateHandler, DisconnectHandler
|
||||
)
|
||||
|
@ -43,6 +43,7 @@ from pyrogram.api.errors import (
|
||||
PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded,
|
||||
PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned,
|
||||
VolumeLocNotFound, UserMigrate, FileIdInvalid)
|
||||
from pyrogram.client.handlers import DisconnectHandler
|
||||
from pyrogram.crypto import AES
|
||||
from pyrogram.session import Auth, Session
|
||||
from .dispatcher import Dispatcher
|
||||
@ -290,6 +291,9 @@ class Client(Methods, BaseClient):
|
||||
Returns:
|
||||
A tuple of (handler, group)
|
||||
"""
|
||||
if isinstance(handler, DisconnectHandler):
|
||||
self.disconnect_handler = handler.callback
|
||||
else:
|
||||
self.dispatcher.add_handler(handler, group)
|
||||
|
||||
return handler, group
|
||||
@ -308,6 +312,9 @@ class Client(Methods, BaseClient):
|
||||
group (``int``, *optional*):
|
||||
The group identifier, defaults to 0.
|
||||
"""
|
||||
if isinstance(handler, DisconnectHandler):
|
||||
self.disconnect_handler = None
|
||||
else:
|
||||
self.dispatcher.remove_handler(handler, group)
|
||||
|
||||
def authorize_bot(self):
|
||||
|
@ -75,6 +75,8 @@ class BaseClient:
|
||||
self.download_queue = Queue()
|
||||
self.download_workers_list = []
|
||||
|
||||
self.disconnect_handler = None
|
||||
|
||||
def send(self, data: Object):
|
||||
pass
|
||||
|
||||
|
@ -17,5 +17,6 @@
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from .callback_query_handler import CallbackQueryHandler
|
||||
from .disconnect_handler import DisconnectHandler
|
||||
from .message_handler import MessageHandler
|
||||
from .raw_update_handler import RawUpdateHandler
|
||||
|
25
pyrogram/client/handlers/disconnect_handler.py
Normal file
25
pyrogram/client/handlers/disconnect_handler.py
Normal file
@ -0,0 +1,25 @@
|
||||
# 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 DisconnectHandler(Handler):
|
||||
# TODO: Documentation
|
||||
def __init__(self, callback: callable):
|
||||
super().__init__(callback)
|
@ -17,9 +17,10 @@
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from .on_callback_query import OnCallbackQuery
|
||||
from .on_disconnect import OnDisconnect
|
||||
from .on_message import OnMessage
|
||||
from .on_raw_update import OnRawUpdate
|
||||
|
||||
|
||||
class Decorators(OnMessage, OnCallbackQuery, OnRawUpdate):
|
||||
class Decorators(OnMessage, OnCallbackQuery, OnRawUpdate, OnDisconnect):
|
||||
pass
|
||||
|
30
pyrogram/client/methods/decorators/on_disconnect.py
Normal file
30
pyrogram/client/methods/decorators/on_disconnect.py
Normal file
@ -0,0 +1,30 @@
|
||||
# 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/>.
|
||||
|
||||
import pyrogram
|
||||
from ...ext import BaseClient
|
||||
|
||||
|
||||
class OnDisconnect(BaseClient):
|
||||
def on_disconnect(self):
|
||||
# TODO: Documentation
|
||||
def decorator(func):
|
||||
self.add_handler(pyrogram.DisconnectHandler(func))
|
||||
return func
|
||||
|
||||
return decorator
|
@ -207,6 +207,12 @@ class Session:
|
||||
for i in self.results.values():
|
||||
i.event.set()
|
||||
|
||||
if self.client and callable(self.client.disconnect_handler):
|
||||
try:
|
||||
self.client.disconnect_handler(self.client)
|
||||
except Exception as e:
|
||||
log.error(e, exc_info=True)
|
||||
|
||||
log.debug("Session stopped")
|
||||
|
||||
def restart(self):
|
||||
|
Loading…
Reference in New Issue
Block a user