mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
Update zulip message
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""Add zulip message id
|
||||
|
||||
Revision ID: 764ce6db4388
|
||||
Revises: 62dea3db63a5
|
||||
Create Date: 2024-09-06 14:02:06.649665
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '764ce6db4388'
|
||||
down_revision: Union[str, None] = '62dea3db63a5'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('transcript', sa.Column('zulip_message_id', sa.Integer(), nullable=True))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('transcript', 'zulip_message_id')
|
||||
# ### end Alembic commands ###
|
||||
@@ -54,6 +54,7 @@ transcripts = sqlalchemy.Table(
|
||||
"meeting_id",
|
||||
sqlalchemy.String,
|
||||
),
|
||||
sqlalchemy.Column("zulip_message_id", sqlalchemy.Integer, nullable=True),
|
||||
)
|
||||
|
||||
|
||||
@@ -150,6 +151,7 @@ class Transcript(BaseModel):
|
||||
audio_location: str = "local"
|
||||
reviewed: bool = False
|
||||
meeting_id: str | None = None
|
||||
zulip_message_id: int | None = None
|
||||
|
||||
def add_event(self, event: str, data: BaseModel) -> TranscriptEvent:
|
||||
ev = TranscriptEvent(event=event, data=data.model_dump())
|
||||
|
||||
@@ -587,7 +587,10 @@ async def pipeline_post_to_zulip(transcript: Transcript, logger: Logger):
|
||||
|
||||
if room.zulip_auto_post:
|
||||
message = get_zulip_message(transcript=transcript)
|
||||
send_message_to_zulip(room.zulip_stream, room.zulip_topic, message)
|
||||
response = send_message_to_zulip(room.zulip_stream, room.zulip_topic, message)
|
||||
await transcripts_controller.update(
|
||||
transcript, {"zulip_message_id": response["id"]}
|
||||
)
|
||||
|
||||
logger.info("Posted to zulip")
|
||||
|
||||
|
||||
@@ -16,6 +16,11 @@ from reflector.db.transcripts import (
|
||||
from reflector.processors.types import Transcript as ProcessorTranscript
|
||||
from reflector.processors.types import Word
|
||||
from reflector.settings import settings
|
||||
from reflector.zulip import (
|
||||
get_zulip_message,
|
||||
send_message_to_zulip,
|
||||
update_zulip_message,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -323,3 +328,28 @@ async def transcript_get_topics_with_words_per_speaker(
|
||||
|
||||
# convert to GetTranscriptTopicWithWordsPerSpeaker
|
||||
return GetTranscriptTopicWithWordsPerSpeaker.from_transcript_topic(topic)
|
||||
|
||||
|
||||
@router.post("/transcripts/{transcript_id}/zulip")
|
||||
async def transcript_post_to_zulip(
|
||||
transcript_id: str,
|
||||
stream: str,
|
||||
topic: str,
|
||||
include_topics: bool,
|
||||
user: Annotated[Optional[auth.UserInfo], Depends(auth.current_user_optional)],
|
||||
):
|
||||
user_id = user["sub"] if user else None
|
||||
transcript = await transcripts_controller.get_by_id_for_http(
|
||||
transcript_id, user_id=user_id
|
||||
)
|
||||
if not transcript:
|
||||
raise HTTPException(status_code=404, detail="Transcript not found")
|
||||
|
||||
content = get_zulip_message(transcript, include_topics)
|
||||
if transcript.zulip_message_id:
|
||||
update_zulip_message(transcript.zulip_message_id, stream, topic, content)
|
||||
else:
|
||||
response = send_message_to_zulip(stream, topic, content)
|
||||
await transcripts_controller.update(
|
||||
transcript, {"zulip_message_id": response["id"]}
|
||||
)
|
||||
|
||||
@@ -6,10 +6,7 @@ from reflector.db.transcripts import Transcript
|
||||
from reflector.settings import settings
|
||||
|
||||
|
||||
def send_message_to_zulip(stream: str, topic: str, message: str):
|
||||
if not stream or not topic or not message:
|
||||
raise ValueError("Missing required parameters")
|
||||
|
||||
def send_message_to_zulip(stream: str, topic: str, content: str):
|
||||
try:
|
||||
response = requests.post(
|
||||
f"https://{settings.ZULIP_REALM}/api/v1/messages",
|
||||
@@ -17,7 +14,7 @@ def send_message_to_zulip(stream: str, topic: str, message: str):
|
||||
"type": "stream",
|
||||
"to": stream,
|
||||
"topic": topic,
|
||||
"content": message,
|
||||
"content": content,
|
||||
},
|
||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||
@@ -30,7 +27,29 @@ def send_message_to_zulip(stream: str, topic: str, message: str):
|
||||
raise Exception(f"Failed to send message to Zulip: {error}")
|
||||
|
||||
|
||||
def get_zulip_message(transcript: Transcript):
|
||||
def update_zulip_message(message_id: int, stream: str, topic: str, content: str):
|
||||
try:
|
||||
response = requests.patch(
|
||||
f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}",
|
||||
data={
|
||||
"type": "stream",
|
||||
"to": stream,
|
||||
"topic": topic,
|
||||
"content": content,
|
||||
},
|
||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()
|
||||
except requests.RequestException as error:
|
||||
print(content)
|
||||
raise Exception(f"Failed to update Zulip message: {error}")
|
||||
|
||||
|
||||
def get_zulip_message(transcript: Transcript, include_topics: bool):
|
||||
transcript_url = f"{settings.UI_BASE_URL}/transcripts/{transcript.id}"
|
||||
|
||||
header_text = f"# Reflector – {transcript.title or 'Unnamed recording'}\n\n"
|
||||
@@ -40,7 +59,7 @@ def get_zulip_message(transcript: Transcript):
|
||||
|
||||
topic_text = ""
|
||||
|
||||
if transcript.topics:
|
||||
if include_topics and transcript.topics:
|
||||
topic_text = "```spoiler Topics\n"
|
||||
for topic in transcript.topics:
|
||||
topic_text += f"1. [{format_time(topic.timestamp)}] {topic.title}\n"
|
||||
@@ -48,7 +67,7 @@ def get_zulip_message(transcript: Transcript):
|
||||
|
||||
summary = "```spoiler Summary\n"
|
||||
summary += transcript.long_summary
|
||||
summary += "```\n\n"
|
||||
summary += "\n```\n\n"
|
||||
|
||||
message = header_text + summary + topic_text + "-----\n"
|
||||
return message
|
||||
|
||||
Reference in New Issue
Block a user