mirror of
https://github.com/Gloridust/WechatOnCloud.git
synced 2026-06-16 19:53:53 +08:00
GitHub-Flavored Markdown ≠ Telegram MarkdownV2, so the old plain-text send showed literal ## ** | etc. New .github/scripts/tg-notify.mjs converts GFM → Telegram-safe HTML (<b>/<i>/<code>/<pre>/<a>; headers→bold, tables→· rows, lists→•, quotes→▎), escapes <>&, and falls back to plain text if Telegram rejects the HTML. Adds workflow_dispatch to telegram-notify so you can send the latest release rendered for testing without cutting a new release. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
134 lines
5.5 KiB
JavaScript
134 lines
5.5 KiB
JavaScript
// 发布 / 新 issue → Telegram 群通知。把 GitHub Markdown 转成 Telegram 支持的 HTML 再发,
|
||
// 失败则回退纯文本,保证必达。无服务器,跑在 GitHub Actions(telegram-notify.yml)。
|
||
//
|
||
// 事件来源(env EVENT):
|
||
// release —— 发布事件,用 release 的 payload
|
||
// issues —— 新 issue 事件,用 issue 的 payload
|
||
// workflow_dispatch —— 手动触发,拉「最新 release」渲染发送(用于测试渲染效果)
|
||
|
||
const TG = process.env.TG_TOKEN;
|
||
const CHAT = process.env.TG_CHAT;
|
||
const EVENT = process.env.EVENT || '';
|
||
if (!TG || !CHAT) {
|
||
console.log('TELEGRAM_BOT_TOKEN / TELEGRAM_CHAT_ID 未配置,跳过');
|
||
process.exit(0);
|
||
}
|
||
|
||
const esc = (s) => (s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||
|
||
// 行内 Markdown → Telegram HTML(在已 HTML-escape 的文本上运行)
|
||
function inlineMd(s) {
|
||
s = s.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, t, u) => `<a href="${u.replace(/"/g, '"')}">${t}</a>`);
|
||
s = s.replace(/\*\*([^*]+?)\*\*/g, '<b>$1</b>');
|
||
s = s.replace(/__([^_]+?)__/g, '<b>$1</b>');
|
||
s = s.replace(/(^|[^*])\*([^*\n]+?)\*(?!\*)/g, '$1<i>$2</i>');
|
||
return s;
|
||
}
|
||
|
||
// GitHub Markdown → Telegram HTML。Telegram 不支持标题/表格/列表标记,转成粗体/·/• 等。
|
||
function mdToHtml(md) {
|
||
const blocks = [];
|
||
md = md.replace(/```[^\n]*\n([\s\S]*?)```/g, (_, code) => {
|
||
blocks.push('<pre>' + esc(code.replace(/\n$/, '')) + '</pre>');
|
||
return ` |