Skip to content

Commit 2265d28

Browse files
authored
Add search_global_count and search_messages_count methods (pyrogram#768)
1 parent 0f204e1 commit 2265d28

File tree

3 files changed

+195
-2
lines changed

3 files changed

+195
-2
lines changed

pyrogram/methods/messages/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from .copy_message import CopyMessage
2019
from .copy_media_group import CopyMediaGroup
20+
from .copy_message import CopyMessage
2121
from .delete_messages import DeleteMessages
2222
from .download_media import DownloadMedia
2323
from .edit_inline_caption import EditInlineCaption
@@ -37,7 +37,9 @@
3737
from .read_history import ReadHistory
3838
from .retract_vote import RetractVote
3939
from .search_global import SearchGlobal
40+
from .search_global_count import SearchGlobalCount
4041
from .search_messages import SearchMessages
42+
from .search_messages_count import SearchMessagesCount
4143
from .send_animation import SendAnimation
4244
from .send_audio import SendAudio
4345
from .send_cached_media import SendCachedMedia
@@ -100,6 +102,8 @@ class Messages(
100102
SearchMessages,
101103
SearchGlobal,
102104
CopyMessage,
103-
CopyMediaGroup
105+
CopyMediaGroup,
106+
SearchMessagesCount,
107+
SearchGlobalCount
104108
):
105109
pass
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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 pyrogram import raw
20+
from pyrogram.scaffold import Scaffold
21+
from .search_messages import Filters, POSSIBLE_VALUES
22+
23+
24+
class SearchGlobalCount(Scaffold):
25+
async def search_global_count(
26+
self,
27+
query: str = "",
28+
filter: str = "empty",
29+
) -> int:
30+
"""Get the count of messages resulting from a global search.
31+
32+
If you want to get the actual messages, see :meth:`~pyrogram.Client.search_global`.
33+
34+
Parameters:
35+
query (``str``, *optional*):
36+
Text query string.
37+
Use "@" to search for mentions.
38+
39+
filter (``str``, *optional*):
40+
Pass a filter in order to search for specific kind of messages only:
41+
42+
- ``"empty"``: Search for all kind of messages (default).
43+
- ``"photo"``: Search for photos.
44+
- ``"video"``: Search for video.
45+
- ``"photo_video"``: Search for either photo or video.
46+
- ``"document"``: Search for documents (generic files).
47+
- ``"url"``: Search for messages containing URLs (web links).
48+
- ``"animation"``: Search for animations (GIFs).
49+
- ``"voice_note"``: Search for voice notes.
50+
- ``"audio"``: Search for audio files (music).
51+
- ``"chat_photo"``: Search for chat photos.
52+
- ``"audio_video_note"``: Search for either audio or video notes.
53+
- ``"video_note"``: Search for video notes.
54+
- ``"location"``: Search for location messages.
55+
- ``"contact"``: Search for contact messages.
56+
57+
Returns:
58+
``int``: On success, the messages count is returned.
59+
"""
60+
try:
61+
filter = Filters.__dict__[filter.upper()]
62+
except KeyError:
63+
raise ValueError('Invalid filter "{}". Possible values are: {}'.format(
64+
filter, ", ".join(f'"{v}"' for v in POSSIBLE_VALUES))) from None
65+
66+
r = await self.send(
67+
raw.functions.messages.SearchGlobal(
68+
q=query,
69+
filter=filter,
70+
min_date=0,
71+
max_date=0,
72+
offset_rate=0,
73+
offset_peer=raw.types.InputPeerEmpty(),
74+
offset_id=0,
75+
limit=1
76+
)
77+
)
78+
79+
if hasattr(r, "count"):
80+
return r.count
81+
else:
82+
return len(r.messages)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <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 typing import Union
20+
21+
from pyrogram import raw
22+
from pyrogram.scaffold import Scaffold
23+
from .search_messages import Filters, POSSIBLE_VALUES
24+
25+
26+
class SearchMessagesCount(Scaffold):
27+
async def search_messages_count(
28+
self,
29+
chat_id: Union[int, str],
30+
query: str = "",
31+
filter: str = "empty",
32+
from_user: Union[int, str] = None
33+
) -> int:
34+
"""Get the count of messages resulting from a search inside a chat.
35+
36+
If you want to get the actual messages, see :meth:`~pyrogram.Client.search_messages`.
37+
38+
Parameters:
39+
chat_id (``int`` | ``str``):
40+
Unique identifier (int) or username (str) of the target chat.
41+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
42+
For a contact that exists in your Telegram address book you can use his phone number (str).
43+
44+
query (``str``, *optional*):
45+
Text query string.
46+
Required for text-only messages, optional for media messages (see the ``filter`` argument).
47+
When passed while searching for media messages, the query will be applied to captions.
48+
Defaults to "" (empty string).
49+
50+
filter (``str``, *optional*):
51+
Pass a filter in order to search for specific kind of messages only:
52+
53+
- ``"empty"``: Search for all kind of messages (default).
54+
- ``"photo"``: Search for photos.
55+
- ``"video"``: Search for video.
56+
- ``"photo_video"``: Search for either photo or video.
57+
- ``"document"``: Search for documents (generic files).
58+
- ``"url"``: Search for messages containing URLs (web links).
59+
- ``"animation"``: Search for animations (GIFs).
60+
- ``"voice_note"``: Search for voice notes.
61+
- ``"audio"``: Search for audio files (music).
62+
- ``"chat_photo"``: Search for chat photos.
63+
- ``"phone_call"``: Search for phone calls.
64+
- ``"audio_video_note"``: Search for either audio or video notes.
65+
- ``"video_note"``: Search for video notes.
66+
- ``"mention"``: Search for messages containing mentions to yourself.
67+
- ``"location"``: Search for location messages.
68+
- ``"contact"``: Search for contact messages.
69+
- ``"pinned"``: Search for pinned messages.
70+
71+
from_user (``int`` | ``str``, *optional*):
72+
Unique identifier (int) or username (str) of the target user you want to search for messages from.
73+
74+
Returns:
75+
``int``: On success, the messages count is returned.
76+
"""
77+
try:
78+
filter = Filters.__dict__[filter.upper()]
79+
except KeyError:
80+
raise ValueError('Invalid filter "{}". Possible values are: {}'.format(
81+
filter, ", ".join(f'"{v}"' for v in POSSIBLE_VALUES))) from None
82+
83+
r = await self.send(
84+
raw.functions.messages.Search(
85+
peer=await self.resolve_peer(chat_id),
86+
q=query,
87+
filter=filter,
88+
min_date=0,
89+
max_date=0,
90+
offset_id=0,
91+
add_offset=0,
92+
limit=1,
93+
min_id=0,
94+
max_id=0,
95+
from_id=(
96+
await self.resolve_peer(from_user)
97+
if from_user
98+
else None
99+
),
100+
hash=0
101+
)
102+
)
103+
104+
if hasattr(r, "count"):
105+
return r.count
106+
else:
107+
return len(r.messages)

0 commit comments

Comments
 (0)