> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Telegram

**TelegramTools** 使代理能够使用 Telegram Bot API 向 Telegram 聊天发送消息。

## 先决条件

```shell theme={null}
pip install -U agno httpx
```

```shell theme={null}
export TELEGRAM_TOKEN=***
```

## 示例

下面的代理将向 Telegram 聊天发送消息。

```python cookbook/tools/tavily_tools.py theme={null}
from agno.agent import Agent
from agno.tools.telegram import TelegramTools

# 如何获取 token 和 chat_id：
# 1. 在 Telegram 上使用 BotFather 创建一个新机器人。https://core.telegram.org/bots/features#creating-a-new-bot
# 2. 从 BotFather 获取 token。
# 3. 向机器人发送消息。
# 4. 通过访问以下 URL 获取 chat_id：
#    https://api.telegram.org/bot/<your-bot-token>/getUpdates

telegram_token = "<enter-your-bot-token>"
chat_id = "<enter-your-chat-id>"

agent = Agent(
    name="telegram",
    tools=[TelegramTools(token=telegram_token, chat_id=chat_id)],
)

agent.print_response("向 Telegram 聊天发送一段关于月球的文字")
```

## Toolkit 参数

| 参数        | 类型                | 默认值    | 描述                                                     |
| --------- | ----------------- | ------ | ------------------------------------------------------ |
| `token`   | `Optional[str]`   | `None` | Telegram Bot API token。如果未提供，将检查 TELEGRAM\_TOKEN 环境变量。 |
| `chat_id` | `Union[str, int]` | -      | 要发送消息的聊天 ID。                                           |

## Toolkit 函数

| 函数             | 描述                                                               |
| -------------- | ---------------------------------------------------------------- |
| `send_message` | 向指定的 Telegram 聊天发送消息。接受消息字符串作为输入，并以文本形式返回 API 响应。如果发生错误，则返回错误消息。 |

## 开发者资源

* 查看 [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/telegram.py)
* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/tools/telegram_tools.py)
