From d11692e69ee983d19d3e379f646db0f7f7291bcc Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 16 Feb 2026 12:16:57 +0000 Subject: [PATCH] Continue fixing issues with WhatsApp sync --- core/clients/whatsapp.py | 54 ++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/core/clients/whatsapp.py b/core/clients/whatsapp.py index de27748..379a1fc 100644 --- a/core/clients/whatsapp.py +++ b/core/clients/whatsapp.py @@ -2164,7 +2164,7 @@ class WhatsAppClient(ClientBase): payload={"presence": state_text, "sender": str(sender), "chat": str(chat)}, ) else: - await self.ur.stopped_typing( + await self.ur.stopped_typing( self.service, identifier=candidate, payload={"presence": state_text, "sender": str(sender), "chat": str(chat)}, @@ -2274,8 +2274,20 @@ class WhatsAppClient(ClientBase): async def send_message_raw(self, recipient, text=None, attachments=None): if not self._client: return False - jid = self._to_jid(recipient) - if not jid: + if self._build_jid is None: + return False + jid_str = self._to_jid(recipient) + if not jid_str: + return False + # Convert string JID to actual JID object that neonize expects + try: + jid = self._build_jid(jid_str) + # Verify it's a proper JID object with SerializeToString method + if not hasattr(jid, 'SerializeToString'): + self.log.error("whatsapp build_jid returned non-JID object: type=%s repr=%s", type(jid).__name__, repr(jid)[:100]) + return False + except Exception as exc: + self.log.warning("whatsapp failed to build JID from %s: %s", jid_str, exc) return False if not self._connected and hasattr(self._client, "connect"): try: @@ -2341,21 +2353,14 @@ class WhatsAppClient(ClientBase): last_error = None for attempt in range(3): try: + # Log what we're about to send for debugging + self.log.debug(f"send_message attempt {attempt+1}: jid={jid} text_type={type(text).__name__} text_len={len(text)}") response = await self._maybe_await(self._client.send_message(jid, text)) sent_any = True last_error = None break - except TypeError: - try: - response = await self._maybe_await( - self._client.send_message(jid, message=text) - ) - sent_any = True - last_error = None - break - except Exception as exc: - last_error = exc except Exception as exc: + self.log.debug(f"send_message attempt {attempt+1} failed: {type(exc).__name__}: {exc}") last_error = exc error_text = str(last_error or "").lower() @@ -2465,3 +2470,26 @@ class WhatsAppClient(ClientBase): return transport._as_qr_png(self._last_qr_payload) except Exception: return None + + async def send_message_to_contact(self, contact_jid: str, text: str) -> bool: + """Send a text message to a WhatsApp contact.""" + try: + jid = build_jid(contact_jid.split("@")[0], contact_jid.split("@")[1]) + # neonize.send_message() accepts either a Message protobuf or a plain string + # If passing a string, it auto-converts to Message(conversation=text) + response = self.client.send_message(jid, text) + return response is not None + except Exception as e: + self.log.error(f"Failed to send WhatsApp message: {e}") + return False + + # If you need to send a Message object explicitly: + async def send_structured_message(self, contact_jid: str, message: Message) -> bool: + """Send a structured Message protobuf to a WhatsApp contact.""" + try: + jid = build_jid(contact_jid.split("@")[0], contact_jid.split("@")[1]) + response = self.client.send_message(jid, message) + return response is not None + except Exception as e: + self.log.error(f"Failed to send structured WhatsApp message: {e}") + return False