import MarkdownIt from "markdown-it"; import type { RouteTarget, Env, NeutralMessage } from "../../types"; import type { PlatformDriver, SendResult } from "../types"; import { sendMessage, editMessageText } from "./rest"; import { renderNeutralMessage } from "./render"; const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false }); function markdownToText(value: string): string { return markdown.renderInline(value).replace(/<[^>]*>/gu, "").replace(/ { const chatId = target.chatId ?? ""; if (!chatId) { return { ok: false, error: "target.chatId is required", errorCode: "NO_TARGET" }; } const token = env.TELEGRAM_TOKEN ?? ""; const text = renderNeutralMessage(message); return sendMessage(token, chatId, text, target.topicId, previewUrl(message, env)); } async edit( message: NeutralMessage, target: RouteTarget, env: Env, messageId: string, ): Promise { const chatId = target.chatId ?? ""; if (!chatId) { return { ok: false, error: "target.chatId is required", errorCode: "NO_TARGET" }; } const token = env.TELEGRAM_TOKEN ?? ""; const text = renderNeutralMessage(message); // A photo sent by an older version has no text to edit; Telegram's error // then makes dispatch send a fresh message instead. return editMessageText( token, chatId, messageId, text, target.topicId, previewUrl(message, env), ); } }