Send message if the original is missing

This commit is contained in:
2024-09-06 18:10:38 +02:00
parent d04e617607
commit a85a1ea3d1
2 changed files with 20 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ from reflector.processors.types import Transcript as ProcessorTranscript
from reflector.processors.types import Word from reflector.processors.types import Word
from reflector.settings import settings from reflector.settings import settings
from reflector.zulip import ( from reflector.zulip import (
InvalidMessageError,
get_zulip_message, get_zulip_message,
send_message_to_zulip, send_message_to_zulip,
update_zulip_message, update_zulip_message,
@@ -346,9 +347,16 @@ async def transcript_post_to_zulip(
raise HTTPException(status_code=404, detail="Transcript not found") raise HTTPException(status_code=404, detail="Transcript not found")
content = get_zulip_message(transcript, include_topics) content = get_zulip_message(transcript, include_topics)
message_updated = False
if transcript.zulip_message_id: if transcript.zulip_message_id:
update_zulip_message(transcript.zulip_message_id, stream, topic, content) try:
else: update_zulip_message(transcript.zulip_message_id, stream, topic, content)
message_updated = True
except InvalidMessageError:
pass
if not message_updated:
response = send_message_to_zulip(stream, topic, content) response = send_message_to_zulip(stream, topic, content)
await transcripts_controller.update( await transcripts_controller.update(
transcript, {"zulip_message_id": response["id"]} transcript, {"zulip_message_id": response["id"]}

View File

@@ -6,6 +6,10 @@ from reflector.db.transcripts import Transcript
from reflector.settings import settings from reflector.settings import settings
class InvalidMessageError(Exception):
pass
def send_message_to_zulip(stream: str, topic: str, content: str): def send_message_to_zulip(stream: str, topic: str, content: str):
try: try:
response = requests.post( response = requests.post(
@@ -32,8 +36,6 @@ def update_zulip_message(message_id: int, stream: str, topic: str, content: str)
response = requests.patch( response = requests.patch(
f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}", f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}",
data={ data={
"type": "stream",
"to": stream,
"topic": topic, "topic": topic,
"content": content, "content": content,
}, },
@@ -41,6 +43,12 @@ def update_zulip_message(message_id: int, stream: str, topic: str, content: str)
headers={"Content-Type": "application/x-www-form-urlencoded"}, headers={"Content-Type": "application/x-www-form-urlencoded"},
) )
if (
response.status_code == 400
and response.json()["msg"] == "Invalid message(s)"
):
raise InvalidMessageError(f"There is no message with id: {message_id}")
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()