Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-telegram-markdown-parse-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chat-adapter/telegram": patch
---

Set `parse_mode` to `"Markdown"` when posting messages with a `markdown` field, not only for card messages
32 changes: 32 additions & 0 deletions packages/adapter-telegram/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,38 @@ describe("TelegramAdapter", () => {
expect(sendMessageBody.text).toBe("raw id message");
});

it("sets parse_mode for markdown messages", async () => {
mockFetch
.mockResolvedValueOnce(
telegramOk({
id: 999,
is_bot: true,
first_name: "Bot",
username: "mybot",
})
)
.mockResolvedValueOnce(telegramOk(sampleMessage()));

const adapter = createTelegramAdapter({
botToken: "token",
mode: "webhook",
logger: mockLogger,
userName: "mybot",
});

await adapter.initialize(createMockChat());

await adapter.postMessage("telegram:123", {
markdown: "**bold** and _italic_",
});

const sendMessageBody = JSON.parse(
String((mockFetch.mock.calls[1]?.[1] as RequestInit).body)
) as { parse_mode?: string };

expect(sendMessageBody.parse_mode).toBe("Markdown");
});

it("posts cards with inline keyboard buttons", async () => {
mockFetch
.mockResolvedValueOnce(
Expand Down
10 changes: 8 additions & 2 deletions packages/adapter-telegram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ export class TelegramAdapter

const card = extractCard(message);
const replyMarkup = card ? cardToTelegramInlineKeyboard(card) : undefined;
const parseMode = card ? TELEGRAM_MARKDOWN_PARSE_MODE : undefined;
const hasMarkdown =
typeof message === "object" && message !== null && "markdown" in message;
const parseMode =
card || hasMarkdown ? TELEGRAM_MARKDOWN_PARSE_MODE : undefined;
const text = this.truncateMessage(
convertEmojiPlaceholders(
card
Expand Down Expand Up @@ -642,7 +645,10 @@ export class TelegramAdapter

const card = extractCard(message);
const replyMarkup = card ? cardToTelegramInlineKeyboard(card) : undefined;
const parseMode = card ? TELEGRAM_MARKDOWN_PARSE_MODE : undefined;
const hasMarkdown =
typeof message === "object" && message !== null && "markdown" in message;
const parseMode =
card || hasMarkdown ? TELEGRAM_MARKDOWN_PARSE_MODE : undefined;
const text = this.truncateMessage(
convertEmojiPlaceholders(
card
Expand Down
Loading