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

先决条件

安装所需的库:

pip install tweepy
Tweepy 是一个用于与 X API 交互的 Python 库。

设置

  1. 创建 X 开发者账户

    • 访问 developer.x.com 并申请开发者访问权限
    • 在您的开发者门户中创建一个新的项目和应用
  2. 生成 API 凭证

    • 导航到您的应用的“密钥和令牌”部分
    • 生成并复制这些凭证:
      • API 密钥和密钥
      • Bearer Token
      • Access Token 和 Secret
  3. 配置环境变量

    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
    

示例

cookbook/tools/x_tools.py
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)
看看 Tweet Analysis Agent 以获取更高级的示例。

工具集参数

参数类型默认值描述
bearer_tokenstrNone用于身份验证的 Bearer token
consumer_keystrNone用于身份验证的 Consumer key
consumer_secretstrNone用于身份验证的 Consumer secret
access_tokenstrNone用于身份验证的 Access token
access_token_secretstrNone用于身份验证的 Access token secret
include_post_metricsboolFalse在搜索结果中包含帖子指标(点赞、转推等)
wait_on_rate_limitboolFalse达到速率限制时重试

工具集函数

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

开发者资源