Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ notification:
ntfy_server_url: "https://ntfy.sh" # ntfy服务器地址,默认使用公共服务,可改为自托管地址
ntfy_topic: "" # ntfy主题名称
ntfy_token: "" # ntfy访问令牌(可选,用于私有主题)
discord_url: "" # Discord 机器人的 webhook URL

# 用于让关注度更高的新闻在更前面显示,即用算法重新组合不同平台的热搜排序形成你侧重的热搜,合起来是 1 就行
weight:
Expand Down
49 changes: 49 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pytz
import requests
import yaml
import textwrap


VERSION = "3.0.5"
Expand Down Expand Up @@ -179,6 +180,11 @@ def load_config():
"ntfy_token", ""
)

# Discord配置
config["DISCORD_WEBHOOK_URL"] = os.environ.get(
"DISCORD_WEBHOOK_URL", ""
).strip() or webhooks.get("discord_url", "")

# 输出配置来源信息
notification_sources = []
if config["FEISHU_WEBHOOK_URL"]:
Expand All @@ -204,6 +210,10 @@ def load_config():
server_source = "环境变量" if os.environ.get("NTFY_SERVER_URL") else "配置文件"
notification_sources.append(f"ntfy({server_source})")

if config["DISCORD_WEBHOOK_URL"]:
source = "环境变量" if os.environ.get("DISCORD_WEBHOOK_URL") else "配置文件"
notification_sources.append(f"Discord({source})")

if notification_sources:
print(f"通知渠道配置来源: {', '.join(notification_sources)}")
else:
Expand Down Expand Up @@ -3339,6 +3349,7 @@ def send_to_notifications(
ntfy_server_url = CONFIG["NTFY_SERVER_URL"]
ntfy_topic = CONFIG["NTFY_TOPIC"]
ntfy_token = CONFIG.get("NTFY_TOKEN", "")
discord_webhook_url = CONFIG["DISCORD_WEBHOOK_URL"]

update_info_to_send = update_info if CONFIG["SHOW_VERSION_UPDATE"] else None

Expand Down Expand Up @@ -3397,6 +3408,14 @@ def send_to_notifications(
email_smtp_port,
)

# 发送 Discord(使用企业微信的 Markdown 模板生成文本)
if discord_webhook_url:
discord_batches = split_content_into_batches(
report_data, "wework", update_info_to_send, mode=mode
)
discord_message = "\n\n------\n\n".join(discord_batches)
results["discord"] = send_discord_message(discord_message)

if not results:
print("未配置任何通知渠道,跳过通知发送")

Expand Down Expand Up @@ -3717,6 +3736,35 @@ def send_to_telegram(
return True


def send_discord_message(content: str) -> bool:
"""使用 Discord Webhook 推送文本消息"""
webhook_url = CONFIG.get("DISCORD_WEBHOOK_URL", "")
if not webhook_url:
print("未配置 DISCORD_WEBHOOK_URL,跳过 Discord 推送")
return False

if not content:
print("Discord 推送内容为空,跳过发送")
return False

# Discord 单条消息最大 2000 字符,保险起见拆成 1800 字符一段
chunks = textwrap.wrap(content, 1800, replace_whitespace=False)

success = True
for chunk in chunks:
payload = {"content": chunk}
try:
resp = requests.post(webhook_url, json=payload, timeout=10)
if resp.status_code >= 400:
success = False
print(f"Discord 推送失败:{resp.status_code} {resp.text}")
except Exception as e:
success = False
print(f"Discord 推送异常:{e}")

return success


def send_to_email(
from_email: str,
password: str,
Expand Down Expand Up @@ -4119,6 +4167,7 @@ def _has_notification_configured(self) -> bool:
and CONFIG["EMAIL_TO"]
),
(CONFIG["NTFY_SERVER_URL"] and CONFIG["NTFY_TOPIC"]),
CONFIG["DISCORD_WEBHOOK_URL"],
]
)

Expand Down