mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-23 00:21:28 +00:00
feat(telegram): render markdown and send author avatar photo
This commit is contained in:
parent
568e206643
commit
50ae8c7607
4 changed files with 175 additions and 32 deletions
|
|
@ -8,35 +8,50 @@ function esc(s: string): string {
|
|||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function inlineUrl(url?: string, text?: string): string {
|
||||
const label = esc(text ?? url ?? "");
|
||||
if (!url) return label;
|
||||
return `<a href="${esc(url)}">${label}</a>`;
|
||||
function mdToHtml(s: string): string {
|
||||
let out = esc(s);
|
||||
out = out.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label, url) => `<a href="${esc(url)}">${label}</a>`);
|
||||
out = out.replace(/\*\*([^*]+)\*\*/g, "<b>$1</b>");
|
||||
out = out.replace(/`([^`]+)`/g, "<code>$1</code>");
|
||||
out = out.replace(/(^|[^*])\*([^*]+)\*/g, "$1<i>$2</i>");
|
||||
out = out.replace(/~~([^~]+)~~/g, "<s>$1</s>");
|
||||
return out;
|
||||
}
|
||||
|
||||
function formatTimestamp(ts?: string): string {
|
||||
if (!ts) return "";
|
||||
const d = new Date(ts);
|
||||
if (Number.isNaN(d.getTime())) return ts;
|
||||
const pad = (n: number): string => String(n).padStart(2, "0");
|
||||
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`;
|
||||
}
|
||||
|
||||
export function renderNeutralMessage(message: NeutralMessage): string {
|
||||
const parts: string[] = [];
|
||||
const title = inlineUrl(message.url, message.title);
|
||||
|
||||
const title = message.url
|
||||
? `<a href="${esc(message.url)}">${mdToHtml(message.title)}</a>`
|
||||
: mdToHtml(message.title);
|
||||
parts.push(`<b>${title}</b>`);
|
||||
|
||||
if (message.author) {
|
||||
const author = message.author.url
|
||||
? `<a href="${esc(message.author.url)}">${esc(message.author.name)}</a>`
|
||||
: esc(message.author.name);
|
||||
const name = mdToHtml(message.author.name);
|
||||
const author = message.author.url ? `<a href="${esc(message.author.url)}">${name}</a>` : name;
|
||||
parts.push(`👤 ${author}`);
|
||||
}
|
||||
|
||||
if (message.description) {
|
||||
parts.push(esc(message.description));
|
||||
parts.push(mdToHtml(message.description));
|
||||
}
|
||||
|
||||
for (const field of message.fields ?? []) {
|
||||
parts.push(`<b>${esc(field.name)}</b>: ${esc(field.value)}`);
|
||||
parts.push(`<b>${mdToHtml(field.name)}</b>: ${mdToHtml(field.value)}`);
|
||||
}
|
||||
|
||||
const meta: string[] = [];
|
||||
if (message.footer) meta.push(esc(message.footer));
|
||||
if (message.timestamp) meta.push(esc(message.timestamp));
|
||||
const ts = formatTimestamp(message.timestamp);
|
||||
if (ts) meta.push(ts);
|
||||
if (meta.length > 0) {
|
||||
parts.push(`<i>${meta.join(" · ")}</i>`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue