Skip to content

SlackSendTool

autogen.tools.experimental.SlackSendTool #

SlackSendTool(*, bot_token, channel_id)

Bases: Tool

Sends a message to a Slack channel.

Initialize the SlackSendTool.

PARAMETER DESCRIPTION
bot_token

Bot User OAuth Token starting with "xoxb-".

TYPE: str

channel_id

Channel ID where messages will be sent.

TYPE: str

Source code in autogen/tools/experimental/messageplatform/slack/slack.py
def __init__(self, *, bot_token: str, channel_id: str) -> None:
    """
    Initialize the SlackSendTool.

    Args:
        bot_token: Bot User OAuth Token starting with "xoxb-".
        channel_id: Channel ID where messages will be sent.
    """

    # Function that sends the message, uses dependency injection for bot token / channel / guild
    async def slack_send_message(
        message: Annotated[str, "Message to send to the channel."],
        bot_token: Annotated[str, Depends(on(bot_token))],
        channel_id: Annotated[str, Depends(on(channel_id))],
    ) -> Any:
        """
        Sends a message to a Slack channel.

        Args:
            message: The message to send to the channel.
            bot_token: The bot token to use for Slack. (uses dependency injection)
            channel_id: The ID of the channel. (uses dependency injection)
        """
        try:
            web_client = WebClient(token=bot_token)

            # Send the message
            if len(message) > MAX_MESSAGE_LENGTH:
                chunks = [
                    message[i : i + (MAX_MESSAGE_LENGTH - 1)]
                    for i in range(0, len(message), (MAX_MESSAGE_LENGTH - 1))
                ]
                for i, chunk in enumerate(chunks):
                    response = web_client.chat_postMessage(channel=channel_id, text=chunk)

                    if not response["ok"]:
                        return f"Message send failed on chunk {i + 1}, Slack response error: {response['error']}"

                    # Store ID for the first chunk
                    if i == 0:
                        sent_message_id = response["ts"]

                return f"Message sent successfully ({len(chunks)} chunks, first ID: {sent_message_id}):\n{message}"
            else:
                response = web_client.chat_postMessage(channel=channel_id, text=message)

                if not response["ok"]:
                    return f"Message send failed, Slack response error: {response['error']}"

                return f"Message sent successfully (ID: {response['ts']}):\n{message}"
        except SlackApiError as e:
            return f"Message send failed, Slack API exception: {e.response['error']} (See https://api.slack.com/automation/cli/errors#{e.response['error']})"
        except Exception as e:
            return f"Message send failed, exception: {e}"

    super().__init__(
        name="slack_send",
        description="Sends a message to a Slack channel.",
        func_or_tool=slack_send_message,
    )

name property #

name

description property #

description

func property #

func

tool_schema property #

tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

function_schema property #

function_schema

Get the schema for the function.

This is the old way of handling function calls with OpenAI and compatible frameworks. It is provided for backward compatibility.

realtime_tool_schema property #

realtime_tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

register_for_llm #

register_for_llm(agent)

Registers the tool for use with a ConversableAgent's language model (LLM).

This method registers the tool so that it can be invoked by the agent during interactions with the language model.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_llm(self, agent: "ConversableAgent") -> None:
    """Registers the tool for use with a ConversableAgent's language model (LLM).

    This method registers the tool so that it can be invoked by the agent during
    interactions with the language model.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_llm()(self)

register_for_execution #

register_for_execution(agent)

Registers the tool for direct execution by a ConversableAgent.

This method registers the tool so that it can be executed by the agent, typically outside of the context of an LLM interaction.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_execution(self, agent: "ConversableAgent") -> None:
    """Registers the tool for direct execution by a ConversableAgent.

    This method registers the tool so that it can be executed by the agent,
    typically outside of the context of an LLM interaction.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_execution()(self)

register_tool #

register_tool(agent)

Register a tool to be both proposed and executed by an agent.

Equivalent to calling both register_for_llm and register_for_execution with the same agent.

Note: This will not make the agent recommend and execute the call in the one step. If the agent recommends the tool, it will need to be the next agent to speak in order to execute the tool.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_tool(self, agent: "ConversableAgent") -> None:
    """Register a tool to be both proposed and executed by an agent.

    Equivalent to calling both `register_for_llm` and `register_for_execution` with the same agent.

    Note: This will not make the agent recommend and execute the call in the one step. If the agent
    recommends the tool, it will need to be the next agent to speak in order to execute the tool.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    self.register_for_llm(agent)
    self.register_for_execution(agent)