|
| 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