示例#1
0
async def handle_event(
        event: EventEnvelope,
        request: Request,
        response: Response,
        background_tasks: BackgroundTasks,
        x_slack_request_timestamp: int = Header(None),
        x_slack_signature: str = Header(None),
        db_session: Session = Depends(get_db),
):
    """Handle all incomming Slack events."""
    raw_request_body = bytes.decode(await request.body())

    # We verify the timestamp
    verify_timestamp(x_slack_request_timestamp)

    # We verify the signature
    verify_signature(raw_request_body, x_slack_request_timestamp,
                     x_slack_signature)

    # Echo the URL verification challenge code back to Slack
    if event.challenge:
        return {"challenge": event.challenge}

    event_body = event.event

    if (event_body.type == "message"
            and event_body.subtype):  # We ignore messages that have a subtype
        # Parse the Event payload and emit the event to the event listener
        response.headers["X-Slack-Powered-By"] = create_ua_string()
        return {"ok"}

    user_id = event_body.user

    # Fetch conversation by channel id
    channel_id = (  # We ensure channel_id always has a value
        event_body.channel_id if event_body.channel_id else event_body.channel)
    conversation = get_by_channel_id(db_session=db_session,
                                     channel_id=channel_id)

    if conversation and dispatch_slack_service.is_user(user_id):
        # We create an async Slack client
        slack_async_client = dispatch_slack_service.create_slack_client(
            run_async=True)

        # We resolve the user's email
        user_email = await dispatch_slack_service.get_user_email_async(
            slack_async_client, user_id)

        # Dispatch event functions to be executed in the background
        for f in event_functions(event):
            background_tasks.add_task(f, user_email, conversation.incident_id)

    # We add the user-agent string to the response headers
    response.headers["X-Slack-Powered-By"] = create_ua_string()
    return {"ok"}
示例#2
0
文件: views.py 项目: omarkhd/dispatch
def get_incident_id_by_action_type(action: dict, db_session: SessionLocal):
    """Returns the incident id based on the action type."""
    incident_id = -1
    if action["type"] == "dialog_submission":
        channel_id = action["channel"]["id"]
        conversation = get_by_channel_id(db_session=db_session, channel_id=channel_id)
        incident_id = conversation.incident_id

    if action["type"] == "block_actions":
        incident_id = action["actions"][0]["value"]

    return incident_id
示例#3
0
async def handle_command(
        request: Request,
        response: Response,
        background_tasks: BackgroundTasks,
        x_slack_request_timestamp: int = Header(None),
        x_slack_signature: str = Header(None),
        db_session: Session = Depends(get_db),
):
    """Handle all incomming Slack commands."""
    raw_request_body = bytes.decode(await request.body())
    request_body_form = await request.form()
    command = request_body_form._dict

    # We verify the timestamp
    verify_timestamp(x_slack_request_timestamp)

    # We verify the signature
    verify_signature(raw_request_body, x_slack_request_timestamp,
                     x_slack_signature)

    # We add the user-agent string to the response headers
    response.headers["X-Slack-Powered-By"] = create_ua_string()

    # If the incoming slash command is equal to reporting new incident slug
    if command.get("command") == SLACK_COMMAND_REPORT_INCIDENT_SLUG:
        background_tasks.add_task(func=create_report_incident_modal,
                                  db_session=db_session,
                                  command=command)

        return INCIDENT_CONVERSATION_COMMAND_MESSAGE.get(
            command.get("command"),
            f"Unable to find message. Command: {command.get('command')}")
    else:
        # Fetch conversation by channel id
        channel_id = command.get("channel_id")
        conversation = get_by_channel_id(db_session=db_session,
                                         channel_id=channel_id)

        # Dispatch command functions to be executed in the background
        if conversation:
            for f in command_functions(command.get("command")):
                background_tasks.add_task(f,
                                          conversation.incident_id,
                                          command=command)

            return INCIDENT_CONVERSATION_COMMAND_MESSAGE.get(
                command.get("command"),
                f"Unable to find message. Command: {command.get('command')}")
        else:
            return render_non_incident_conversation_command_error_message(
                command.get("command"))
示例#4
0
async def handle_event(
        event: EventEnvelope,
        request: Request,
        response: Response,
        background_tasks: BackgroundTasks,
        x_slack_request_timestamp: int = Header(None),
        x_slack_signature: str = Header(None),
        db_session: Session = Depends(get_db),
):
    """Handle all incomming Slack events."""
    raw_request_body = bytes.decode(await request.body())

    # We verify the timestamp
    verify_timestamp(x_slack_request_timestamp)

    # We verify the signature
    verify_signature(raw_request_body, x_slack_request_timestamp,
                     x_slack_signature)

    # Echo the URL verification challenge code back to Slack
    if event.challenge:
        return {"challenge": event.challenge}

    event_body = event.event

    user_id = event_body.user
    channel_id = get_channel_id_from_event(event_body)

    if user_id and channel_id:
        conversation = get_by_channel_id(db_session=db_session,
                                         channel_id=channel_id)

        if conversation and dispatch_slack_service.is_user(user_id):
            # We create an async Slack client
            slack_async_client = dispatch_slack_service.create_slack_client(
                run_async=True)

            # We resolve the user's email
            user_email = await dispatch_slack_service.get_user_email_async(
                slack_async_client, user_id)

            # Dispatch event functions to be executed in the background
            for f in event_functions(event):
                background_tasks.add_task(f,
                                          user_email,
                                          conversation.incident_id,
                                          event=event)

    # We add the user-agent string to the response headers
    response.headers["X-Slack-Powered-By"] = create_ua_string()
    return {"ok"}
示例#5
0
def handle_dialog_action(action: dict, background_tasks: BackgroundTasks,
                         db_session: SessionLocal):
    """Handles all dialog actions."""
    channel_id = action["channel"]["id"]
    conversation = get_by_channel_id(db_session=db_session,
                                     channel_id=channel_id)
    incident_id = conversation.incident_id

    user_id = action["user"]["id"]
    user_email = action["user"]["email"]

    action_id = action["callback_id"]

    for f in dialog_action_functions(action_id):
        background_tasks.add_task(f, user_id, user_email, incident_id, action)
def test_get_by_channel_id(session, conversation):
    from dispatch.conversation.service import get_by_channel_id

    t_conversation = get_by_channel_id(db_session=session,
                                       channel_id=conversation.channel_id)
    assert t_conversation.channel_id == conversation.channel_id