尝尝错过论坛的抽奖,于是想创建一个自动检测论坛的关键字的提醒,用telegram的机器人来给我发消息。
让我不错过任何一次抽奖。
下面的步骤是用来记录一些操作,因为自己怕忘了怎么来修改关键字。用的是PY来实现因为甲骨文的是ARM ai说不支持。
所以就用的py。
在你的工作目录(例如 /etc/docker/nodeseek-monitor)下创建 monitor.py:
将以下代码粘贴进去(记得修改 Telegram 配置):
!/usr/bin/env python3
import feedparser
import requests
import time
import re
import json
import os
========== 配置区 ==========
RSS_FEED_URL = 'https://rss.nodeseek.com/'
TELEGRAM_BOT_TOKEN = '你的Bot Token' # 替换为你的 Token
TELEGRAM_CHAT_ID = '你的Chat ID' # 替换为你的 Chat ID
CHECK_INTERVAL = 30 # 检查间隔(秒)
KEYWORDS_FILE = 'keywords.json' # 关键词配置文件
============================
sent_links = set()
if os.path.exists('sent_links.txt'):
with open('sent_links.txt', 'r') as f:
sent_links = set(line.strip() for line in f)
def load_keywords():
try:
with open(KEYWORDS_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
return data.get('keywords', [])
except:
return []
def save_sent_links():
with open('sent_links.txt', 'w') as f:
for link in sent_links:
f.write(link + '\n')
def check_keywords(text, keywords):
text_lower = text.lower()
for word in keywords:
if word.lower() in text_lower:
return True
return False
def send_to_telegram(title, link):
# 提取帖子编号(可选)
match = re.search(r'post-(\d+)-', link)
post_id = match.group(1) if match else ''
post_display = f" (<a href='{link}'>#{post_id}</a>)" if post_id else ""
text = f"<b>{title}</b>{post_display}"
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
data = {
'chat_id': TELEGRAM_CHAT_ID,
'text': text,
'parse_mode': 'HTML',
'disable_web_page_preview': True
}
try:
requests.post(url, data=data, timeout=10)
except Exception as e:
print(f"Telegram send error: {e}")
def check_rss():
keywords = load_keywords()
if not keywords:
print("没有设置关键词,请先编辑 keywords.json")
return
feed = feedparser.parse(RSS_FEED_URL)
for entry in feed.entries:
if entry.link not in sent_links:
if check_keywords(entry.title, keywords):
print(f"匹配关键词: {entry.title}")
send_to_telegram(entry.title, entry.link)
sent_links.add(entry.link)
save_sent_links()
if name == "__main__":
print("NodeSeek 监控脚本已启动,按 Ctrl+C 停止")
while True:
try:
check_rss()
except Exception as e:
print(f"Error: {e}")
time.sleep(CHECK_INTERVAL)
- 创建关键词配置文件
在同目录下创建 keywords.json,填入你想监控的关键词(支持带空格的短语,不区分大小写):
{
"keywords": ["抽奖", "dmit isif", "cc", "斯巴达"]
} - 安装依赖
确保已安装 Python 3 和 pip,然后安装所需库:
pip3 install feedparser requests - 测试运行
python3 monitor.py - 后台运行(使用 systemd)
为了长期运行,推荐使用 systemd 服务。
创建服务文件:
/etc/systemd/system/nodeseek-monitor.service
填入以下内容(注意修改路径):
[Unit]
Description=NodeSeek Keyword Monitor
After=network.target
[Service]
ExecStart=/usr/bin/python3 /etc/docker/nodeseek-monitor/monitor.py
WorkingDirectory=/etc/docker/nodeseek-monitor
Restart=always
RestartSec=10
User=root
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
然后启动并设置开机自启:
sudo systemctl daemon-reload
sudo systemctl start nodeseek-monitor
sudo systemctl enable nodeseek-monitor
查看状态和日志:
sudo systemctl status nodeseek-monitor
sudo journalctl -u nodeseek-monitor -f