mirror of
https://github.com/EvolutionAPI/evolution-client-python.git
synced 2025-07-13 15:14:48 -06:00
Refactor chat message retrieval in ChatService to include optional filters for message ID, sender, type, and timestamps. Update test_evolution.py to include commented-out examples for fetching group messages. Remove unused response print statements.
This commit is contained in:
parent
7e970f5f15
commit
1f915943e7
Binary file not shown.
Binary file not shown.
@ -1,4 +1,4 @@
|
|||||||
from typing import Union, BinaryIO
|
from typing import Union, BinaryIO, Optional
|
||||||
from ..models.chat import *
|
from ..models.chat import *
|
||||||
|
|
||||||
class ChatService:
|
class ChatService:
|
||||||
@ -68,13 +68,53 @@ class ChatService:
|
|||||||
instance_token=instance_token
|
instance_token=instance_token
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_messages(self, instance_id: str, remote_jid: str, instance_token: str, page: int = 1, offset: int = 50):
|
def get_messages(
|
||||||
'''Get messages from a chat'''
|
self,
|
||||||
|
instance_id: str,
|
||||||
|
remote_jid: str,
|
||||||
|
instance_token: str,
|
||||||
|
message_id: Optional[str] = None,
|
||||||
|
whatsapp_message_id: Optional[str] = None,
|
||||||
|
from_me: Optional[bool] = None,
|
||||||
|
message_type: Optional[str] = None,
|
||||||
|
source: Optional[str] = None,
|
||||||
|
timestamp_start: Optional[str] = None,
|
||||||
|
timestamp_end: Optional[str] = None,
|
||||||
|
page: int = 1,
|
||||||
|
offset: int = 50
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Obtém mensagens de um chat com filtros opcionais
|
||||||
|
|
||||||
|
Args:
|
||||||
|
timestamp_start: Data inicial no formato ISO (ex: "2025-01-16T00:00:00Z")
|
||||||
|
timestamp_end: Data final no formato ISO (ex: "2025-01-16T23:59:59Z")
|
||||||
|
'''
|
||||||
|
where = {"key": {"remoteJid": remote_jid}}
|
||||||
|
|
||||||
|
if message_id:
|
||||||
|
where["id"] = message_id
|
||||||
|
if whatsapp_message_id:
|
||||||
|
where["key"]["id"] = whatsapp_message_id
|
||||||
|
if from_me is not None:
|
||||||
|
where["key"]["fromMe"] = from_me
|
||||||
|
if message_type:
|
||||||
|
where["messageType"] = message_type
|
||||||
|
if source:
|
||||||
|
where["source"] = source
|
||||||
|
if timestamp_start or timestamp_end:
|
||||||
|
where["messageTimestamp"] = {}
|
||||||
|
if timestamp_start:
|
||||||
|
where["messageTimestamp"]["gte"] = timestamp_start
|
||||||
|
if timestamp_end:
|
||||||
|
where["messageTimestamp"]["lte"] = timestamp_end
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"where": {"key": {"remoteJid": remote_jid}},
|
"where": where,
|
||||||
"page": page,
|
"page": page,
|
||||||
"offset": offset,
|
"offset": offset,
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.client.post(
|
return self.client.post(
|
||||||
f'chat/findMessages/{instance_id}',
|
f'chat/findMessages/{instance_id}',
|
||||||
data=payload,
|
data=payload,
|
||||||
|
@ -14,9 +14,10 @@ client = EvolutionClient(
|
|||||||
instance_token = "60BDC703E413-4710-9473-CA2A763866FE"
|
instance_token = "60BDC703E413-4710-9473-CA2A763866FE"
|
||||||
instance_id = "94a0aafa-e636-4534-a185-5562bf8f2c22"
|
instance_id = "94a0aafa-e636-4534-a185-5562bf8f2c22"
|
||||||
|
|
||||||
response = client.group.fetch_all_groups(instance_id, instance_token, False)
|
# response = client.group.fetch_all_groups(instance_id, instance_token, False)
|
||||||
|
|
||||||
|
# print(response)
|
||||||
|
|
||||||
print(response)
|
|
||||||
|
|
||||||
# text_message = TextMessage(
|
# text_message = TextMessage(
|
||||||
# number="557499879409",
|
# number="557499879409",
|
||||||
@ -91,4 +92,20 @@ print(response)
|
|||||||
# delete_instance = client.instance_operations.delete(instance_id, instance_token)
|
# delete_instance = client.instance_operations.delete(instance_id, instance_token)
|
||||||
|
|
||||||
# print("Instância deletada")
|
# print("Instância deletada")
|
||||||
# print(delete_instance)
|
# print(delete_instance)
|
||||||
|
|
||||||
|
# group_id = "120363024931487276@g.us"
|
||||||
|
|
||||||
|
# # Buscando as 3 últimas mensagens do grupo
|
||||||
|
# mensagens = client.chat.get_messages(
|
||||||
|
# instance_id=instance_id,
|
||||||
|
# remote_jid=group_id,
|
||||||
|
# instance_token=instance_token,
|
||||||
|
# timestamp_start="2024-12-27T00:00:00Z",
|
||||||
|
# timestamp_end="2024-12-27T23:59:59Z",
|
||||||
|
# page=1,
|
||||||
|
# offset=3
|
||||||
|
# )
|
||||||
|
|
||||||
|
# print("Mensagens encontradas:")
|
||||||
|
# print(mensagens)
|
Loading…
Reference in New Issue
Block a user