refactor: improve code formatting and readability in autobuild and telegram scripts
This commit is contained in:
8
.github/workflows/autobuild.yml
vendored
8
.github/workflows/autobuild.yml
vendored
@@ -459,7 +459,13 @@ jobs:
|
|||||||
notify-telegram:
|
notify-telegram:
|
||||||
name: Notify Telegram
|
name: Notify Telegram
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [update_tag, autobuild-x86-windows-macos-linux, autobuild-arm-linux, autobuild-x86-arm-windows_webview2]
|
needs:
|
||||||
|
[
|
||||||
|
update_tag,
|
||||||
|
autobuild-x86-windows-macos-linux,
|
||||||
|
autobuild-arm-linux,
|
||||||
|
autobuild-x86-arm-windows_webview2,
|
||||||
|
]
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|||||||
@@ -53,7 +53,9 @@ function getGitShortCommit() {
|
|||||||
*/
|
*/
|
||||||
function getLatestTauriCommit() {
|
function getLatestTauriCommit() {
|
||||||
try {
|
try {
|
||||||
const fullHash = execSync("bash ./scripts-workflow/get_latest_tauri_commit.bash")
|
const fullHash = execSync(
|
||||||
|
"bash ./scripts-workflow/get_latest_tauri_commit.bash",
|
||||||
|
)
|
||||||
.toString()
|
.toString()
|
||||||
.trim();
|
.trim();
|
||||||
return execSync(`git rev-parse --short ${fullHash}`).toString().trim();
|
return execSync(`git rev-parse --short ${fullHash}`).toString().trim();
|
||||||
|
|||||||
@@ -2,22 +2,27 @@ import axios from "axios";
|
|||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { log_success, log_error, log_info } from "./utils.mjs";
|
import { log_success, log_error, log_info } from "./utils.mjs";
|
||||||
|
|
||||||
const CHAT_ID_RELEASE = "@clash_verge_re"; // 正式发布频道
|
const CHAT_ID_RELEASE = "@clash_verge_re"; // 正式发布频道
|
||||||
const CHAT_ID_TEST = "@vergetest"; // 测试频道
|
const CHAT_ID_TEST = "@vergetest"; // 测试频道
|
||||||
|
|
||||||
async function sendTelegramNotification() {
|
async function sendTelegramNotification() {
|
||||||
if (!process.env.TELEGRAM_BOT_TOKEN) {
|
if (!process.env.TELEGRAM_BOT_TOKEN) {
|
||||||
throw new Error("TELEGRAM_BOT_TOKEN is required");
|
throw new Error("TELEGRAM_BOT_TOKEN is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
const version = process.env.VERSION || (() => {
|
const version =
|
||||||
const pkg = readFileSync("package.json", "utf-8");
|
process.env.VERSION ||
|
||||||
return JSON.parse(pkg).version;
|
(() => {
|
||||||
})();
|
const pkg = readFileSync("package.json", "utf-8");
|
||||||
|
return JSON.parse(pkg).version;
|
||||||
|
})();
|
||||||
|
|
||||||
const downloadUrl = process.env.DOWNLOAD_URL || `https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v${version}`;
|
const downloadUrl =
|
||||||
|
process.env.DOWNLOAD_URL ||
|
||||||
|
`https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v${version}`;
|
||||||
|
|
||||||
const isAutobuild = process.env.BUILD_TYPE === "autobuild" || version.includes("autobuild");
|
const isAutobuild =
|
||||||
|
process.env.BUILD_TYPE === "autobuild" || version.includes("autobuild");
|
||||||
const chatId = isAutobuild ? CHAT_ID_TEST : CHAT_ID_RELEASE;
|
const chatId = isAutobuild ? CHAT_ID_TEST : CHAT_ID_RELEASE;
|
||||||
const buildType = isAutobuild ? "滚动更新版" : "正式版";
|
const buildType = isAutobuild ? "滚动更新版" : "正式版";
|
||||||
|
|
||||||
@@ -39,7 +44,7 @@ async function sendTelegramNotification() {
|
|||||||
function convertMarkdownToTelegramHTML(content) {
|
function convertMarkdownToTelegramHTML(content) {
|
||||||
return content
|
return content
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map(line => {
|
.map((line) => {
|
||||||
if (line.trim().length === 0) {
|
if (line.trim().length === 0) {
|
||||||
return "";
|
return "";
|
||||||
} else if (line.startsWith("## ")) {
|
} else if (line.startsWith("## ")) {
|
||||||
@@ -49,8 +54,14 @@ async function sendTelegramNotification() {
|
|||||||
} else if (line.startsWith("#### ")) {
|
} else if (line.startsWith("#### ")) {
|
||||||
return `<b>${line.replace("#### ", "")}</b>`;
|
return `<b>${line.replace("#### ", "")}</b>`;
|
||||||
} else {
|
} else {
|
||||||
let processedLine = line.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
|
let processedLine = line.replace(
|
||||||
processedLine = processedLine.replace(/\*\*([^*]+)\*\*/g, '<b>$1</b>');
|
/\[([^\]]+)\]\(([^)]+)\)/g,
|
||||||
|
'<a href="$2">$1</a>',
|
||||||
|
);
|
||||||
|
processedLine = processedLine.replace(
|
||||||
|
/\*\*([^*]+)\*\*/g,
|
||||||
|
"<b>$1</b>",
|
||||||
|
);
|
||||||
return processedLine;
|
return processedLine;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -64,19 +75,25 @@ async function sendTelegramNotification() {
|
|||||||
|
|
||||||
// 发送到 Telegram
|
// 发送到 Telegram
|
||||||
try {
|
try {
|
||||||
await axios.post(`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`, {
|
await axios.post(
|
||||||
chat_id: chatId,
|
`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`,
|
||||||
text: content,
|
{
|
||||||
link_preview_options: {
|
chat_id: chatId,
|
||||||
is_disabled: false,
|
text: content,
|
||||||
url: `https://github.com/clash-verge-rev/clash-verge-rev/releases/tag/v${version}`,
|
link_preview_options: {
|
||||||
prefer_large_media: true,
|
is_disabled: false,
|
||||||
|
url: `https://github.com/clash-verge-rev/clash-verge-rev/releases/tag/v${version}`,
|
||||||
|
prefer_large_media: true,
|
||||||
|
},
|
||||||
|
parse_mode: "HTML",
|
||||||
},
|
},
|
||||||
parse_mode: "HTML",
|
);
|
||||||
});
|
|
||||||
log_success(`✅ Telegram 通知发送成功到 ${chatId}`);
|
log_success(`✅ Telegram 通知发送成功到 ${chatId}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log_error(`❌ Telegram 通知发送失败到 ${chatId}:`, error.response?.data || error.message);
|
log_error(
|
||||||
|
`❌ Telegram 通知发送失败到 ${chatId}:`,
|
||||||
|
error.response?.data || error.message,
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user