使用说明:
1、将代码中的TARGETS替换为你需要监控的页面URL和信息
2、将TELEGRAM_BOT_TOKEN替换为你的Telegram Bot Token
3、将TELEGRAM_CHAT_ID替换为你的Telegram Chat ID
4、在Cloudflare Workers中创建新Worker,粘贴代码
在worker里配置触发器:
在Workers的Triggers页面添加Cron Trigger
设置Cron表达式为* * * * *(每分钟执行)
保存并部署。其他家的补货也可以添加进来~~
最后,祝大家玩的愉快~~
export default { async fetch(request, env, ctx) { // 默认页面响应 const html = ` <!DOCTYPE html> <html> <head> <title>Stock 监控</title> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; text-align: center; padding: 50px; } h1 { color: #333; } p { color: #666; } </style> </head> <body> <h1>Stock 监控运行中</h1> <p>每 60 秒检查一次新回复,并通过 Telegram 通知。</p> </body> </html> `; await handleRequest(env); return new Response(html, { headers: { 'Content-Type': 'text/html; charset=utf-8' }, status: 200 }); }, async scheduled(event, env, ctx) { ctx.waitUntil(handleRequest(env)); }, };async function handleRequest(request) { // Telegram Bot配置 const TELEGRAM_BOT_TOKEN = 'TELEGRAM_BOT_TOKEN'; const TELEGRAM_CHAT_ID = 'TELEGRAM_CHAT_ID'; const TELEGRAM_API = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`; // 目标地址、对应的缺货标志词、名称和描述 const TARGETS = [ { name: 'hostdzire 32刀', url: 'https://hostdzire.com/billing/index.php?rp=/store/indian-cloudvps/in-cloudvps-5-nodeseek-special', outOfStockText: 'out of stock', description: `hostdzire 32刀闪购补货了。 ` }, { name: 'colocrossing E3-2124G', url: 'https://portal.colocrossing.com/register/order/service/592', outOfStockText: 'this service is not available', description: `colocrossing E3-2124G有货了。` } ]; // 转义MarkdownV2特殊字符,仅用于动态内容 function escapeMarkdown(text) { return text.replace(/([_*[\]()~`>#+=|{}!.])/g, '\\$1'); } // 存储有货的目标信息 const inStockTargets = []; async function checkStock() { // 记录循环开始 console.log('Starting stock check for all targets...'); for (const target of TARGETS) { console.log(`Checking target: ${target.name} (${target.url})`); try { // 设置超时(例如 10 秒) const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 10000); const response = await fetch(target.url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' }, signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const text = await response.text(); // 检查页面是否包含对应的缺货标志词 if (!text.toLowerCase().includes(target.outOfStockText.toLowerCase())) { console.log(`Stock found for ${target.name}`); // 添加到有货列表 inStockTargets.push({ name: target.name, message: `🎉 ` + target.description + `\n\n🔗 ${target.url}` }); } else { console.log(`No stock for ${target.name}`); } } catch (error) { console.error(`Error checking ${target.name} (${target.url}):`, error.message); // 发送错误通知,仅转义动态内容 await fetch(TELEGRAM_API, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_id: TELEGRAM_CHAT_ID, text: `监控错误 (${escapeMarkdown(target.name)}): ${escapeMarkdown(error.message)}`, parse_mode: 'MarkdownV2' }) }); // 继续循环,不中断 continue; } } console.log('Completed stock check for all targets.'); // 如果有货,发送合并通知 if (inStockTargets.length > 0) { console.log(`Found ${inStockTargets.length} targets in stock, sending combined notification...`); // 合并消息 const combinedMessage = inStockTargets.map(target => target.message).join("\n\n ----- \n\n"); console.log(combinedMessage); const telegramResponse = await fetch(TELEGRAM_API, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_id: TELEGRAM_CHAT_ID, text: combinedMessage }) }); if (!telegramResponse.ok) { console.error(`Failed to send Telegram notification: ${telegramResponse.status}`); } else { console.log('Combined notification sent successfully.'); } } else { console.log('No targets in stock, no notification sent.'); } console.log('Completed stock check for all targets.'); } // 立即执行一次检查 await checkStock(); // 返回响应(Cloudflare Workers需要返回Response对象) return new Response('Monitor running', { status: 200 });}
评论 (0)