Skip to content

Commit

Permalink
add listChats
Browse files Browse the repository at this point in the history
  • Loading branch information
3mora2 committed Jul 22, 2024
1 parent 30a6aa0 commit 3271ae4
Show file tree
Hide file tree
Showing 7 changed files with 1,638 additions and 69 deletions.
6 changes: 3 additions & 3 deletions WPP_Whatsapp/api/Whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ def downloadFile(self, data: str, timeout=120):

def downloadMedia(self, messageId: str | dict, timeout=120):
return self.ThreadsafeBrowser.run_threadsafe(self.downloadMedia_, messageId, timeout_=timeout)

def takeScreenshot(self, timeout_=120, **kwargs):
return self.ThreadsafeBrowser.run_threadsafe(self.takeScreenshot_,**kwargs, timeout_=timeout_)
return self.ThreadsafeBrowser.run_threadsafe(self.takeScreenshot_, **kwargs, timeout_=timeout_)

def useHere(self, timeout=120):
return self.ThreadsafeBrowser.run_threadsafe(self.useHere_, timeout_=timeout)
Expand Down Expand Up @@ -153,13 +154,12 @@ async def downloadMedia_(self, messageId: str | dict):
return await self.ThreadsafeBrowser.page_evaluate(
"async (messageId) => WPP.util.blobToBase64(await WPP.chat.downloadMedia(messageId))", messageId,
page=self.page)

async def takeScreenshot_(self, **kwargs):
return await self.ThreadsafeBrowser.create_task(
self.page.screenshot(**kwargs)
)



async def useHere_(self):
return await self.ThreadsafeBrowser.page_evaluate("() => WAPI.takeOver()", page=self.page)

Expand Down
153 changes: 106 additions & 47 deletions WPP_Whatsapp/api/layers/RetrieverLayer.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,121 @@
from WPP_Whatsapp.api.layers.SenderLayer import SenderLayer
from WPP_Whatsapp.api.model import ChatListOptions


class RetrieverLayer(SenderLayer):
def getSessionTokenBrowser(self, removePath, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getSessionTokenBrowser_, removePath, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getSessionTokenBrowser_(removePath), timeout_=timeout)

def getTheme(self, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getTheme_, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getTheme_(), timeout_=timeout)

def getAllChats(self, withNewMessageOnly=False, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllChats_, withNewMessageOnly, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllChats_(withNewMessageOnly), timeout_=timeout)

def listChats(self, options: ChatListOptions = {}, timeout=60):
"""
/**
* Return list of chats
* * @example
* ```javascript
* // All chats
* const chats = await client.listChats();
*
* // Some chats
* const chats = client.listChats({count: 20});
*
* // 20 chats before specific chat
* const chats = client.listChats({count: 20, direction: 'before', id: '[number]@c.us'});
*
* // Only users chats
* const chats = await client.listChats({onlyUsers: true});
*
* // Only groups chats
* const chats = await client.listChats({onlyGroups: true});
*
* // Only with label Text
* const chats = await client.listChats({withLabels: ['Test']});
*
* // Only with label id
* const chats = await client.listChats({withLabels: ['1']});
*
* // Only with label with one of text or id
* const chats = await client.listChats({withLabels: ['Alfa','5']});
* ```
* @category Chat
* @returns array of [Chat]
*/
"""
return self.ThreadsafeBrowser.run_threadsafe(self.listChats_(options), timeout_=timeout)

def checkNumberStatus(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.checkNumberStatus_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.checkNumberStatus_(contactId), timeout_=timeout)

def getAllChatsWithMessages(self, withNewMessageOnly=False, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllChatsWithMessages_, withNewMessageOnly, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllChatsWithMessages_(withNewMessageOnly), timeout_=timeout)

def getAllGroups(self, withNewMessagesOnly=False, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllGroups_, withNewMessagesOnly, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllGroups_(withNewMessagesOnly), timeout_=timeout)

def getAllBroadcastList(self, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllBroadcastList_, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllBroadcastList_(), timeout_=timeout)

def getContact(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getContact_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getContact_(contactId), timeout_=timeout)

def getAllContacts(self, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllContacts_, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllContacts_(), timeout_=timeout)

def getChatById(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getChatById_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getChatById_(contactId), timeout_=timeout)

def getChat(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getChat_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getChat_(contactId), timeout_=timeout)

def getProfilePicFromServer(self, chatId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getProfilePicFromServer_, chatId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getProfilePicFromServer_(chatId), timeout_=timeout)

def loadEarlierMessages(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.loadEarlierMessages_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.loadEarlierMessages_(contactId), timeout_=timeout)

def getStatus(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getStatus_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getStatus_(contactId), timeout_=timeout)

def getNumberProfile(self, contactId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getNumberProfile_, contactId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getNumberProfile_(contactId), timeout_=timeout)

def getUnreadMessages(self, includeMe, includeNotifications, useUnreadCount, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getUnreadMessages_, includeMe, includeNotifications, useUnreadCount, timeout_=timeout)
self.getUnreadMessages_(includeMe, includeNotifications, useUnreadCount), timeout_=timeout)

def getAllUnreadMessages(self, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllUnreadMessages_, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllUnreadMessages_(), timeout_=timeout)

def getAllNewMessages(self, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllNewMessages_, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getAllNewMessages_(), timeout_=timeout)

def getAllMessagesInChat(self, chatId, includeMe=False, includeNotifications=False, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getAllMessagesInChat_, chatId, includeMe, includeNotifications, timeout_=timeout)
self.getAllMessagesInChat_(chatId, includeMe, includeNotifications), timeout_=timeout)

def loadAndGetAllMessagesInChat(self, chatId, includeMe=False, includeNotifications=False, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.loadAndGetAllMessagesInChat_, chatId, includeMe, includeNotifications, timeout_=timeout)
self.loadAndGetAllMessagesInChat_(chatId, includeMe, includeNotifications), timeout_=timeout)

def getChatIsOnline(self, chatId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getChatIsOnline_, chatId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getChatIsOnline_(chatId), timeout_=timeout)

def getLastSeen(self, chatId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getLastSeen_, chatId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getLastSeen_(chatId), timeout_=timeout)

def getPlatformFromMessage(self, msgId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getPlatformFromMessage_, msgId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getPlatformFromMessage_(msgId), timeout_=timeout)

def getReactions(self, msgId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getReactions_, msgId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getReactions_(msgId), timeout_=timeout)

def getVotes(self, msgId, timeout=60):
return self.ThreadsafeBrowser.run_threadsafe(
self.getVotes_, msgId, timeout_=timeout)
return self.ThreadsafeBrowser.run_threadsafe(self.getVotes_(msgId), timeout_=timeout)

########################################################
async def getSessionTokenBrowser_(self, removePath):
Expand Down Expand Up @@ -144,6 +159,50 @@ async def getAllChats_(self, withNewMessageOnly=False):

return await self.ThreadsafeBrowser.page_evaluate("() => WAPI.getAllChats()", page=self.page)

async def listChats_(self, options: ChatListOptions = {}):
"""
/**
* Return list of chats
* * @example
* ```javascript
* // All chats
* const chats = await client.listChats();
*
* // Some chats
* const chats = client.listChats({count: 20});
*
* // 20 chats before specific chat
* const chats = client.listChats({count: 20, direction: 'before', id: '[number]@c.us'});
*
* // Only users chats
* const chats = await client.listChats({onlyUsers: true});
*
* // Only groups chats
* const chats = await client.listChats({onlyGroups: true});
*
* // Only with label Text
* const chats = await client.listChats({withLabels: ['Test']});
*
* // Only with label id
* const chats = await client.listChats({withLabels: ['1']});
*
* // Only with label with one of text or id
* const chats = await client.listChats({withLabels: ['Alfa','5']});
* ```
* @category Chat
* @returns array of [Chat]
*/
"""
return await self.ThreadsafeBrowser.page_evaluate(
"""
async ({ options }) => {
const chats = await WPP.chat.list(options);
const serialized = chats.map((c) => WAPI._serializeChatObj(c));
return serialized;
}
""", {"options": options}, page=self.page)

async def checkNumberStatus_(self, contactId):
# @returns contact detial as promise
contactId = self.valid_chatId(contactId)
Expand Down
Loading

0 comments on commit 3271ae4

Please sign in to comment.