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

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

        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 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)
            else:
                SlackTools().send_message_thread(channel=channel, text=message or "", thread_ts=thread_ts)
            return

        # Split message into batches of 4000 characters (WhatsApp message limit is 4096)
        message_batches = [message[i : i + 40000] for i in range(0, len(message), 40000)]


Review comment (suggestion):
```suggestion
        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
```

Simplification

Code after:
        # 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,
                )

            _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)
            else:
                SlackTools().send_message_thread(channel=channel, text=message or "", thread_ts=thread_ts)
            return

        # Split message into batches of 4000 characters (WhatsApp message limit is 4096)
        message_batches = [message[i : i + 40000] for i in range(0, len(message), 40000)]