logo NodeSeekbeta

更新了一下前两天的komari通知模版

原贴内容
原贴大佬:@Newbie-rd

新改代码

async function sendMessage(message, title, instanceId = null) {
  const token = "你的token";
  const chatId = "你的id";
  const panelUrl = "你的域名"; 

  if (!token || !chatId) return false;

  const url = `https://api.telegram.org/bot${token}/sendMessage`;
  
  // 构建交互按钮
  let inline_keyboard = [];
  let row1 = [{ text: "📊 进入面板", url: panelUrl }];

  // 这里的路径修正为 /instance/,匹配你提供的格式
  if (instanceId && instanceId !== '未知') {
    row1.push({ 
      text: "🌐 实例详情", 
      url: `${panelUrl}/instance/${instanceId}` 
    });
  }
  
  inline_keyboard.push(row1);

  const resp = await fetch(url, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      chat_id: chatId,
      text: `<b>${title}</b>\n\n${message}`,
      parse_mode: 'HTML',
      reply_markup: { inline_keyboard: inline_keyboard }
    }),
  });
  return resp.ok;
}

async function sendEvent(event) {
  try {
    // ⏰ 时间转换函数 (处理 CST)
    const getCSTTime = (timeStr) => {
      if (!timeStr || timeStr.startsWith('0001')) return "1-01-01 08:00:00";
      const date = new Date(timeStr.replace(/\.\d+Z$/, 'Z'));
      const cst = new Date(date.getTime() + 8 * 60 * 60 * 1000);
      const f = (n) => n.toString().padStart(2, '0');
      return `${cst.getUTCFullYear()}-${f(cst.getUTCMonth() + 1)}-${f(cst.getUTCDate())} ${f(cst.getUTCHours())}:${f(cst.getUTCMinutes())}:${f(cst.getUTCSeconds())}`;
    };

    // 📦 流量单位转换函数
    const formatTraffic = (bytes) => {
      if (!bytes || bytes === 0) return '无限制';
      const gb = bytes / (1024 ** 3);
      if (gb >= 1024) return `${(gb / 1024).toFixed(2)} TB`;
      return `${gb.toFixed(2)} GB`;
    };

    // 内存和磁盘格式化函数
    const formatMemory = (bytes) => {
      if (!bytes || bytes === 0) return '0';
      const gb = bytes / (1024 ** 3);
      return gb < 1 ? `${Math.round(gb * 1024)}MB` : `${Math.round(gb)}G`;
    };

    const formatSwapMemory = (bytes) => {
      if (!bytes || bytes === 0) return '0';
      const gb = bytes / (1024 ** 3);
      return gb < 1 ? `${Math.round(gb * 1024)}MB` : `${Math.round(gb)}G`;
    };

    // IP 地址部分隐藏函数
    const hideIP = (ip) => {
      if (!ip) return '未知';
      const parts = ip.split('.');
      if (parts.length === 4) {
        return `${parts[0]}.${parts[1]}.xxx.xxx`;
      }
      // 对于 IPv6 地址,只显示前半部分
      const v6Parts = ip.split(':');
      return v6Parts.slice(0, 3).join(':') + ':xxxx:xxxx:xxxx';
    };

    // 标题映射
    const eventTitles = {
      'Online':  '🟢 服务器上线',
      'Offline': '🔴 服务器离线',
      'Alert':   '⚠️ 异常警报',
      'Renew':   '💰 续费通知',
      'Expire':  '🚨 到期预警',
      'Test':    '🧪 测试通知'
    };

    const title = eventTitles[event.event] || '📌 系统通知';
    
    let clientInfo = '';
    let targetInstanceId = null;

    if (event.clients && event.clients.length > 0) {
      const c = event.clients[0];
      targetInstanceId = c.uuid;
      
      const region = c.region ? ` [${c.region}]` : '';
      const hiddenIPv4 = hideIP(c.ipv4);
      const hiddenIPv6 = hideIP(c.ipv6);
      
      const mem = c.mem_total ? formatMemory(c.mem_total) : '0';
      const swap = c.swap_total ? formatSwapMemory(c.swap_total) : '0';
      const disk = c.disk_total ? formatMemory(c.disk_total) : '0';
      
      clientInfo += `🖥️服务器:${c.name}${region}\n`;
      clientInfo += `📝配置:${c.cpu_cores || '0'}C / ${mem}${swap !== '0' ? `+${swap}` : ''} / ${disk}\n`;
      clientInfo += `🌐IPv4:${hiddenIPv4}\n`;
      clientInfo += `🌐IPv6:${hiddenIPv6}\n`;

      // 流量管家
      const trafficLimit = formatTraffic(c.traffic_limit);
      clientInfo += `📶流量限额:${trafficLimit}${c.traffic_limit_type ? ` (${c.traffic_limit_type})` : ''}\n`;
      
      if (event.event === 'Renew' || event.event === 'Expire') {
         clientInfo += `💰账单:${c.currency || '$'}${c.price || '0'} (${c.billing_cycle || '0'}天/付)\n`;
      }
    } else {
      clientInfo += `🖥️服务器:未知设备\n📝配置:未知\n🌐IPv4:未知\n🌐IPv6:未知\n📶流量限额:未知\n`;
    }

    let message = clientInfo;
    message += `\n🗒️状态:${event.event} (${eventTitles[event.event] || '未知'})\n`;
    message += `⌚️时间:${getCSTTime(event.time)} (CST)`;

    if (event.message && event.message.trim()) {
      message += `\n\n📄详细描述:\n<i>${event.message}</i>`;
    }

    // 发送通知,传入真正的 UUID 以生成正确的按钮链接
    return await sendMessage(message, title, targetInstanceId);
  } catch (error) {
    return await sendMessage(`脚本解析出错: ${error.message}`, '❌ Error');
  }
}

在源代码修改内容
1.简化通知标题
2.通知内容基础上添加图标
3.内存增加swap显示+号后边显示swap内存(如果有)
4.隐藏消息ip只显示前ip段

效果图
IMG_4489.jpeg

123
123

你好啊,陌生人!

我的朋友,看起来你是新来的,如果想参与到讨论中,点击下面的按钮!

📈用户数目📈

目前论坛共有63709位seeker

🎉欢迎新用户🎉