You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建Twitter API流规则:监听固定账户加任意账户的提及内容

实现同时提及@example和任意其他账户的X API流规则

要创建符合需求的流规则,核心是利用X API v2的搜索语法,结合逻辑运算符和过滤条件精准筛选推文:

规则语法

直接使用以下规则即可实现需求:

@example has:mentions -only:mentions @example

规则各部分解释

  • @example:强制要求推文包含对目标固定账户的提及
  • has:mentions:确保推文存在至少一个用户提及(配合后续条件,排除仅@example的情况)
  • -only:mentions @example:排除仅提及@example这一个账户的推文,剩下的就是同时包含@example和其他任意账户提及的内容

创建规则的示例代码

以Python为例,通过API请求添加规则:

import requests

# 替换为你的Bearer Token
BEARER_TOKEN = "your-bearer-token-here"
API_URL = "https://api.twitter.com/2/tweets/search/stream/rules"

headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
# 定义规则内容和标签(标签用于标识规则,方便后续管理)
payload = {
    "add": [
        {
            "value": "@example has:mentions -only:mentions @example",
            "tag": "提及@example+其他账户"
        }
    ]
}

response = requests.post(API_URL, json=payload, headers=headers)
# 检查请求结果
if response.status_code == 201:
    print("规则创建成功:", response.json())
else:
    print("规则创建失败:", response.json())

注意事项

  • 确保你的API密钥拥有Elevated或Academic Research访问级别,基础访问无法使用流API的高级过滤条件
  • 可以根据需求添加额外过滤条件,比如-is:retweet排除转推、lang:zh限定中文推文等
  • 如果需要监听多个固定账户,只需扩展规则为(@example1 OR @example2) has:mentions -only:mentions @example1 -only:mentions @example2

内容的提问来源于stack exchange,提问作者YulePale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:50:29