Repository: phidatahq/phidata
File: libs/agno/agno/os/interfaces/slack/router.py (Python)

Code before:
                log_info("bot event")
                pass
            else:
                background_tasks.add_task(_process_slack_event, event)

        return SlackEventResponse(status="ok")

    async def _process_slack_event(event: dict):
        # Handle both app_mention and message events
        if event.get("type") in ("app_mention", "message"):
            user = None
            message_text = event.get("text", "")
            channel_id = event.get("channel", "")
            user = event.get("user")
            channel_type = event.get("channel_type", "")
            if event.get("thread_ts"):
                ts = event.get("thread_ts", "")
            else:
                ts = event.get("ts", "")

            # Use the timestamp as the session id, so that each thread is a separate session
            session_id = ts

            # prevents duplicate responses when both app_mention and message.channels are subscribed
            if mention_only and event.get("type") == "message" and channel_type in ("channel", "group"):
                return

            if agent:
                response = await agent.arun(message_text, user_id=user if user else None, session_id=session_id)
            elif team:
                response = await team.arun(message_text, user_id=user if user else None, session_id=session_id)  # type: ignore
            elif workflow:
                response = await workflow.arun(message_text, user_id=user if user else None, session_id=session_id)  # type: ignore

            if response:
                if hasattr(response, "reasoning_content") and response.reasoning_content:
                    _send_slack_message(
                        channel=channel_id,
                        message=f"Reasoning: \n{response.reasoning_content}",
                        thread_ts=ts,
                        italics=True,
                    )

                _send_slack_message(channel=channel_id, message=response.content or "", thread_ts=ts)

    def _send_slack_message(channel: str, thread_ts: str, message: str, italics: bool = False):
        if len(message) <= 40000:
            if italics:
                # Handle multi-line messages by making each line italic
                formatted_message = "\n".join([f"_{line}_" for line in message.split("\n")])
                SlackTools().send_message_thread(channel=channel, text=formatted_message or "", thread_ts=thread_ts)

Review comment (refactor):
1. Let's move this block to the top, should work as an early exit. 
2. Last check should probably be `channel_type != "dm"`, as there may be more types. 

Code after:
                log_info("bot event")
                pass
            else:
                background_tasks.add_task(_process_slack_event, event)

        return SlackEventResponse(status="ok")

    async def _process_slack_event(event: dict):
        event_type = event.get("type")

        # Only handle app_mention and message events
        if event_type not in ("app_mention", "message"):
            return

        channel_type = event.get("channel_type", "")

        # Handle duplicate replies
        if not reply_to_mentions_only and event_type == "app_mention":
            return

        # If reply_to_mentions_only is True, ignore every message that is not a DM
        if reply_to_mentions_only and event_type == "message" and channel_type != "im":
            return

        # Extract event data
        user = None
        message_text = event.get("text", "")
        channel_id = event.get("channel", "")
        user = event.get("user")
        if event.get("thread_ts"):
            ts = event.get("thread_ts", "")
        else:
            ts = event.get("ts", "")

        # Use the timestamp as the session id, so that each thread is a separate session
        session_id = ts

        if agent:
            response = await agent.arun(message_text, user_id=user, session_id=session_id)
        elif team:
            response = await team.arun(message_text, user_id=user, session_id=session_id)  # type: ignore
        elif workflow:
            response = await workflow.arun(message_text, user_id=user, session_id=session_id)  # type: ignore

        if response:
            if hasattr(response, "reasoning_content") and response.reasoning_content:
                _send_slack_message(
                    channel=channel_id,
                    message=f"Reasoning: \n{response.reasoning_content}",
                    thread_ts=ts,
                    italics=True,