From 59396bdbef4a2ac1ae16edb83473bb11468c57f4 Mon Sep 17 00:00:00 2001 From: comadreja Date: Thu, 26 Mar 2026 21:48:30 -0500 Subject: [PATCH] fix(whatsapp): detect phone vs LID by JID suffix, not field name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bridge's pn/sender fields don't consistently map to phone/LID across different versions. Classify by JID suffix instead: @s.whatsapp.net → phone number @lid.whatsapp.net → LID (internal WhatsApp identifier) This ensures allowFrom works reliably with phone numbers regardless of which field the bridge populates. --- nanobot/channels/whatsapp.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/nanobot/channels/whatsapp.py b/nanobot/channels/whatsapp.py index 95bde46e9..c4c011304 100644 --- a/nanobot/channels/whatsapp.py +++ b/nanobot/channels/whatsapp.py @@ -51,6 +51,7 @@ class WhatsAppChannel(BaseChannel): self._ws = None self._connected = False self._processed_message_ids: OrderedDict[str, None] = OrderedDict() + self._lid_to_phone: dict[str, str] = {} async def login(self, force: bool = False) -> bool: """ @@ -197,9 +198,28 @@ class WhatsAppChannel(BaseChannel): if not was_mentioned: return - user_id = pn if pn else sender - sender_id = user_id.split("@")[0] if "@" in user_id else user_id - logger.info("Sender {}", sender) + # Classify by JID suffix: @s.whatsapp.net = phone, @lid.whatsapp.net = LID + # The bridge's pn/sender fields don't consistently map to phone/LID across versions. + raw_a = pn or "" + raw_b = sender or "" + id_a = raw_a.split("@")[0] if "@" in raw_a else raw_a + id_b = raw_b.split("@")[0] if "@" in raw_b else raw_b + + phone_id = "" + lid_id = "" + for raw, extracted in [(raw_a, id_a), (raw_b, id_b)]: + if "@s.whatsapp.net" in raw: + phone_id = extracted + elif "@lid.whatsapp.net" in raw: + lid_id = extracted + elif extracted and not phone_id: + phone_id = extracted # best guess for bare values + + if phone_id and lid_id: + self._lid_to_phone[lid_id] = phone_id + sender_id = phone_id or self._lid_to_phone.get(lid_id, "") or lid_id or id_a or id_b + + logger.info("Sender phone={} lid={} → sender_id={}", phone_id or "(empty)", lid_id or "(empty)", sender_id) # Handle voice transcription if it's a voice message if content == "[Voice Message]":