feat(telegram): render markdown and send author avatar photo

This commit is contained in:
RhenCloud 2026-08-03 06:42:29 +08:00
parent 568e206643
commit 50ae8c7607
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
4 changed files with 175 additions and 32 deletions

View file

@ -9,27 +9,13 @@ interface TelegramResponse {
result?: { message_id?: number };
}
export async function sendMessage(
async function post(
token: string,
method: string,
body: Record<string, unknown>,
chatId: string,
text: string,
topicId?: string,
): Promise<SendResult> {
if (!token) {
return { ok: false, error: "TELEGRAM_TOKEN not configured", errorCode: "NO_TOKEN" };
}
const body: Record<string, unknown> = {
chat_id: chatId,
text,
parse_mode: "HTML",
disable_web_page_preview: true,
};
if (topicId) {
body.message_thread_id = Number(topicId);
}
const url = `${TELEGRAM_API}/bot${token}/sendMessage`;
const url = `${TELEGRAM_API}/bot${token}/${method}`;
let lastStatus = 0;
let lastError = "";
@ -95,3 +81,48 @@ export async function sendMessage(
attempts: 3,
};
}
export async function sendMessage(
token: string,
chatId: string,
text: string,
topicId?: string,
): Promise<SendResult> {
if (!token) {
return { ok: false, error: "TELEGRAM_TOKEN not configured", errorCode: "NO_TOKEN" };
}
const body: Record<string, unknown> = {
chat_id: chatId,
text,
parse_mode: "HTML",
disable_web_page_preview: true,
};
if (topicId) {
body.message_thread_id = Number(topicId);
}
return post(token, "sendMessage", body, chatId);
}
export async function sendPhoto(
token: string,
chatId: string,
photoUrl: string,
caption?: string,
topicId?: string,
): Promise<SendResult> {
if (!token) {
return { ok: false, error: "TELEGRAM_TOKEN not configured", errorCode: "NO_TOKEN" };
}
const body: Record<string, unknown> = {
chat_id: chatId,
photo: photoUrl,
parse_mode: "HTML",
};
if (caption) {
body.caption = caption;
}
if (topicId) {
body.message_thread_id = Number(topicId);
}
return post(token, "sendPhoto", body, chatId);
}