mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
Send message if the original is missing
This commit is contained in:
@@ -17,6 +17,7 @@ from reflector.processors.types import Transcript as ProcessorTranscript
|
||||
from reflector.processors.types import Word
|
||||
from reflector.settings import settings
|
||||
from reflector.zulip import (
|
||||
InvalidMessageError,
|
||||
get_zulip_message,
|
||||
send_message_to_zulip,
|
||||
update_zulip_message,
|
||||
@@ -346,9 +347,16 @@ async def transcript_post_to_zulip(
|
||||
raise HTTPException(status_code=404, detail="Transcript not found")
|
||||
|
||||
content = get_zulip_message(transcript, include_topics)
|
||||
|
||||
message_updated = False
|
||||
if transcript.zulip_message_id:
|
||||
try:
|
||||
update_zulip_message(transcript.zulip_message_id, stream, topic, content)
|
||||
else:
|
||||
message_updated = True
|
||||
except InvalidMessageError:
|
||||
pass
|
||||
|
||||
if not message_updated:
|
||||
response = send_message_to_zulip(stream, topic, content)
|
||||
await transcripts_controller.update(
|
||||
transcript, {"zulip_message_id": response["id"]}
|
||||
|
||||
@@ -6,6 +6,10 @@ from reflector.db.transcripts import Transcript
|
||||
from reflector.settings import settings
|
||||
|
||||
|
||||
class InvalidMessageError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def send_message_to_zulip(stream: str, topic: str, content: str):
|
||||
try:
|
||||
response = requests.post(
|
||||
@@ -32,8 +36,6 @@ def update_zulip_message(message_id: int, stream: str, topic: str, content: str)
|
||||
response = requests.patch(
|
||||
f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}",
|
||||
data={
|
||||
"type": "stream",
|
||||
"to": stream,
|
||||
"topic": topic,
|
||||
"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"},
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
return response.json()
|
||||
|
||||
Reference in New Issue
Block a user