宇宙主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

欧基零壹微头条IP归属甄别会员请立即修改密码
查看: 472|回复: 8

写个了网页关键词检测,从此传家宝不再错过

[复制链接]
发表于 前天 20:54 | 显示全部楼层 |阅读模式
  1. addEventListener("scheduled", event => {
  2.   event.waitUntil(checkKeywordsAndNotify());
  3. });

  4. async function checkKeywordsAndNotify() {
  5.   const url = 'https://fastfib.com';  // 要检测的网页URL
  6.   const keyword = 'FastFib VPS';  // 要检测的关键词
  7.   const telegramToken = '##';  // TG机器人Token
  8.   const chatId = '##';  //要推送的TG用户ID
  9.   
  10.   try {
  11.     const urlWithTimestamp = `${url}?t=${Date.now()}`;
  12.    
  13.     const response = await fetch(urlWithTimestamp, {
  14.       method: 'GET',
  15.       headers: {
  16.         'Cache-Control': 'no-cache',
  17.         'Pragma': 'no-cache'
  18.       }
  19.     });
  20.     const text = await response.text();

  21.     if (!text.includes(keyword)) {
  22.       await sendTelegramNotification(telegramToken, chatId, `关键词 "${keyword}" 已从 ${url} 上消失`);
  23.     }
  24.   } catch (error) {
  25.     await sendTelegramNotification(telegramToken, chatId, `获取或处理URL时出错: ${error.message}`);
  26.   }
  27. }

  28. async function sendTelegramNotification(token, chatId, message) {
  29.   const telegramUrl = `https://api.telegram.org/bot${token}/sendMessage`;
  30.   const response = await fetch(telegramUrl, {
  31.     method: 'POST',
  32.     headers: { 'Content-Type': 'application/json' },
  33.     body: JSON.stringify({
  34.       chat_id: chatId,
  35.       text: message
  36.     })
  37.   });
  38.   return response.json();
  39. }
复制代码



1,无需服务器,丢进cloudflare的workers
2,设定→触发程序→Cron


可以检测传家宝是否补货,也可以检测自己的网站是不是掉线了
发表于 前天 20:55 | 显示全部楼层
很多机器人可以直接检测啊 就是关键字不好搞
 楼主| 发表于 前天 20:58 | 显示全部楼层
有个缺点,一旦传家宝补货了,机器人会一直提醒你。数据库可以解决这个问题,但是想法就是简单稳定快捷。数据库增加了不确定因素
 楼主| 发表于 前天 21:01 | 显示全部楼层
燕十三丶 发表于 2024-9-18 20:55
很多机器人可以直接检测啊 就是关键字不好搞

别人的能跟自己建的比啊
发表于 前天 22:21 来自手机 | 显示全部楼层
https://github.com/Xinslive/NotifyMe
发表于 前天 22:22 | 显示全部楼层
平安喜乐 发表于 2024-9-18 22:21
https://github.com/Xinslive/NotifyMe

这个不错 简单实用
发表于 前天 22:22 来自手机 | 显示全部楼层
分享一下我的网页关键词检测开源项目
发表于 前天 22:26 | 显示全部楼层
优化了一下
  1. /**
  2. * Cloudflare Workers 入口:处理所有的 HTTP 请求
  3. * 每当有请求到达时,都会调用此方法并传递请求对象
  4. */
  5. addEventListener('fetch', event => {
  6.   // 使用 event.respondWith 方法,传入我们自定义的 handleRequest 函数来处理请求
  7.   event.respondWith(handleRequest(event.request));
  8. });

  9. /**
  10. * 处理传入的 HTTP 请求
  11. * @param {Request} request - HTTP 请求对象
  12. * @returns {Response} - 返回处理结果的 HTTP 响应对象
  13. */
  14. async function handleRequest(request) {
  15.   try {
  16.     // 从请求体中解析 JSON 数据,假设请求体包含需要检测的任务列表
  17.     const { tasks } = await request.json();

  18.     // 使用 Promise.all 同时执行多个任务
  19.     const results = await Promise.all(tasks.map(task => checkKeywordsAndNotify(task.url, task.keyword, task.telegramToken, task.chatId)));

  20.     // 返回一个包含所有任务结果的 JSON 响应
  21.     return new Response(JSON.stringify({ success: true, results }), {
  22.       headers: { 'Content-Type': 'application/json' },
  23.     });
  24.   } catch (error) {
  25.     // 如果有错误,返回错误信息
  26.     return new Response(JSON.stringify({ success: false, error: error.message }), {
  27.       headers: { 'Content-Type': 'application/json' },
  28.     });
  29.   }
  30. }

  31. /**
  32. * 检查指定 URL 是否包含关键字,并发送 Telegram 通知
  33. * @param {string} url - 要检测的网页 URL
  34. * @param {string} keyword - 要检测的关键词
  35. * @param {string} telegramToken - Telegram 机器人的 Token
  36. * @param {string} chatId - 要发送通知的 Telegram 用户 ID
  37. * @returns {Object} - 返回检测结果
  38. */
  39. async function checkKeywordsAndNotify(url, keyword, telegramToken, chatId) {
  40.   try {
  41.     // 为了防止缓存,给 URL 添加时间戳参数
  42.     const urlWithTimestamp = `${url}?t=${Date.now()}`;
  43.    
  44.     // 发送 GET 请求获取网页内容,禁止缓存
  45.     const response = await fetch(urlWithTimestamp, {
  46.       method: 'GET',
  47.       headers: {
  48.         'Cache-Control': 'no-cache',
  49.         'Pragma': 'no-cache',
  50.       },
  51.     });
  52.     const text = await response.text();

  53.     // 检查网页内容是否包含指定的关键词
  54.     if (!text.includes(keyword)) {
  55.       // 如果关键词不包含在网页中,发送 Telegram 通知
  56.       await sendTelegramNotification(telegramToken, chatId, `关键词 "${keyword}" 已从 ${url} 上消失`);
  57.       return { url, keyword, status: 'missing' };  // 返回状态:关键词消失
  58.     }
  59.     return { url, keyword, status: 'found' };  // 返回状态:关键词仍然存在
  60.   } catch (error) {
  61.     // 如果请求或处理过程中出错,发送错误通知
  62.     await sendTelegramNotification(telegramToken, chatId, `获取或处理URL时出错: ${error.message}`);
  63.     return { url, keyword, status: 'error', error: error.message };  // 返回错误信息
  64.   }
  65. }

  66. /**
  67. * 发送 Telegram 消息通知
  68. * @param {string} token - Telegram 机器人的 Token
  69. * @param {string} chatId - 要发送消息的 Telegram 用户 ID
  70. * @param {string} message - 要发送的消息内容
  71. * @returns {Object} - 返回 Telegram API 的响应
  72. */
  73. async function sendTelegramNotification(token, chatId, message) {
  74.   const telegramUrl = `https://api.telegram.org/bot${token}/sendMessage`;
  75.   const response = await fetch(telegramUrl, {
  76.     method: 'POST',
  77.     headers: { 'Content-Type': 'application/json' },
  78.     body: JSON.stringify({
  79.       chat_id: chatId,
  80.       text: message,
  81.     }),
  82.   });
  83.   return response.json();
  84. }
复制代码
发表于 前天 22:29 | 显示全部楼层
要是能自动下单就好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|FastFib VPS论坛

GMT+8, 2024-9-20 01:15 , Processed in 0.062002 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表