Skip to content

Commit

Permalink
Enhancement/slack bolt (#2767)
Browse files Browse the repository at this point in the history
Migrates existing slack functionality onto the slack-bolt framework.

Co-authored-by: Will Sheldon <[email protected]>
Co-authored-by: Marc Vilanova <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2023
1 parent f3ca691 commit 31dbe92
Show file tree
Hide file tree
Showing 38 changed files with 3,848 additions and 4,516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The following are the bot and user scopes required for the Dispatch Slack App to
**Bot Token Scopes**

```text
bookmarks:write
channels:read
chat:write
commands
Expand Down
29 changes: 23 additions & 6 deletions src/dispatch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,16 @@ def run_slack_websocket(organization: str, project: str):
"""Runs the slack websocket process."""
import asyncio
from sqlalchemy import true
from dispatch.project.models import ProjectRead
from dispatch.project import service as project_service
from dispatch.plugins.dispatch_slack import socket_mode
from dispatch.plugins.dispatch_slack.decorators import get_organization_scope_from_slug

from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler

from dispatch.common.utils.cli import install_plugins
from dispatch.plugins.dispatch_slack.bolt import app
from dispatch.plugins.dispatch_slack.incident.interactive import configure as incident_configure
from dispatch.plugins.dispatch_slack.service import get_organization_scope_from_slug
from dispatch.plugins.dispatch_slack.workflow import configure as workflow_configure
from dispatch.project import service as project_service
from dispatch.project.models import ProjectRead

install_plugins()

Expand All @@ -749,7 +754,7 @@ def run_slack_websocket(organization: str, project: str):
instance = None
for i in instances:
if i.plugin.slug == "slack-conversation":
instance = i
instance: PluginInstance = i
break

if not instance:
Expand All @@ -760,8 +765,20 @@ def run_slack_websocket(organization: str, project: str):
return

session.close()

click.secho("Slack websocket process started...", fg="blue")
asyncio.run(socket_mode.run_websocket_process(instance.configuration))
incident_configure(instance.configuration)
workflow_configure(instance.configuration)

app._token = instance.configuration.api_bot_token.get_secret_value()

async def main():
handler = AsyncSocketModeHandler(
app, instance.configuration.socket_mode_app_token.get_secret_value()
)
await handler.start_async()

asyncio.run(main())


@dispatch_server.command("shell")
Expand Down
64 changes: 61 additions & 3 deletions src/dispatch/incident/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,62 @@ def set_conversation_topic(incident: Incident, db_session: SessionLocal):
log.exception(e)


def set_conversation_bookmarks(incident: Incident, db_session: SessionLocal):
"""Sets the conversation bookmarks."""
if not incident.conversation:
log.warning("Conversation bookmark not set. No conversation available for this incident.")
return

plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=incident.project.id, plugin_type="conversation"
)
if not plugin:
log.warning("Bookmarks not created. No conversation plugin enabled.")
return

try:
plugin.instance.set_bookmark(
incident.conversation.channel_id,
resolve_attr(incident, "incident_document.weblink"),
title="Incident Document",
) if incident.documents else log.warning(
"Document bookmark not set. No document available for this incident."
)

plugin.instance.set_bookmark(
incident.conversation.channel_id,
resolve_attr(incident, "conference.weblink"),
title="Video Conference",
) if incident.conference else log.warning(
"Conference bookmark not set. No conference available for this incident."
)

plugin.instance.set_bookmark(
incident.conversation.channel_id,
resolve_attr(incident, "storage.weblink"),
title="Storage",
) if incident.storage else log.warning(
"Storage bookmark not set. No storage available for this incident."
)

plugin.instance.set_bookmark(
incident.conversation.channel_id,
resolve_attr(incident, "ticket.weblink"),
title="Ticket",
) if incident.ticket else log.warning(
"Ticket bookmark not set. No ticket available for this incident."
)

except Exception as e:
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Setting the incident conversation bookmarks failed. Reason: {e}",
incident_id=incident.id,
)
log.exception(e)


def add_participants_to_conversation(
participant_emails: List[str], incident: Incident, db_session: SessionLocal
):
Expand Down Expand Up @@ -848,9 +904,6 @@ def incident_create_flow(*, organization_slug: str, incident_id: int, db_session
description="Conversation added to incident",
incident_id=incident.id,
)

# we set the conversation topic
set_conversation_topic(incident, db_session)
except Exception as e:
event_service.log_incident_event(
db_session=db_session,
Expand Down Expand Up @@ -896,6 +949,11 @@ def incident_create_flow(*, organization_slug: str, incident_id: int, db_session
)
log.exception(e)

# we set the conversation topic
set_conversation_topic(incident, db_session)
# we set the conversation bookmarks
set_conversation_bookmarks(incident, db_session)

# we defer this setup for all resolved incident roles until after resources have been created
roles = ["reporter", "commander", "liaison", "scribe"]

Expand Down
Loading

0 comments on commit 31dbe92

Please sign in to comment.