Visitar URL original
Merge branch 'user-status' into develop · ryuuzake/pyrogram@8a81bd1 · GitHub
Skip to content

Commit 8a81bd1

Browse files
committed
Merge branch 'user-status' into develop
2 parents c7a3865 + 5b17376 commit 8a81bd1

15 files changed

Lines changed: 254 additions & 10 deletions

File tree

‎compiler/api/compiler.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ def start():
506506
f.write("\n 0xb0700028: \"pyrogram.client.types.Dialog\",")
507507
f.write("\n 0xb0700029: \"pyrogram.client.types.Dialogs\",")
508508
f.write("\n 0xb0700030: \"pyrogram.client.types.ChatMembers\",")
509+
f.write("\n 0xb0700031: \"pyrogram.client.types.UserStatus\"")
509510

510511
f.write("\n}\n")
511512

‎docs/source/pyrogram/Types.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Users & Chats
1010
:nosignatures:
1111

1212
User
13+
UserStatus
1314
Chat
1415
ChatPhoto
1516
ChatMember
@@ -73,6 +74,9 @@ Input Media
7374
.. autoclass:: User
7475
:members:
7576

77+
.. autoclass:: UserStatus
78+
:members:
79+
7680
.. autoclass:: Chat
7781
:members:
7882

‎pyrogram/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
from .client.types import (
3030
Audio, Chat, ChatMember, ChatMembers, ChatPhoto, Contact, Document, InputMediaPhoto,
3131
InputMediaVideo, InputMediaDocument, InputMediaAudio, InputMediaAnimation, InputPhoneContact,
32-
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, Update, User,
32+
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, Update, User, UserStatus,
3333
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
3434
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
3535
)
3636
from .client import (
3737
Client, ChatAction, ParseMode, Emoji,
3838
MessageHandler, DeletedMessagesHandler, CallbackQueryHandler,
39-
RawUpdateHandler, DisconnectHandler, Filters
39+
RawUpdateHandler, DisconnectHandler, UserStatusHandler, Filters
4040
)

‎pyrogram/client/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
from .handlers import (
2323
MessageHandler, DeletedMessagesHandler,
2424
CallbackQueryHandler, RawUpdateHandler,
25-
DisconnectHandler
25+
DisconnectHandler, UserStatusHandler
2626
)

‎pyrogram/client/dispatcher/dispatcher.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import pyrogram
2626
from pyrogram.api import types
2727
from ..ext import utils
28-
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler
28+
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler
2929

3030
log = logging.getLogger(__name__)
3131

@@ -108,6 +108,8 @@ def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool
108108

109109
callback_query = update.callback_query
110110

111+
user_status = update.user_status
112+
111113
if message and isinstance(handler, MessageHandler):
112114
if not handler.check(message):
113115
continue
@@ -123,6 +125,11 @@ def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool
123125
continue
124126

125127
args = (self.client, callback_query)
128+
elif user_status and isinstance(handler, UserStatusHandler):
129+
if not handler.check(user_status):
130+
continue
131+
132+
args = (self.client, user_status)
126133
else:
127134
continue
128135

@@ -209,6 +216,14 @@ def update_worker(self):
209216
)
210217
)
211218
)
219+
elif isinstance(update, types.UpdateUserStatus):
220+
self.dispatch(
221+
pyrogram.Update(
222+
user_status=utils.parse_user_status(
223+
update.status, update.user_id
224+
)
225+
)
226+
)
212227
else:
213228
continue
214229
except Exception as e:

‎pyrogram/client/ext/utils.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ def parse_chat_photo(photo):
129129
)
130130

131131

132+
def parse_user_status(user_status, user_id: int = None, is_bot: bool = False) -> pyrogram_types.UserStatus or None:
133+
if is_bot:
134+
return None
135+
136+
status = pyrogram_types.UserStatus(user_id)
137+
138+
if isinstance(user_status, types.UserStatusOnline):
139+
status.online = True
140+
status.date = user_status.expires
141+
elif isinstance(user_status, types.UserStatusOffline):
142+
status.offline = True
143+
status.date = user_status.was_online
144+
elif isinstance(user_status, types.UserStatusRecently):
145+
status.recently = True
146+
elif isinstance(user_status, types.UserStatusLastWeek):
147+
status.within_week = True
148+
elif isinstance(user_status, types.UserStatusLastMonth):
149+
status.within_month = True
150+
else:
151+
status.long_time_ago = True
152+
153+
return status
154+
155+
132156
def parse_user(user: types.User) -> pyrogram_types.User or None:
133157
return pyrogram_types.User(
134158
id=user.id,
@@ -142,7 +166,8 @@ def parse_user(user: types.User) -> pyrogram_types.User or None:
142166
username=user.username,
143167
language_code=user.lang_code,
144168
phone_number=user.phone,
145-
photo=parse_chat_photo(user.photo)
169+
photo=parse_chat_photo(user.photo),
170+
status=parse_user_status(user.status, is_bot=user.bot),
146171
) if user else None
147172

148173

‎pyrogram/client/handlers/__init__.py‎

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

1919
from .callback_query_handler import CallbackQueryHandler
20+
from .deleted_messages_handler import DeletedMessagesHandler
2021
from .disconnect_handler import DisconnectHandler
2122
from .message_handler import MessageHandler
22-
from .deleted_messages_handler import DeletedMessagesHandler
2323
from .raw_update_handler import RawUpdateHandler
24+
from .user_status_handler import UserStatusHandler
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram 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+
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .handler import Handler
20+
21+
22+
class UserStatusHandler(Handler):
23+
"""The UserStatus handler class. Used to handle user status updates (user going online or offline).
24+
It is intended to be used with :meth:`add_handler() <pyrogram.Client.add_handler>`
25+
26+
For a nicer way to register this handler, have a look at the
27+
:meth:`on_user_status() <pyrogram.Client.on_user_status>` decorator.
28+
29+
Args:
30+
callback (``callable``):
31+
Pass a function that will be called when a new UserStatus update arrives. It takes *(client, user_status)*
32+
as positional arguments (look at the section below for a detailed description).
33+
34+
filters (:obj:`Filters <pyrogram.Filters>`):
35+
Pass one or more filters to allow only a subset of messages to be passed
36+
in your callback function.
37+
38+
Other parameters:
39+
client (:obj:`Client <pyrogram.Client>`):
40+
The Client itself, useful when you want to call other API methods inside the user status handler.
41+
42+
user_status (:obj:`UserStatus <pyrogram.UserStatus>`):
43+
The received UserStatus update.
44+
"""
45+
46+
def __init__(self, callback: callable, filters=None):
47+
super().__init__(callback, filters)
48+
49+
def check(self, user_status):
50+
return (
51+
self.filters(user_status)
52+
if callable(self.filters)
53+
else True
54+
)

‎pyrogram/client/methods/decorators/__init__.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from .on_callback_query import OnCallbackQuery
20+
from .on_deleted_messages import OnDeletedMessages
2021
from .on_disconnect import OnDisconnect
2122
from .on_message import OnMessage
22-
from .on_deleted_messages import OnDeletedMessages
2323
from .on_raw_update import OnRawUpdate
24+
from .on_user_status import OnUserStatus
2425

2526

26-
class Decorators(OnMessage, OnDeletedMessages, OnCallbackQuery, OnRawUpdate, OnDisconnect):
27+
class Decorators(
28+
OnMessage,
29+
OnDeletedMessages,
30+
OnCallbackQuery,
31+
OnRawUpdate,
32+
OnDisconnect,
33+
OnUserStatus
34+
):
2735
pass
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram 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+
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import pyrogram
20+
from ...ext import BaseClient
21+
22+
23+
class OnUserStatus(BaseClient):
24+
def on_user_status(self, filters=None, group: int = 0):
25+
"""Use this decorator to automatically register a function for handling
26+
user status updates. This does the same thing as :meth:`add_handler` using the
27+
:class:`UserStatusHandler`.
28+
29+
Args:
30+
filters (:obj:`Filters <pyrogram.Filters>`):
31+
Pass one or more filters to allow only a subset of UserStatus updated to be passed in your function.
32+
33+
group (``int``, *optional*):
34+
The group identifier, defaults to 0.
35+
"""
36+
37+
def decorator(func):
38+
self.add_handler(pyrogram.UserStatusHandler(func, filters), group)
39+
return func
40+
41+
return decorator

0 commit comments

Comments
 (0)