Improve insights and continue WhatsApp implementation
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import quote_plus, urlparse
|
||||
|
||||
import aiohttp
|
||||
from asgiref.sync import sync_to_async
|
||||
@@ -10,7 +10,7 @@ from django.urls import reverse
|
||||
from signalbot import Command, Context, SignalBot
|
||||
|
||||
from core.clients import ClientBase, signalapi
|
||||
from core.messaging import ai, history, natural, replies, utils
|
||||
from core.messaging import ai, history, media_bridge, natural, replies, utils
|
||||
from core.models import Chat, Manipulation, PersonIdentifier, QueuedMessage
|
||||
from core.util import logs
|
||||
|
||||
@@ -339,6 +339,7 @@ class HandleMessage(Command):
|
||||
# Handle attachments across multiple Signal payload variants.
|
||||
attachment_list = _extract_attachments(raw)
|
||||
xmpp_attachments = []
|
||||
compose_media_urls = []
|
||||
|
||||
# Asynchronously fetch all attachments
|
||||
log.info(f"ATTACHMENT LIST {attachment_list}")
|
||||
@@ -366,10 +367,25 @@ class HandleMessage(Command):
|
||||
"size": fetched["size"],
|
||||
}
|
||||
)
|
||||
blob_key = media_bridge.put_blob(
|
||||
service="signal",
|
||||
content=fetched["content"],
|
||||
filename=fetched["filename"],
|
||||
content_type=fetched["content_type"],
|
||||
)
|
||||
if blob_key:
|
||||
compose_media_urls.append(
|
||||
f"/compose/media/blob/?key={quote_plus(str(blob_key))}"
|
||||
)
|
||||
|
||||
if (not text) and compose_media_urls:
|
||||
text = "\n".join(compose_media_urls)
|
||||
|
||||
# Forward incoming Signal messages to XMPP and apply mutate rules.
|
||||
identifier_text_overrides = {}
|
||||
for identifier in identifiers:
|
||||
user = identifier.user
|
||||
session_key = (identifier.user.id, identifier.person.id)
|
||||
|
||||
mutate_manips = await sync_to_async(list)(
|
||||
Manipulation.objects.filter(
|
||||
@@ -381,6 +397,7 @@ class HandleMessage(Command):
|
||||
)
|
||||
)
|
||||
if mutate_manips:
|
||||
uploaded_urls = []
|
||||
for manip in mutate_manips:
|
||||
prompt = replies.generate_mutate_reply_prompt(
|
||||
text,
|
||||
@@ -393,24 +410,36 @@ class HandleMessage(Command):
|
||||
log.info(
|
||||
f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP."
|
||||
)
|
||||
await self.ur.xmpp.client.send_from_external(
|
||||
uploaded_urls = await self.ur.xmpp.client.send_from_external(
|
||||
user,
|
||||
identifier,
|
||||
result,
|
||||
is_outgoing_message,
|
||||
attachments=xmpp_attachments,
|
||||
)
|
||||
resolved_text = text
|
||||
if (not resolved_text) and uploaded_urls:
|
||||
resolved_text = "\n".join(uploaded_urls)
|
||||
elif (not resolved_text) and compose_media_urls:
|
||||
resolved_text = "\n".join(compose_media_urls)
|
||||
identifier_text_overrides[session_key] = resolved_text
|
||||
else:
|
||||
log.info(
|
||||
f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP."
|
||||
)
|
||||
await self.ur.xmpp.client.send_from_external(
|
||||
uploaded_urls = await self.ur.xmpp.client.send_from_external(
|
||||
user,
|
||||
identifier,
|
||||
text,
|
||||
is_outgoing_message,
|
||||
attachments=xmpp_attachments,
|
||||
)
|
||||
resolved_text = text
|
||||
if (not resolved_text) and uploaded_urls:
|
||||
resolved_text = "\n".join(uploaded_urls)
|
||||
elif (not resolved_text) and compose_media_urls:
|
||||
resolved_text = "\n".join(compose_media_urls)
|
||||
identifier_text_overrides[session_key] = resolved_text
|
||||
|
||||
# Persist message history for every resolved identifier, even when no
|
||||
# manipulations are active, so manual chat windows stay complete.
|
||||
@@ -425,11 +454,12 @@ class HandleMessage(Command):
|
||||
session_cache[session_key] = chat_session
|
||||
sender_key = source_uuid or source_number or identifier_candidates[0]
|
||||
message_key = (chat_session.id, ts, sender_key)
|
||||
message_text = identifier_text_overrides.get(session_key, text)
|
||||
if message_key not in stored_messages:
|
||||
await history.store_message(
|
||||
session=chat_session,
|
||||
sender=sender_key,
|
||||
text=text,
|
||||
text=message_text,
|
||||
ts=ts,
|
||||
outgoing=is_from_bot,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user