TweetStream

Twitter 到 Discord Webhook:分步配置指南 (2026)

完整教程:使用 Webhook、代码示例与富 Embed 格式将实时 Twitter/X 提醒路由到 Discord 频道。

为什么要将 Twitter 提醒路由到 Discord?

许多加密团队已经通过 Discord 中的交易群、研究频道和执行面板协作。把 Twitter/X 提醒路由到同一个工作区,团队可以直接在那里查看信号。

Discord webhook 会把格式化的 Twitter/X 提醒发送到团队已经关注的频道,无需再盯着另一个应用或邮箱。

什么是 Discord Webhook?

Discord Webhook 是一个接收 HTTP POST 请求并将消息发布到指定频道的 URL。你不需要机器人账号、OAuth 令牌或网关连接;只需向该 URL 发送 JSON,消息就会出现在频道中。

Webhook URL 格式:https://discord.com/api/webhooks/WEBHOOK_ID/WEBHOOK_TOKEN

Discord Webhook 支持纯文本消息、富 Embed(包含标题、颜色、字段、图片和页脚)、文件附件,以及每条消息最多 10 个 Embed。

第 1 步:创建 Discord Webhook

  1. 打开你的 Discord 服务器并进入要接收提醒的频道
  2. 点击频道名称旁的齿轮图标(编辑频道)
  3. 进入集成 → Webhook → 新建 Webhook
  4. 为 Webhook 命名(例如“Twitter 提醒”),并可选择设置头像
  5. 点击“复制 Webhook URL”,并妥善保存该 URL
  6. 点击保存

请将 Webhook URL 保密。任何获得该 URL 的人都可以向你的频道发布消息。

第 2 步:了解 Payload 格式

Discord Webhook 接收包含两个主要字段的 JSON Payload:content 用于纯文本,embeds 用于富格式消息。对于交易提醒,建议使用 embeds,因为它支持结构化数据、颜色编码和行内字段。

第 3 步:使用 Node.js 发送提醒

javascript
const WEBHOOK_URL = 'https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN';
 
function normalizeHandle(handle) {
  return String(handle ?? '').replace(/^@/, '');
}
 
function postLabel(tweet) {
  if (tweet.author.platform === 'binance_square') return 'Binance Square post';
  if (tweet.author.platform === 'truth_social') return 'Truth';
  return 'X post';
}
 
function postUrl(tweet, handle) {
  if (tweet.author.platform === 'binance_square') {
    return /^\d+$/.test(tweet.tweetId)
      ? `https://www.binance.com/en/square/post/${tweet.tweetId}`
      : undefined;
  }
  if (tweet.author.platform === 'truth_social') {
    return tweet.link ?? `https://truthsocial.com/@${handle}/posts/${tweet.tweetId}`;
  }
  return tweet.link ?? `https://x.com/${handle}/status/${tweet.tweetId}`;
}
 
async function sendPostAlert(tweet) {
  const handle = normalizeHandle(tweet.author.handle);
  const embed = {
    title: postLabel(tweet),
    description: tweet.text,
    color: 3447003, // Blue
    url: postUrl(tweet, handle),
    author: { name: handle ? `@${handle}` : postLabel(tweet) },
    fields: [],
    footer: { text: 'TweetStream Alert' },
    timestamp: new Date().toISOString(),
  };
 
  // Add detected tokens as fields
  if (tweet.tokens?.length) {
    for (const token of tweet.tokens) {
      embed.fields.push({
        name: token.symbol,
        value: `$${token.price} | ${token.chain}`,
        inline: true,
      });
    }
  }
 
  const response = await fetch(WEBHOOK_URL, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ embeds: [embed] }),
  });
 
  if (response.status === 429) {
    const retryAfter = response.headers.get('Retry-After');
    console.log(`Rate limited. Retry after ${retryAfter}s`);
  }
}

第 4 步:使用 Python 发送提醒

python
import requests
import time
from typing import Optional
 
WEBHOOK_URL = "https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN"
 
def normalize_handle(handle: object) -> str:
    return str(handle or "").removeprefix("@")
 
def post_label(tweet: dict) -> str:
    platform = tweet.get("author", {}).get("platform", "twitter")
    if platform == "binance_square":
        return "Binance Square post"
    if platform == "truth_social":
        return "Truth"
    return "X post"
 
def post_url(tweet: dict, handle: str) -> Optional[str]:
    platform = tweet.get("author", {}).get("platform", "twitter")
    if platform == "binance_square":
        post_id = str(tweet["tweetId"])
        return f"https://www.binance.com/en/square/post/{post_id}" if post_id.isascii() and post_id.isdigit() else None
    if platform == "truth_social":
        return tweet.get("link") or f"https://truthsocial.com/@{handle}/posts/{tweet['tweetId']}"
    return tweet.get("link") or f"https://x.com/{handle}/status/{tweet['tweetId']}"
 
def send_post_alert(tweet: dict) -> None:
    handle = normalize_handle(tweet.get("author", {}).get("handle"))
    embed = {
        "title": post_label(tweet),
        "description": tweet["text"],
        "color": 3447003,  # Blue
        "url": post_url(tweet, handle),
        "author": {"name": f"@{handle}" if handle else post_label(tweet)},
        "fields": [],
        "footer": {"text": "TweetStream Alert"},
    }
 
    # Add detected tokens
    for token in tweet.get("tokens", []):
        embed["fields"].append({
            "name": token["symbol"],
            "value": f"${token['price']} | {token['chain']}",
            "inline": True,
        })
 
    payload = {"embeds": [embed]}
    resp = requests.post(WEBHOOK_URL, json=payload)
 
    if resp.status_code == 429:
        retry_after = resp.json().get("retry_after", 1)
        time.sleep(retry_after)
        requests.post(WEBHOOK_URL, json=payload)

交易提醒的富 Embed 格式

Discord Embed 支持丰富的格式设置。以下是交易提醒的关键字段:

  • title — 提醒标题(例如账号名或代币符号)
  • description — 推文文本或提醒正文
  • color — 十进制颜色值(绿色:3066993,红色:15158332,蓝色:3447003)
  • fields — 结构化数据的名称/值对(代币、价格、链)
  • footer — 时间戳或来源归属
  • thumbnail — 小图(例如代币 Logo 或用户头像)
  • url — 返回原始推文的链接

Discord Webhook 速率限制

Discord 会对 Webhook 执行速率限制;发送过快时会返回 HTTP 429 和 Retry-After 值。具体限制可能因路由和场景而异,因此应依据响应标头构建处理逻辑,而不是硬编码一个通用数值。

对于高流量提醒频道,请采用以下策略:

  • 将消息加入队列并批量发送,而不是逐条立即发送
  • 为不同提醒类型在不同频道中使用多个 Webhook
  • 收到 429 响应时遵循 Retry-After
  • 使用多个 Embed 将多条提醒合并到一条消息中(每条消息最多 10 个)
  • 将响应标头作为重试时机的事实来源

常见问题排查

  • Webhook 返回 401 Unauthorized — Webhook URL 已被删除或重新生成。请创建新的 Webhook。
  • 消息未出现 — 检查 Webhook 是否分配给正确频道,以及机器人是否具有发送消息权限。
  • Embed 未渲染 — 检查 JSON 结构。即使只有一个 Embed,embeds 字段也必须是数组;color 必须是十进制数字,不能是十六进制值。
  • 触发速率限制(429)— 降低请求速率,将消息加入队列并实现退避。
  • 内容过长 — Embed 描述上限为 4,096 个字符,所有 Embed 的内容总计不得超过 6,000 个字符。

TweetStream Discord 投递

如果你不想自行构建和维护 Webhook 管线,TweetStream 可以零代码将提醒直接投递到 Discord。在控制台配置账号和 Webhook 后,TweetStream 会处理过滤、富化(OCR、代币检测、实时价格)、格式化与投递。配置详情请参阅 Discord 加密提醒 页面。

更快把这个工作流投入生产

这篇文章介绍了这类工作流。TweetStream 负责受监控帖子的接入、代币检测和 OCR,并通过 WebSocket 投递帖子、更新、删除、置顶、资料变化、关注、取消关注和富化 JSON 载荷。支持的套餐还提供历史回放。你的客户端负责按文档重连 WebSocket,并把收到的事件路由到下游。你可以用自己的账号开始 3 天试用。

开始 3 天试用

常见问题

TweetStream 团队

最近更新:2026 年 6 月

把实时信号接入工作流

从关键 X 账号开始,把事件路由到你的机器人、提醒和交易工作流。