From 7b4de7c00d5a790487daca22895aa72e89584968 Mon Sep 17 00:00:00 2001 From: LabFox Date: Sat, 28 Sep 2024 18:36:19 +0200 Subject: [PATCH] Add connection state checks and auto-reconnect on event handler Implemented `LoggedInWrapper` and `ConnectedWrapper` to check the connection and login status of the WhatsApp client. Updated Python bindings and handlers to utilize these new wrappers, also added an auto-reconnect on the event handler, fixing #37 --- whatsfly/dependencies/main.go | 23 +++++++++++++++++++++++ whatsfly/dependencies/wapp.h | 2 ++ whatsfly/whatsapp.py | 26 +++++++++++++++++++++++++- whatsfly/whatsmeow.py | 6 ++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/whatsfly/dependencies/main.go b/whatsfly/dependencies/main.go index 0990cb8..5732f07 100644 --- a/whatsfly/dependencies/main.go +++ b/whatsfly/dependencies/main.go @@ -390,6 +390,9 @@ func (w *WhatsAppClient) MessageThread() { w.runMessageThread = true for { if w.wpClient != nil { + if !w.wpClient.IsConnected() { + w.wpClient.Connect() + } var is_logged_in_now = w.wpClient.IsLoggedIn() if w.isLoggedIn != is_logged_in_now { @@ -712,6 +715,26 @@ func DisconnectWrapper(id C.int) { w.Disconnect(nil) } +//export ConnectedWrapper +func ConnectedWrapper(id C.int) C.int { + w := handles[int(id)] + if w.wpClient.IsConnected() { + return C.int(1) + } else { + return C.int(0) + } +} + +//export LoggedInWrapper +func LoggedInWrapper(id C.int) C.int { + w := handles[int(id)] + if w.wpClient.IsLoggedIn() { + return C.int(1) + } else { + return C.int(0) + } +} + //export MessageThreadWrapper func MessageThreadWrapper(id C.int) { w := handles[int(id)] diff --git a/whatsfly/dependencies/wapp.h b/whatsfly/dependencies/wapp.h index 8a80914..0052661 100644 --- a/whatsfly/dependencies/wapp.h +++ b/whatsfly/dependencies/wapp.h @@ -23,6 +23,8 @@ extern "C" { extern int NewWhatsAppClientWrapper(char* c_phone_number, char* c_media_path, ptr_to_pyfunc fn_disconnect_callback, ptr_to_pyfunc_str fn_event_callback); extern void ConnectWrapper(int id); extern void DisconnectWrapper(int id); + extern int LoggedInWrapper(int id); + extern int ConnectedWrapper(int id); extern void MessageThreadWrapper(int id); extern int SendMessageProtobufWrapper(int id, char* c_number, char* c_msg, bool is_group); extern int SendMessageWithUploadWrapper(int id, char* c_phone_number, char* c_message, bool is_group, int upload_id, char* c_upload_id, char* kind); diff --git a/whatsfly/whatsapp.py b/whatsfly/whatsapp.py index 5c620b3..40a27a6 100644 --- a/whatsfly/whatsapp.py +++ b/whatsfly/whatsapp.py @@ -19,7 +19,7 @@ set_group_name_wrapper, set_group_topic_wrapper, get_group_info_wrapper, - upload_file_wrapper + upload_file_wrapper, logged_in_wrapper, connected_wrapper ) from .proto.waE2E import WAWebProtobufsE2E_pb2 import ctypes @@ -63,6 +63,7 @@ class WhatsApp: """ The main whatsapp handler """ + c_WhatsAppClientId = None def __init__( self, @@ -180,6 +181,29 @@ def _handleMessage(self, message): for handler in self._userEventHandlers: handler(message) + def loggedIn(self) -> bool: + """ + Determines if the user is logged into WhatsApp. + + Returns: + bool: True if the user is logged in, False otherwise. + """ + if self.c_WhatsAppClientId == None: + return False + return logged_in_wrapper(self.c_WhatsAppClientId) == 1 + + def isConnected(self) -> bool: + """ + + Checks if the connection is currently established. + + Returns: + bool: True if the client is connected, otherwise False. + """ + if self.c_WhatsAppClientId == None: + return False + return connected_wrapper(self.c_WhatsAppClientId) == 1 + def sendMessage(self, phone: str, message, group: bool = False, upload: Upload = None): """ Sends a text message diff --git a/whatsfly/whatsmeow.py b/whatsfly/whatsmeow.py index 6477ffc..bb97018 100644 --- a/whatsfly/whatsmeow.py +++ b/whatsfly/whatsmeow.py @@ -45,6 +45,12 @@ disconnect_wrapper = lib.DisconnectWrapper disconnect_wrapper.argstype = [ctypes.c_int] +logged_in_wrapper = lib.LoggedInWrapper +logged_in_wrapper.argstype = [ctypes.c_int] + +connected_wrapper = lib.ConnectedWrapper +connected_wrapper.argstype = [ctypes.c_int] + message_thread_wrapper = lib.MessageThreadWrapper message_thread_wrapper.argstype = [ctypes.c_int]