neonize icon indicating copy to clipboard operation
neonize copied to clipboard

Implement `get_message_history` Function for Retrieving Chat History by JID

Open Eikosa opened this issue 10 months ago • 1 comments

Currently, the Neonize library provides excellent functionality for interacting with WhatsApp in real-time. However, there appears to be a missing feature for retrieving historical messages from a specific chat (user or group) identified by its JID.

Users often need to access past conversations programmatically, for tasks like:

  • Archiving specific conversations.
  • Searching for information within a chat's history.
  • Building bots that need context from previous messages.
  • Syncing chat history to another platform.

Proposed Functionality:

We propose adding a new method, potentially named get_message_history or similar, to the Neonize client. This method would accept a JID and optional parameters for pagination and return a list of past messages from that chat.

Desired Behavior:

  1. Input: Takes at least the target jid (e.g., [email protected] or [email protected]) as an argument.
  2. Output: Returns a list of message objects (ideally similar in structure to the message objects received via event handlers).
  3. Ordering: Messages should be returned in reverse chronological order (most recent first, i.e., "bottom to top"), mirroring how users typically view chat history.
  4. Pagination: Include parameters like limit (to specify the maximum number of messages to retrieve per call) and potentially an offset or before_message_id parameter to fetch older batches of messages.

Analogy (Similar to Pyrogram):

This functionality would be analogous to the get_chat_history method found in libraries like Pyrogram, which provides a convenient way to iterate through a chat's message history:

# Pyrogram Example (Illustrative)
async for message in client.get_chat_history(chat_id, limit=100):
    print(message.text)

Example Usage (Proposed for Neonize):

# Desired Neonize Implementation Example
from neonize.client import NewClient # Assuming 'client' is an initialized Neonize client instance

target_jid = "[email protected]" # Or a group JID

try:
    # Get the 50 most recent messages
    messages = await client.get_message_history(jid=target_jid, limit=50)

    if messages:
        print(f"Retrieved {len(messages)} messages from {target_jid}:")
        for msg in messages:
            # Process each message object (assuming it has attributes like 'text', 'sender', 'timestamp')
            sender_jid = msg.sender.jid # Hypothetical attribute
            message_text = msg.message.conversation # Hypothetical attribute for text
            timestamp = msg.info.timestamp # Hypothetical attribute
            print(f"[{timestamp}] {sender_jid}: {message_text}")

        # Example: Fetching older messages (pagination)
        oldest_message_in_batch = messages[-1]
        # Assuming the message object has an ID suitable for pagination
        # Or maybe the function handles pagination internally or via an offset
        older_messages = await client.get_message_history(
            jid=target_jid,
            limit=50,
            before_message_id=oldest_message_in_batch.id # Hypothetical parameter
        )
        print(f"\nRetrieved {len(older_messages)} older messages.")

    else:
        print(f"No message history found or unable to retrieve for {target_jid}.")

except Exception as e:
    print(f"An error occurred: {e}")

Conclusion:

Adding a get_message_history function would significantly enhance Neonize's capabilities, making it more versatile for applications requiring access to past chat data. We believe this would be a valuable addition for many users.

Eikosa avatar Apr 04 '25 00:04 Eikosa

This library doesn't actually save messages You can do that yourself (Using an SQL/postgres database)

Nubuki-all avatar Jun 16 '25 15:06 Nubuki-all