Skip to content

TelegramSendTool

autogen.tools.experimental.TelegramSendTool #

TelegramSendTool(*, api_id, api_hash, chat_id)

Bases: BaseTelegramTool, Tool

Sends a message to a Telegram channel, group, or user.

Initialize the TelegramSendTool.

PARAMETER DESCRIPTION
api_id

Telegram API ID from https://my.telegram.org/apps.

TYPE: str

api_hash

Telegram API hash from https://my.telegram.org/apps.

TYPE: str

chat_id

The ID of the destination (Channel, Group, or User ID).

TYPE: str

Source code in autogen/tools/experimental/messageplatform/telegram/telegram.py
def __init__(self, *, api_id: str, api_hash: str, chat_id: str) -> None:
    """
    Initialize the TelegramSendTool.

    Args:
        api_id: Telegram API ID from https://my.telegram.org/apps.
        api_hash: Telegram API hash from https://my.telegram.org/apps.
        chat_id: The ID of the destination (Channel, Group, or User ID).
    """
    BaseTelegramTool.__init__(self, api_id, api_hash, "telegram_send_session")

    async def telegram_send_message(
        message: Annotated[str, "Message to send to the chat."],
        chat_id: Annotated[str, Depends(on(chat_id))],
    ) -> Any:
        """
        Sends a message to a Telegram chat.

        Args:
            message: The message to send.
            chat_id: The ID of the destination. (uses dependency injection)
        """
        try:
            client = self._get_client()
            async with client:
                # Initialize and cache the entity
                entity = await self._initialize_entity(client, chat_id)

                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))
                    ]
                    first_message: Union[Message, None] = None  # type: ignore[no-any-unimported]

                    for i, chunk in enumerate(chunks):
                        sent = await client.send_message(
                            entity=entity,
                            message=chunk,
                            parse_mode="html",
                            reply_to=first_message.id if first_message else None,
                        )

                        # Store the first message to chain replies
                        if i == 0:
                            first_message = sent
                            sent_message_id = str(sent.id)

                    return (
                        f"Message sent successfully ({len(chunks)} chunks, first ID: {sent_message_id}):\n{message}"
                    )
                else:
                    sent = await client.send_message(entity=entity, message=message, parse_mode="html")
                    return f"Message sent successfully (ID: {sent.id}):\n{message}"

        except Exception as e:
            return f"Message send failed, exception: {str(e)}"

    Tool.__init__(
        self,
        name="telegram_send",
        description="Sends a message to a personal channel, bot channel, group, or channel.",
        func_or_tool=telegram_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)