> ## 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.

# X (Twitter)

**XTools** 允许 Agent 与 X 进行交互，提供发布、发送消息和搜索推文的功能。

## 先决条件

安装所需的库：

```shell theme={null}
pip install tweepy
```

<Info>Tweepy 是一个用于与 X API 交互的 Python 库。</Info>

## 设置

1. **创建 X 开发者账户**
   * 访问 [developer.x.com](https://developer.x.com) 并申请开发者访问权限
   * 在您的开发者门户中创建一个新的项目和应用

2. **生成 API 凭证**
   * 导航到您的应用的“密钥和令牌”部分
   * 生成并复制这些凭证：
     * API 密钥和密钥
     * Bearer Token
     * Access Token 和 Secret

3. **配置环境变量**
   ```shell theme={null}
   export X_CONSUMER_KEY=your_api_key
   export X_CONSUMER_SECRET=your_api_secret
   export X_ACCESS_TOKEN=your_access_token
   export X_ACCESS_TOKEN_SECRET=your_access_token_secret
   export X_BEARER_TOKEN=your_bearer_token
   ```

## 示例

```python cookbook/tools/x_tools.py theme={null}
from agno.agent import Agent
from agno.tools.x import XTools

# 初始化 X 工具集
x_tools = XTools(
    wait_on_rate_limit=True # 达到速率限制时重试
) 

# 创建一个配备 X 工具集的代理
agent = Agent(
    instructions=[
        "使用 X 工具作为授权用户进行交互",
        "在被要求创建帖子时生成适当的内容",
        "仅在明确指示时发布内容",
        "遵守 X 的使用政策和速率限制",
    ],
    tools=[x_tools],
    show_tool_calls=True,
)

# 搜索帖子
agent.print_response("Search for recent posts about AI agents", markdown=True)

# 创建并发布一条推文
agent.print_response("Create a post about AI ethics", markdown=True)

# 获取用户时间线
agent.print_response("Get my timeline", markdown=True)

# 回复一条帖子
agent.print_response(
    "Can you reply to this [post ID] post as a general message as to how great this project is: https://x.com/AgnoAgi",
    markdown=True,
)

# 获取用户信息
agent.print_response("Can you retrieve information about this user https://x.com/AgnoAgi ", markdown=True)

# 发送直接消息
agent.print_response(
    "Send direct message to the user @AgnoAgi telling them I want to learn more about them and a link to their community.",
    markdown=True,
)

# 获取用户个人资料
agent.print_response("Get my X profile", markdown=True)
```

<Note> 看看 [Tweet Analysis Agent](/examples/agents/tweet-analysis-agent) 以获取更高级的示例。 </Note>

## 工具集参数

| 参数                     | 类型     | 默认值     | 描述                          |
| ---------------------- | ------ | ------- | --------------------------- |
| `bearer_token`         | `str`  | `None`  | 用于身份验证的 Bearer token        |
| `consumer_key`         | `str`  | `None`  | 用于身份验证的 Consumer key        |
| `consumer_secret`      | `str`  | `None`  | 用于身份验证的 Consumer secret     |
| `access_token`         | `str`  | `None`  | 用于身份验证的 Access token        |
| `access_token_secret`  | `str`  | `None`  | 用于身份验证的 Access token secret |
| `include_post_metrics` | `bool` | `False` | 在搜索结果中包含帖子指标（点赞、转推等）        |
| `wait_on_rate_limit`   | `bool` | `False` | 达到速率限制时重试                   |

## 工具集函数

| 函数                  | 描述            |
| ------------------- | ------------- |
| `create_post`       | 创建并发布新帖子      |
| `reply_to_post`     | 回复现有帖子        |
| `send_dm`           | 向 X 用户发送直接消息  |
| `get_user_info`     | 检索关于 X 用户的信息  |
| `get_home_timeline` | 获取已认证用户的首页时间线 |
| `search_posts`      | 搜索推文          |

## 开发者资源

* 查看 [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/x.py)
* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/tools/x_tools.py)
* 查看 [Tweet Analysis Agent Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/examples/agents/social_media_agent.py)
