树洞剪藏#剪藏#树洞剪藏

基于CF worker手动撸一个自动监控补充的自动化通知

2025 年 7 月 23 日1 分钟
分享Twitter / XTelegram微博

同步来源:树洞剪藏 源站剪藏 ID:589 原文地址:https://www.nodeseek.com/post-401789-1

打开原文 · 打开源站剪藏快照


基于CF worker手动撸一个自动监控补充的自动化通知

NodeSeek beta 日常 技术 情报 测评 交易 拼车 推广

日常 技术 情报 测评 交易 拼车 推广 生活 Dev 贴图 曝光 内版 沙盒

基于CF worker手动撸一个自动监控补充的自动化通知

rouxyang Lv 1 楼主 Dev 8min ago in 技术 #0

使用说明:

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 = `

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 + `

🔗 ${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 ( "


" );

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 });

}

5 0 11 引用 回复

Vivi不懂 6min ago #1

绑定大佬

✈️私 丨 ▶️油管 丨 ❤️博客 丨 ✈️粉丝群 丨⚡刷PT-1元/TB丨 ⚡NC注册教程-新人5欧券:36nc17471050350

0 0 引用 回复

guboysky 5min ago #2

支持技术大佬

0 0 引用 回复

DmitChat 5min ago #3

大佬

♨️ DMIT交流群 ✅ RFCHost交流群 ㊙️ 炸酱面探针 教程

✈️ DMIT RFCHOST 搬瓦工补货通知 ✈️ 斯巴达补货通知

0 0 引用 回复

封景 4min ago #4

技术佬

0 0 引用 回复

qiuyufeng 3min ago edited 3min ago #5

支持

TG联系 | 个人博客

0 0 引用 回复

cola-xr9 3min ago #6

BD

TG联系 | 剩余价值计算 | 官方图床

0 0 引用 回复

Wucat 2min ago #7

大佬

站内私信

0 0 引用 回复

内容 预览 对照

支持 markdown语法

鼓励友善发言,禁止人身攻击

xxxxxxxxxx

1

1 ​

AC娘

洋葱头

小黄鸡

App

发布评论(Ctrl+Enter)

88

等级 Lv 5 鸡腿 2922 关注 0 回复 0 我 0

主题帖 12 评论数 402 粉丝 2 收藏 330 私信 0 发帖

快捷功能区 推荐阅读 管理记录 幸运抽奖 邀请好友 合作商家 友站链接

📈用户数目📈

目前论坛共有35702位seeker

🎉欢迎新用户🎉

兔子

Xeus-Victor5

smallmain

Bassepress

zkakka

lixingsrc

helen5106

丨小赖

相关网站 LowEndTalk LowEndSpirit HostLoc ServerHunter

站内导航 关于本站 隐私协议 RSS订阅 sitemap

商业推广 商家申请规则 Premium Provider 合作商家展示 广告合作

其他平台 电报频道 电报群组

联系我们

Copyright © 2022 - 2025 All rights Reserved

相关文章