Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from .unpin_all_chat_messages import UnpinAllChatMessages
from .unpin_chat_message import UnpinChatMessage
from .update_chat_username import UpdateChatUsername
from .get_chat_onlines import GetChatOnlines


class Chats(
Expand Down Expand Up @@ -92,6 +93,7 @@ class Chats(
DeleteUserHistory,
UnpinAllChatMessages,
MarkChatUnread,
GetChatEventLog
GetChatEventLog,
GetChatOnlines
):
pass
59 changes: 59 additions & 0 deletions pyrogram/methods/chats/get_chat_onlines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 Dan <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 typing import Optional, Union

@alissonlauffer alissonlauffer Apr 4, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import when applying the suggestion below.

Suggested change
from typing import Optional, Union
from typing import Union

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.


from pyrogram import raw
from pyrogram.scaffold import Scaffold


class GetChatOnlines(Scaffold):
async def get_chat_onlines(
self,
chat_id: Union[int, str]
) -> Optional[int]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function does not return None. Just int or raise an exception.

Suggested change
) -> Optional[int]:
) -> int:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😳

"""Get count of online users in a chat.

Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.

Returns:
``int``: On success, the chat members online count is returned.

Raises:
ValueError: In case a chat id belongs to user.

Example:
.. code-block:: python

onlines = app.get_chat_onlines("pyrogram")
print(onlines)
"""
peer = await self.resolve_peer(chat_id)

if isinstance(peer, (raw.types.InputPeerChat, raw.types.InputPeerChannel)):
r = await self.send(
raw.functions.messages.GetOnlines(
peer=peer
)
)

return r.onlines
else:
raise ValueError(f'The chat_id "{chat_id}" belongs to a user')