From 295e074fab881ea2e7bd55d7f726de8ca4fc53d0 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 6 Dec 2022 16:33:28 +0100 Subject: [PATCH] feat(chalice): share note to MSTeams --- api/chalicelib/core/collaboration_msteams.py | 8 +-- api/chalicelib/core/sessions_notes.py | 58 ++++++++++++++++++++ api/routers/core_dynamic.py | 7 +++ ee/api/chalicelib/core/sessions_notes.py | 58 ++++++++++++++++++++ ee/api/routers/core_dynamic.py | 7 +++ 5 files changed, 134 insertions(+), 4 deletions(-) diff --git a/api/chalicelib/core/collaboration_msteams.py b/api/chalicelib/core/collaboration_msteams.py index 17a69da03..4dc3d276a 100644 --- a/api/chalicelib/core/collaboration_msteams.py +++ b/api/chalicelib/core/collaboration_msteams.py @@ -40,21 +40,21 @@ class MSTeams(BaseCollaboration): def send_raw(cls, tenant_id, webhook_id, body): integration = cls.__get(tenant_id=tenant_id, integration_id=webhook_id) if integration is None: - return {"errors": ["slack integration not found"]} + return {"errors": ["msteams integration not found"]} try: r = requests.post( url=integration["endpoint"], json=body, timeout=5) if r.status_code != 200: - print(f"!! issue sending slack raw; webhookId:{webhook_id} code:{r.status_code}") + print(f"!! issue sending msteams raw; webhookId:{webhook_id} code:{r.status_code}") print(r.text) return None except requests.exceptions.Timeout: - print(f"!! Timeout sending slack raw webhookId:{webhook_id}") + print(f"!! Timeout sending msteams raw webhookId:{webhook_id}") return None except Exception as e: - print(f"!! Issue sending slack raw webhookId:{webhook_id}") + print(f"!! Issue sending msteams raw webhookId:{webhook_id}") print(str(e)) return None return {"data": r.text} diff --git a/api/chalicelib/core/sessions_notes.py b/api/chalicelib/core/sessions_notes.py index 3ef4ccc21..cbb33f11b 100644 --- a/api/chalicelib/core/sessions_notes.py +++ b/api/chalicelib/core/sessions_notes.py @@ -5,6 +5,7 @@ from decouple import config import schemas from chalicelib.core import sessions from chalicelib.core.collaboration_slack import Slack +from chalicelib.core.collaboration_msteams import MSTeams from chalicelib.utils import pg_client, helper from chalicelib.utils.TimeUTC import TimeUTC @@ -166,3 +167,60 @@ def share_to_slack(tenant_id, user_id, project_id, note_id, webhook_id): webhook_id=webhook_id, body={"blocks": blocks} ) + + +def share_to_msteams(tenant_id, user_id, project_id, note_id, webhook_id): + note = get_note(tenant_id=tenant_id, project_id=project_id, user_id=user_id, note_id=note_id, share=user_id) + if note is None: + return {"errors": ["Note not found"]} + session_url = urljoin(config('SITE_URL'), f"{note['projectId']}/session/{note['sessionId']}?note={note['noteId']}") + if note["timestamp"] > 0: + session_url += f"&jumpto={note['timestamp']}" + title = f"[session_url](Note for session {note['sessionId']})" + + blocks = [{ + "type": "TextBlock", + "text": title, + "style": "heading", + "size": "Large" + }, + { + "type": "TextBlock", + "spacing": "Small", + "text": note["message"] + } + ] + if note["tag"]: + blocks.append({"type": "TextBlock", + "spacing": "Small", + "text": f"Tag: *{note['tag']}*", + "size": "Small"}) + bottom = f"Created by {note['creatorName'].capitalize()}" + if user_id != note["userId"]: + bottom += f"\nSent by {note['shareName']}: " + blocks.append({"type": "TextBlock", + "spacing": "Default", + "text": bottom, + "size": "Small", + "fontType": "Monospace"}) + return MSTeams.send_raw( + tenant_id=tenant_id, + webhook_id=webhook_id, + body={"type": "message", + "attachments": [ + {"contentType": "application/vnd.microsoft.card.adaptive", + "contentUrl": None, + "content": { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.5", + "body": [{ + "type": "ColumnSet", + "style": "emphasis", + "separator": True, + "bleed": True, + "columns": [{"width": "stretch", + "items": blocks, + "type": "Column"}] + }]}} + ]}) diff --git a/api/routers/core_dynamic.py b/api/routers/core_dynamic.py index 4bd7550e4..fa351214c 100644 --- a/api/routers/core_dynamic.py +++ b/api/routers/core_dynamic.py @@ -394,6 +394,13 @@ def share_note_to_slack(projectId: int, noteId: int, webhookId: int, note_id=noteId, webhook_id=webhookId) +@app.get('/{projectId}/notes/{noteId}/msteams/{webhookId}', tags=["sessions", "notes"]) +def share_note_to_msteams(projectId: int, noteId: int, webhookId: int, + context: schemas.CurrentContext = Depends(OR_context)): + return sessions_notes.share_to_msteams(tenant_id=context.tenant_id, project_id=projectId, user_id=context.user_id, + note_id=noteId, webhook_id=webhookId) + + @app.post('/{projectId}/notes', tags=["sessions", "notes"]) def get_all_notes(projectId: int, data: schemas.SearchNoteSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): diff --git a/ee/api/chalicelib/core/sessions_notes.py b/ee/api/chalicelib/core/sessions_notes.py index de1f83854..6acb2188b 100644 --- a/ee/api/chalicelib/core/sessions_notes.py +++ b/ee/api/chalicelib/core/sessions_notes.py @@ -5,6 +5,7 @@ from decouple import config import schemas from chalicelib.core import sessions from chalicelib.core.collaboration_slack import Slack +from chalicelib.core.collaboration_msteams import MSTeams from chalicelib.utils import pg_client, helper from chalicelib.utils.TimeUTC import TimeUTC @@ -170,3 +171,60 @@ def share_to_slack(tenant_id, user_id, project_id, note_id, webhook_id): webhook_id=webhook_id, body={"blocks": blocks} ) + + +def share_to_msteams(tenant_id, user_id, project_id, note_id, webhook_id): + note = get_note(tenant_id=tenant_id, project_id=project_id, user_id=user_id, note_id=note_id, share=user_id) + if note is None: + return {"errors": ["Note not found"]} + session_url = urljoin(config('SITE_URL'), f"{note['projectId']}/session/{note['sessionId']}?note={note['noteId']}") + if note["timestamp"] > 0: + session_url += f"&jumpto={note['timestamp']}" + title = f"[session_url](Note for session {note['sessionId']})" + + blocks = [{ + "type": "TextBlock", + "text": title, + "style": "heading", + "size": "Large" + }, + { + "type": "TextBlock", + "spacing": "Small", + "text": note["message"] + } + ] + if note["tag"]: + blocks.append({"type": "TextBlock", + "spacing": "Small", + "text": f"Tag: *{note['tag']}*", + "size": "Small"}) + bottom = f"Created by {note['creatorName'].capitalize()}" + if user_id != note["userId"]: + bottom += f"\nSent by {note['shareName']}: " + blocks.append({"type": "TextBlock", + "spacing": "Default", + "text": bottom, + "size": "Small", + "fontType": "Monospace"}) + return MSTeams.send_raw( + tenant_id=tenant_id, + webhook_id=webhook_id, + body={"type": "message", + "attachments": [ + {"contentType": "application/vnd.microsoft.card.adaptive", + "contentUrl": None, + "content": { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.5", + "body": [{ + "type": "ColumnSet", + "style": "emphasis", + "separator": True, + "bleed": True, + "columns": [{"width": "stretch", + "items": blocks, + "type": "Column"}] + }]}} + ]}) diff --git a/ee/api/routers/core_dynamic.py b/ee/api/routers/core_dynamic.py index 666ea4199..8f84c7b13 100644 --- a/ee/api/routers/core_dynamic.py +++ b/ee/api/routers/core_dynamic.py @@ -414,6 +414,13 @@ def share_note_to_slack(projectId: int, noteId: int, webhookId: int, note_id=noteId, webhook_id=webhookId) +@app.get('/{projectId}/notes/{noteId}/msteams/{webhookId}', tags=["sessions", "notes"]) +def share_note_to_msteams(projectId: int, noteId: int, webhookId: int, + context: schemas.CurrentContext = Depends(OR_context)): + return sessions_notes.share_to_msteams(tenant_id=context.tenant_id, project_id=projectId, user_id=context.user_id, + note_id=noteId, webhook_id=webhookId) + + @app.post('/{projectId}/notes', tags=["sessions", "notes"], dependencies=[OR_scope(Permissions.session_replay)]) def get_all_notes(projectId: int, data: schemas.SearchNoteSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)):