Skip to content

Commit

Permalink
Add connection state checks and auto-reconnect on event handler
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Labfox committed Sep 28, 2024
1 parent f5ce70a commit 7b4de7c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
23 changes: 23 additions & 0 deletions whatsfly/dependencies/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions whatsfly/dependencies/wapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 25 additions & 1 deletion whatsfly/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +63,7 @@ class WhatsApp:
"""
The main whatsapp handler
"""
c_WhatsAppClientId = None

def __init__(
self,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions whatsfly/whatsmeow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 7b4de7c

Please sign in to comment.