Skip to content

Commit 0b1ba6d

Browse files
feat: add a method to get information about sessions (#41)
Co-authored-by: Alisson Lauffer <alissonvitortc@gmail.com>
1 parent 46dd578 commit 0b1ba6d

File tree

5 files changed

+196
-1
lines changed

5 files changed

+196
-1
lines changed

hydrogram/methods/auth/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .sign_in_bot import SignInBot
3333
from .sign_up import SignUp
3434
from .terminate import Terminate
35+
from .get_sessions import GetSessions
3536

3637

3738
class Auth(
@@ -50,5 +51,6 @@ class Auth(
5051
SignInBot,
5152
SignUp,
5253
Terminate,
54+
GetSessions,
5355
):
5456
pass
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Hydrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
3+
#
4+
# This file is part of Hydrogram.
5+
#
6+
# Hydrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Hydrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import logging
20+
21+
import hydrogram
22+
from hydrogram import raw, types
23+
24+
log = logging.getLogger(__name__)
25+
26+
27+
class GetSessions:
28+
async def get_sessions(
29+
self: "hydrogram.Client",
30+
) -> list["types.Session"]:
31+
"""Get your info data by other sessions .
32+
33+
Returns:
34+
List[:obj:`~hydrogram.types.Session`]: List of active sessions.
35+
"""
36+
37+
authorizations = await self.invoke(raw.functions.account.GetAuthorizations())
38+
return [types.Session._parse(auth) for auth in authorizations.authorizations]

hydrogram/types/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from .authorization import (
2121
SentCode,
22+
Session,
2223
TermsOfService,
2324
sent_code,
2425
terms_of_service,
@@ -355,6 +356,7 @@
355356
"Restriction",
356357
"SentCode",
357358
"SentWebAppMessage",
359+
"Session",
358360
"Sticker",
359361
"StrippedThumbnail",
360362
"TermsOfService",

hydrogram/types/authorization/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
1919

2020
from .sent_code import SentCode
21+
from .session import Session
2122
from .terms_of_service import TermsOfService
2223

23-
__all__ = ["SentCode", "TermsOfService"]
24+
__all__ = ["SentCode", "Session", "TermsOfService"]
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Hydrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
3+
#
4+
# This file is part of Hydrogram.
5+
#
6+
# Hydrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Hydrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from __future__ import annotations
20+
21+
from typing import TYPE_CHECKING
22+
23+
from hydrogram.types.object import Object
24+
25+
if TYPE_CHECKING:
26+
from hydrogram import raw
27+
28+
29+
class Session(Object):
30+
"""Contains info about a device session.
31+
32+
Parameters:
33+
id (``int``):
34+
Session id.
35+
36+
device_model (``str``):
37+
Device model used for authorization.
38+
39+
platform (``str``):
40+
Platform used for authorization.
41+
42+
system_version (``str``):
43+
System version used for authorization.
44+
45+
api_id (``int``):
46+
API ID used for authorization.
47+
48+
app_name (``str``):
49+
App name used for authorization.
50+
51+
app_version (``str``):
52+
App version used for authorization.
53+
54+
date_created (``int``):
55+
Date when authorization was created.
56+
57+
date_active (``int``):
58+
Date when authorization was last active.
59+
60+
ip (``str``):
61+
IP address used for authorization.
62+
63+
country (``str``):
64+
Country where authorization occurred.
65+
66+
region (``str``):
67+
Region where authorization occurred.
68+
69+
is_current (``bool``):
70+
Whether this is the current authorization.
71+
72+
is_official_app (``bool``):
73+
Whether this is an official app.
74+
75+
is_password_pending (``bool``):
76+
Whether a password is pending.
77+
78+
accepts_secret_chats (``bool``):
79+
Whether secret chat requests are allowed.
80+
81+
accepts_calls (``bool``):
82+
Whether call requests are allowed.
83+
84+
is_confirmed (``bool``):
85+
Whether the authorization is confirmed.
86+
"""
87+
88+
def __init__(
89+
self,
90+
*,
91+
id: int,
92+
device_model: str,
93+
platform: str,
94+
system_version: str,
95+
api_id: int,
96+
app_name: str,
97+
app_version: str,
98+
date_created: int,
99+
date_active: int,
100+
ip: str,
101+
country: str,
102+
region: str,
103+
is_current: bool,
104+
is_official_app: bool,
105+
is_password_pending: bool,
106+
accepts_secret_chats: bool,
107+
accepts_calls: bool,
108+
is_confirmed: bool,
109+
):
110+
super().__init__()
111+
112+
self.id = id
113+
self.device_model = device_model
114+
self.platform = platform
115+
self.system_version = system_version
116+
self.api_id = api_id
117+
self.app_name = app_name
118+
self.app_version = app_version
119+
self.date_created = date_created
120+
self.date_active = date_active
121+
self.ip = ip
122+
self.country = country
123+
self.region = region
124+
self.is_current = is_current
125+
self.is_official_app = is_official_app
126+
self.is_password_pending = is_password_pending
127+
self.accepts_secret_chats = accepts_secret_chats
128+
self.accepts_calls = accepts_calls
129+
self.is_confirmed = is_confirmed
130+
131+
@staticmethod
132+
def _parse(authorization: raw.types.Authorization) -> Session:
133+
return Session(
134+
id=authorization.hash,
135+
device_model=authorization.device_model,
136+
platform=authorization.platform,
137+
system_version=authorization.system_version,
138+
api_id=authorization.api_id,
139+
app_name=authorization.app_name,
140+
app_version=authorization.app_version,
141+
date_created=authorization.date_created,
142+
date_active=authorization.date_active,
143+
ip=authorization.ip,
144+
country=authorization.country,
145+
region=authorization.region,
146+
is_current=authorization.current,
147+
is_official_app=authorization.official_app,
148+
is_password_pending=authorization.password_pending,
149+
accepts_secret_chats=not authorization.encrypted_requests_disabled,
150+
accepts_calls=not authorization.call_requests_disabled,
151+
is_confirmed=not authorization.unconfirmed,
152+
)

0 commit comments

Comments
 (0)