diff --git a/src/dispatch/plugins/dispatch_slack/enums.py b/src/dispatch/plugins/dispatch_slack/enums.py index e7585c37da68..58125b82562a 100644 --- a/src/dispatch/plugins/dispatch_slack/enums.py +++ b/src/dispatch/plugins/dispatch_slack/enums.py @@ -2,8 +2,10 @@ class SlackAPIGetEndpoints(DispatchEnum): + chat_permalink = "chat.getPermalink" conversations_history = "conversations.history" conversations_info = "conversations.info" + team_info = "team.info" users_conversations = "users.conversations" users_info = "users.info" users_lookup_by_email = "users.lookupByEmail" diff --git a/src/dispatch/plugins/dispatch_slack/incident/interactive.py b/src/dispatch/plugins/dispatch_slack/incident/interactive.py index 3251ca205527..37b2235fb232 100644 --- a/src/dispatch/plugins/dispatch_slack/incident/interactive.py +++ b/src/dispatch/plugins/dispatch_slack/incident/interactive.py @@ -931,6 +931,11 @@ def handle_member_joined_channel( participant = incident_flows.incident_add_or_reactivate_participant_flow( user_email=user.email, incident_id=context["subject"].id, db_session=db_session ) + + if not participant: + # Participant is already in the incident channel. + return + participant.user_conversation_id = context["user_id"] incident = incident_service.get(db_session=db_session, incident_id=context["subject"].id) diff --git a/src/dispatch/plugins/dispatch_slack/service.py b/src/dispatch/plugins/dispatch_slack/service.py index d7b4271ba136..aa44b5d46d02 100644 --- a/src/dispatch/plugins/dispatch_slack/service.py +++ b/src/dispatch/plugins/dispatch_slack/service.py @@ -86,6 +86,12 @@ def list_conversation_messages(client: WebClient, conversation_id: str, **kwargs ) +@functools.lru_cache() +def get_domain(client: WebClient) -> str: + """Gets the team's Slack domain.""" + return make_call(client, SlackAPIGetEndpoints.team_info)["team"]["domain"] + + @functools.lru_cache() def get_user_info_by_id(client: WebClient, user_id: str) -> dict: """Gets profile information about a user by id.""" @@ -195,7 +201,7 @@ def create_conversation(client: WebClient, name: str, is_private: bool = False) return { "id": response["id"], "name": response["name"], - "weblink": f"https://slack.com/app_redirect?channel={response['id']}", + "weblink": f"https://{get_domain(client)}.slack.com/app_redirect?channel={response['id']}", } @@ -270,6 +276,15 @@ def add_users_to_conversation( pass +def get_message_permalink(client: WebClient, conversation_id: str, ts: str) -> str: + return make_call( + client, + SlackAPIGetEndpoints.chat_permalink, + channel=conversation_id, + message_ts=ts, + ) + + def send_message( client: WebClient, conversation_id: str, @@ -294,7 +309,7 @@ def send_message( return { "id": response["channel"], "timestamp": response["ts"], - "weblink": f"https://slack.com/app_redirect?channel={response['id']}", # TODO should we fetch the permalink? + "weblink": get_message_permalink(client, response["channel"], response["ts"]), } @@ -318,7 +333,7 @@ def update_message( return { "id": response["channel"], "timestamp": response["ts"], - "weblink": f"https://slack.com/app_redirect?channel={response['id']}", # TODO should we fetch the permalink? + "weblink": get_message_permalink(client, response["channel"], response["ts"]), }