2025-08-21 18:33:54 +08:00
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
import { readFileSync } from "fs";
|
|
|
|
|
|
import { log_success, log_error, log_info } from "./utils.mjs";
|
|
|
|
|
|
|
2025-08-21 21:23:38 +08:00
|
|
|
|
const CHAT_ID_RELEASE = "@clash_verge_re"; // 正式发布频道
|
|
|
|
|
|
const CHAT_ID_TEST = "@vergetest"; // 测试频道
|
2025-08-21 18:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
async function sendTelegramNotification() {
|
|
|
|
|
|
if (!process.env.TELEGRAM_BOT_TOKEN) {
|
|
|
|
|
|
throw new Error("TELEGRAM_BOT_TOKEN is required");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-21 21:23:38 +08:00
|
|
|
|
const version =
|
|
|
|
|
|
process.env.VERSION ||
|
|
|
|
|
|
(() => {
|
|
|
|
|
|
const pkg = readFileSync("package.json", "utf-8");
|
|
|
|
|
|
return JSON.parse(pkg).version;
|
|
|
|
|
|
})();
|
2025-08-21 18:33:54 +08:00
|
|
|
|
|
2025-08-21 21:23:38 +08:00
|
|
|
|
const downloadUrl =
|
|
|
|
|
|
process.env.DOWNLOAD_URL ||
|
|
|
|
|
|
`https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v${version}`;
|
2025-08-21 18:33:54 +08:00
|
|
|
|
|
2025-08-21 21:23:38 +08:00
|
|
|
|
const isAutobuild =
|
|
|
|
|
|
process.env.BUILD_TYPE === "autobuild" || version.includes("autobuild");
|
2025-08-21 18:33:54 +08:00
|
|
|
|
const chatId = isAutobuild ? CHAT_ID_TEST : CHAT_ID_RELEASE;
|
|
|
|
|
|
const buildType = isAutobuild ? "滚动更新版" : "正式版";
|
|
|
|
|
|
|
|
|
|
|
|
log_info(`Preparing Telegram notification for ${buildType} ${version}`);
|
|
|
|
|
|
log_info(`Target channel: ${chatId}`);
|
|
|
|
|
|
log_info(`Download URL: ${downloadUrl}`);
|
|
|
|
|
|
|
|
|
|
|
|
// 读取发布说明和下载地址
|
|
|
|
|
|
let releaseContent = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
releaseContent = readFileSync("release.txt", "utf-8");
|
|
|
|
|
|
log_info("成功读取 release.txt 文件");
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
log_error("无法读取 release.txt,使用默认发布说明");
|
|
|
|
|
|
releaseContent = "更多新功能现已支持,详细更新日志请查看发布页面。";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Markdown 转换为 HTML
|
|
|
|
|
|
function convertMarkdownToTelegramHTML(content) {
|
|
|
|
|
|
return content
|
|
|
|
|
|
.split("\n")
|
2025-08-21 21:23:38 +08:00
|
|
|
|
.map((line) => {
|
2025-08-21 18:33:54 +08:00
|
|
|
|
if (line.trim().length === 0) {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
} else if (line.startsWith("## ")) {
|
|
|
|
|
|
return `<b>${line.replace("## ", "")}</b>`;
|
|
|
|
|
|
} else if (line.startsWith("### ")) {
|
|
|
|
|
|
return `<b>${line.replace("### ", "")}</b>`;
|
|
|
|
|
|
} else if (line.startsWith("#### ")) {
|
|
|
|
|
|
return `<b>${line.replace("#### ", "")}</b>`;
|
|
|
|
|
|
} else {
|
2025-08-21 21:23:38 +08:00
|
|
|
|
let processedLine = line.replace(
|
|
|
|
|
|
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
|
|
|
|
'<a href="$2">$1</a>',
|
|
|
|
|
|
);
|
|
|
|
|
|
processedLine = processedLine.replace(
|
|
|
|
|
|
/\*\*([^*]+)\*\*/g,
|
|
|
|
|
|
"<b>$1</b>",
|
|
|
|
|
|
);
|
2025-08-21 18:33:54 +08:00
|
|
|
|
return processedLine;
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.join("\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const formattedContent = convertMarkdownToTelegramHTML(releaseContent);
|
|
|
|
|
|
|
|
|
|
|
|
const releaseTitle = isAutobuild ? "滚动更新版发布" : "正式发布";
|
|
|
|
|
|
const content = `<b>🎉 <a href="https://github.com/clash-verge-rev/clash-verge-rev/releases/tag/v${version}">Clash Verge Rev v${version}</a> ${releaseTitle}</b>\n\n${formattedContent}`;
|
|
|
|
|
|
|
|
|
|
|
|
// 发送到 Telegram
|
|
|
|
|
|
try {
|
2025-08-21 21:23:38 +08:00
|
|
|
|
await axios.post(
|
|
|
|
|
|
`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`,
|
|
|
|
|
|
{
|
|
|
|
|
|
chat_id: chatId,
|
|
|
|
|
|
text: content,
|
|
|
|
|
|
link_preview_options: {
|
|
|
|
|
|
is_disabled: false,
|
|
|
|
|
|
url: `https://github.com/clash-verge-rev/clash-verge-rev/releases/tag/v${version}`,
|
|
|
|
|
|
prefer_large_media: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
parse_mode: "HTML",
|
2025-08-21 18:33:54 +08:00
|
|
|
|
},
|
2025-08-21 21:23:38 +08:00
|
|
|
|
);
|
2025-08-21 18:33:54 +08:00
|
|
|
|
log_success(`✅ Telegram 通知发送成功到 ${chatId}`);
|
|
|
|
|
|
} catch (error) {
|
2025-08-21 21:23:38 +08:00
|
|
|
|
log_error(
|
|
|
|
|
|
`❌ Telegram 通知发送失败到 ${chatId}:`,
|
|
|
|
|
|
error.response?.data || error.message,
|
|
|
|
|
|
);
|
2025-08-21 18:33:54 +08:00
|
|
|
|
process.exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 执行函数
|
|
|
|
|
|
sendTelegramNotification().catch((error) => {
|
|
|
|
|
|
log_error("脚本执行失败:", error.message);
|
|
|
|
|
|
process.exit(1);
|
|
|
|
|
|
});
|