Skip to content

Commit

Permalink
Add missing parameters to search_messages and search_messages_count (#50
Browse files Browse the repository at this point in the history
)
  • Loading branch information
SpEcHiDe committed Jul 16, 2024
1 parent c77f096 commit 3d2aa8b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
| Scheme layer used: 184 |
+------------------------+

- Added the parameters ``offset_id`` to the :meth:`~pyrogram.Client.search_messages` and the parameters ``min_date``, ``max_date``, ``min_id``, ``max_id``, ``saved_messages_topic_id`` to the :meth:`~pyrogram.Client.search_messages_count`.
- Dynamic session ReStart + restart optimizations (`#56 <https://github.com/TelegramPlayGround/pyrogram/pull/56>`__)
- Added the :meth:`~pyrogram.Client.delete_account`, :meth:`~pyrogram.Client.transfer_chat_ownership`, :meth:`~pyrogram.Client.update_status` (`#49 <https://github.com/TelegramPlayGround/pyrogram/pull/49>`__, `#51 <https://github.com/TelegramPlayGround/pyrogram/pull/51>`__)
- Added the class :obj:`~pyrogram.types.RefundedPayment`, containing information about a refunded payment.
Expand Down
81 changes: 60 additions & 21 deletions pyrogram/methods/messages/search_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# 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 datetime import datetime
from typing import Union, List, AsyncGenerator, Optional

import pyrogram
Expand All @@ -27,31 +28,39 @@ async def get_chunk(
client,
chat_id: Union[int, str],
query: str = "",
filter: "enums.MessagesFilter" = enums.MessagesFilter.EMPTY,
offset: int = 0,
filter: "enums.MessagesFilter" = enums.MessagesFilter.EMPTY,
limit: int = 100,
from_user: Union[int, str] = None,
message_thread_id: int = None
message_thread_id: int = None,
offset_id: int = 0,
min_date: datetime = utils.zero_datetime(),
max_date: datetime = utils.zero_datetime(),
min_id: int = 0,
max_id: int = 0,
saved_messages_topic_id: Optional[Union[int, str]] = None
) -> List["types.Message"]:
r = await client.invoke(
raw.functions.messages.Search(
peer=await client.resolve_peer(chat_id),
q=query,
filter=filter.value(),
min_date=0,
max_date=0,
offset_id=0,
min_date=utils.datetime_to_timestamp(min_date),
max_date= utils.datetime_to_timestamp(max_date),
offset_id=offset_id,
add_offset=offset,
limit=limit,
min_id=0,
max_id=0,
min_id=min_id,
max_id=max_id,
from_id=(
await client.resolve_peer(from_user)
if from_user
else None
),
hash=0,
top_msg_id=message_thread_id
top_msg_id=message_thread_id,
saved_peer_id=await client.resolve_peer(saved_messages_topic_id) if saved_messages_topic_id else None
# saved_reaction:flags.3?Vector<Reaction>
),
sleep_threshold=60
)
Expand All @@ -65,11 +74,17 @@ async def search_messages(
self: "pyrogram.Client",
chat_id: Union[int, str],
query: str = "",
offset: int = 0,
filter: "enums.MessagesFilter" = enums.MessagesFilter.EMPTY,
limit: int = 0,
from_user: Union[int, str] = None,
message_thread_id: int = None
message_thread_id: int = None,
offset: int = 0,
limit: int = 0,
offset_id: int = 0,
min_date: datetime = utils.zero_datetime(),
max_date: datetime = utils.zero_datetime(),
min_id: int = 0,
max_id: int = 0,
saved_messages_topic_id: Optional[Union[int, str]] = None
) -> Optional[AsyncGenerator["types.Message", None]]:
"""Search for text and media messages inside a specific chat.
Expand All @@ -89,24 +104,42 @@ async def search_messages(
When passed while searching for media messages, the query will be applied to captions.
Defaults to "" (empty string).
offset (``int``, *optional*):
Sequential number of the first message to be returned.
Defaults to 0.
filter (:obj:`~pyrogram.enums.MessagesFilter`, *optional*):
Pass a filter in order to search for specific kind of messages only.
Defaults to any message (no filter).
limit (``int``, *optional*):
Limits the number of messages to be retrieved.
By default, no limit is applied and all messages are returned.
from_user (``int`` | ``str``, *optional*):
Unique identifier (int) or username (str) of the target user you want to search for messages from.
message_thread_id (``int``, *optional*):
Unique identifier for the target message thread (topic) of the forum; for forum supergroups only
offset (``int``, *optional*):
Sequential number of the first message to be returned.
Defaults to 0.
limit (``int``, *optional*):
Limits the number of messages to be retrieved.
By default, no limit is applied and all messages are returned.
offset_id (``int``, *optional*):
Identifier of the first message to be returned.
min_date (:py:obj:`~datetime.datetime`, *optional*):
Pass a date as offset to retrieve only older messages starting from that date.
max_date (:py:obj:`~datetime.datetime`, *optional*):
Pass a date as offset to retrieve only newer messages starting from that date.
min_id (``int``, *optional*):
If a positive value was provided, the method will return only messages with IDs more than min_id.
max_id (``int``, *optional*):
If a positive value was provided, the method will return only messages with IDs less than max_id.
saved_messages_topic_id (``int`` | ``str``, *optional*):
If not None, only messages in the specified Saved Messages topic will be returned; pass None to return all messages, or for chats other than Saved Messages.
Returns:
``Generator``: A generator yielding :obj:`~pyrogram.types.Message` objects.
Expand Down Expand Up @@ -137,11 +170,17 @@ async def search_messages(
client=self,
chat_id=chat_id,
query=query,
filter=filter,
offset=offset,
filter=filter,
limit=limit,
from_user=from_user,
message_thread_id=message_thread_id
message_thread_id=message_thread_id,
offset_id=offset_id,
min_date=min_date,
max_date=max_date,
min_id=min_id,
max_id=max_id,
saved_messages_topic_id=saved_messages_topic_id
)

if not messages:
Expand Down
40 changes: 32 additions & 8 deletions pyrogram/methods/messages/search_messages_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
# 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 Union
from datetime import datetime
from typing import Optional, Union

import pyrogram
from pyrogram import raw, enums
from pyrogram import enums, raw, utils


class SearchMessagesCount:
Expand All @@ -29,7 +30,12 @@ async def search_messages_count(
query: str = "",
filter: "enums.MessagesFilter" = enums.MessagesFilter.EMPTY,
from_user: Union[int, str] = None,
message_thread_id: int = None
message_thread_id: int = None,
min_date: datetime = utils.zero_datetime(),
max_date: datetime = utils.zero_datetime(),
min_id: int = 0,
max_id: int = 0,
saved_messages_topic_id: Optional[Union[int, str]] = None
) -> int:
"""Get the count of messages resulting from a search inside a chat.
Expand Down Expand Up @@ -58,28 +64,46 @@ async def search_messages_count(
message_thread_id (``int``, *optional*):
Unique identifier for the target message thread (topic) of the forum; for forum supergroups only
min_date (:py:obj:`~datetime.datetime`, *optional*):
Pass a date as offset to retrieve only older messages starting from that date.
max_date (:py:obj:`~datetime.datetime`, *optional*):
Pass a date as offset to retrieve only newer messages starting from that date.
min_id (``int``, *optional*):
If a positive value was provided, the method will return only messages with IDs more than min_id.
max_id (``int``, *optional*):
If a positive value was provided, the method will return only messages with IDs less than max_id.
saved_messages_topic_id (``int`` | ``str``, *optional*):
If not None, only messages in the specified Saved Messages topic will be returned; pass None to return all messages, or for chats other than Saved Messages.
Returns:
``int``: On success, the messages count is returned.
"""
r = await self.invoke(
raw.functions.messages.Search(
peer=await self.resolve_peer(chat_id),
q=query,
filter=filter.value(),
min_date=0,
max_date=0,
min_date=utils.datetime_to_timestamp(min_date),
max_date= utils.datetime_to_timestamp(max_date),
offset_id=0,
add_offset=0,
limit=1,
min_id=0,
max_id=0,
min_id=min_id,
max_id=max_id,
from_id=(
await self.resolve_peer(from_user)
if from_user
else None
),
hash=0,
top_msg_id=message_thread_id
top_msg_id=message_thread_id,
saved_peer_id=await self.resolve_peer(saved_messages_topic_id) if saved_messages_topic_id else None
# saved_reaction:flags.3?Vector<Reaction>
)
)

Expand Down

0 comments on commit 3d2aa8b

Please sign in to comment.