fix WhatsApp group chat support and self-message handling

Bridge: allow fromMe messages in groups, include participant field in
message queue, bind to 0.0.0.0 for network access, QR served as HTML.

Adapter: process registered group messages (route to Claude), extract
participant for user identification, fix unbound 'phone' variable.

Tested end-to-end: WhatsApp group chat with Claude working. 442 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MoltBot Service
2026-02-13 22:31:22 +00:00
parent 80502b7931
commit 624eb095f1
5 changed files with 51 additions and 34 deletions

View File

@@ -130,23 +130,21 @@ async def handle_incoming(msg: dict, client: httpx.AsyncClient) -> None:
# Group chat: only registered chats
if is_group:
chat_id = sender # group JID
if not is_registered_chat(chat_id):
group_jid = sender # group JID like 123456@g.us
if not is_registered_chat(group_jid):
return
# Group messages — skip for now (can be enhanced later)
return
# Private chat: check admin
phone = sender.split("@")[0]
if not is_admin(phone):
_security_log.warning(
"Unauthorized WhatsApp DM from %s (%s): %.100s",
phone, push_name, text,
)
return
# Use phone number as channel ID
channel_id = f"wa-{phone}"
# Use group JID as channel ID
channel_id = f"wa-{group_jid.split('@')[0]}"
else:
# Private chat: check admin
phone = sender.split("@")[0]
if not is_admin(phone):
_security_log.warning(
"Unauthorized WhatsApp DM from %s (%s): %.100s",
phone, push_name, text,
)
return
channel_id = f"wa-{phone}"
# Handle slash commands locally for immediate response
if text.startswith("/"):
@@ -175,15 +173,19 @@ async def handle_incoming(msg: dict, client: httpx.AsyncClient) -> None:
await send_whatsapp(client, sender, reply)
return
# Identify sender for logging/routing
participant = msg.get("participant") or sender
user_id = participant.split("@")[0]
# Route to Claude via router (handles /model and regular messages)
log.info("Message from %s (%s): %.50s", phone, push_name, text)
log.info("Message from %s (%s): %.50s", user_id, push_name, text)
try:
response, _is_cmd = await asyncio.to_thread(
route_message, channel_id, phone, text
route_message, channel_id, user_id, text
)
await send_whatsapp(client, sender, response)
except Exception as e:
log.error("Error handling message from %s: %s", phone, e)
log.error("Error handling message from %s: %s", user_id, e)
await send_whatsapp(client, sender, "Sorry, an error occurred.")