import axios from 'axios'
import { readFileSync } from 'fs'
import { getProcessedVersion, isDevBuild, getDownloadUrl, generateDownloadLinksMarkdown, getGitCommitHash } from './version-utils.mjs'
const chat_id = '@MihomoPartyChannel'
const pkg = readFileSync('package.json', 'utf-8')
const changelog = readFileSync('changelog.md', 'utf-8')
const { version: packageVersion } = JSON.parse(pkg)
// 获取处理后的版本号
const version = getProcessedVersion()
const releaseType = process.env.RELEASE_TYPE || process.argv[2] || 'release'
const isDevRelease = releaseType === 'dev' || isDevBuild()
function convertMarkdownToTelegramHTML(content) {
return content
.split("\n")
.map((line) => {
if (line.trim().length === 0) {
return "";
} else if (line.startsWith("## ")) {
return `${line.replace("## ", "")}`;
} else if (line.startsWith("### ")) {
return `${line.replace("### ", "")}`;
} else if (line.startsWith("#### ")) {
return `${line.replace("#### ", "")}`;
} else {
let processedLine = line.replace(
/\[([^\]]+)\]\(([^)]+)\)/g,
(match, text, url) => {
const encodedUrl = encodeURI(url);
return `${text}`;
},
);
processedLine = processedLine.replace(
/\*\*([^*]+)\*\*/g,
"$1",
);
return processedLine;
}
})
.join("\n");
}
let content = '';
if (isDevRelease) {
// 版本号中提取commit hash
const shortCommitSha = getGitCommitHash(true)
const commitSha = getGitCommitHash(false)
content = `🚧 Clash Party Dev Build 开发版本发布\n\n`
content += `基于版本: ${version}\n`
content += `提交哈希: ${shortCommitSha}\n\n`
content += `更新日志:\n`
content += convertMarkdownToTelegramHTML(changelog)
content += '\n\n⚠️ 注意:这是开发版本,可能存在不稳定性,仅供测试使用\n'
} else {
// 正式版本通知
content = `🌟 Clash Party v${version} 正式发布\n\n`
content += convertMarkdownToTelegramHTML(changelog)
}
// 构建下载链接
const downloadUrl = getDownloadUrl(isDevRelease, version)
const downloadLinksMarkdown = generateDownloadLinksMarkdown(downloadUrl, version)
content += convertMarkdownToTelegramHTML(downloadLinksMarkdown)
await axios.post(`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`, {
chat_id,
text: content,
link_preview_options: {
is_disabled: false,
url: 'https://github.com/mihomo-party-org/clash-party',
prefer_large_media: true
},
parse_mode: 'HTML'
})
console.log(`${isDevRelease ? '开发版本' : '正式版本'}Telegram 通知发送成功`)