Skip to content

Commit 4c367aa

Browse files
Add session in connect/disconnect handler
1 parent 0448585 commit 4c367aa

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

pyrogram/handlers/connect_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class ConnectHandler(Handler):
3737
client (:obj:`~pyrogram.Client`):
3838
The Client itself. Useful, for example, when you want to change the proxy before a new connection
3939
is established.
40+
41+
session (:obj:`~pyrogram.session.Session`):
42+
The Session used for the connection.
4043
"""
4144

4245
def __init__(self, callback: Callable):

pyrogram/handlers/disconnect_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class DisconnectHandler(Handler):
3737
client (:obj:`~pyrogram.Client`):
3838
The Client itself. Useful, for example, when you want to change the proxy before a new connection
3939
is established.
40+
41+
session (:obj:`~pyrogram.session.Session`):
42+
The Session used for the connection.
4043
"""
4144

4245
def __init__(self, callback: Callable):

pyrogram/session/session.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,6 @@ async def start(self):
146146

147147
self.ping_task = self.client.loop.create_task(self.ping_worker())
148148

149-
if not self.is_media and callable(self.client.connect_handler):
150-
try:
151-
await self.client.connect_handler(self.client)
152-
except Exception as e:
153-
log.exception(e)
154-
155149
log.info("Session initialized: Pyrogram v%s (Layer %s)", pyrogram.__version__, layer)
156150
log.info("Device: %s - %s", self.client.device_model, self.client.app_version)
157151
log.info("System: %s (%s)", self.client.system_version, self.client.lang_code)
@@ -173,7 +167,19 @@ async def start(self):
173167

174168
log.info("Session started")
175169

170+
if callable(self.client.connect_handler):
171+
try:
172+
await self.client.connect_handler(self.client, self)
173+
except Exception as e:
174+
log.exception(e)
175+
176176
async def stop(self):
177+
if callable(self.client.disconnect_handler):
178+
try:
179+
await self.client.disconnect_handler(self.client, self)
180+
except Exception as e:
181+
log.exception(e)
182+
177183
self.is_started.clear()
178184

179185
self.stored_msg_ids.clear()
@@ -190,12 +196,6 @@ async def stop(self):
190196
if self.recv_task:
191197
await self.recv_task
192198

193-
if not self.is_media and callable(self.client.disconnect_handler):
194-
try:
195-
await self.client.disconnect_handler(self.client)
196-
except Exception as e:
197-
log.exception(e)
198-
199199
log.info("Session stopped")
200200

201201
async def restart(self):

0 commit comments

Comments
 (0)