Skip to content

Commit

Permalink
feat: Bot API 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Jan 6, 2024
1 parent b74e2cf commit e1e7971
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
21 changes: 21 additions & 0 deletions source/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,34 @@ test("underline", () => {
strictEqual(format.underline("underline"), "<u>underline</u>");
});

test("blockquote", () => {
strictEqual(
format.blockquote("blockquote"),
"<blockquote>blockquote</blockquote>",
);
});

test("blockquote multiline", () => {
strictEqual(
format.blockquote("blockquote\nmultiline"),
"<blockquote>blockquote\nmultiline</blockquote>",
);
});

test("spoiler", () => {
strictEqual(
format.spoiler("spoiler"),
'<span class="tg-spoiler">spoiler</span>',
);
});

test("tgEmoji", () => {
strictEqual(
format.tgEmoji("👍", "5368324170671202286"),
'<tg-emoji emoji-id="5368324170671202286">👍</tg-emoji>',
);
});

test("url", () => {
strictEqual(
format.url("me", "https://edjopato.de"),
Expand Down
14 changes: 14 additions & 0 deletions source/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,25 @@ export function underline(text: string): string {
return `<u>${text}</u>`;
}

/** Format the input text as blockquote */
export function blockquote(text: string): string {
return `<blockquote>${text}</blockquote>`;
}

/** Format the input text as spoiler */
export function spoiler(text: string): string {
return `<span class="tg-spoiler">${text}</span>`;
}

/** Create a custom Telegram Emoji.
*
* A valid emoji must be used as the fallback. The emoji will be shown instead of the custom emoji in places where a custom emoji cannot be displayed (e.g., system notifications) or if the message is forwarded by a non-premium user. It is recommended to use the emoji from the emoji field of the custom emoji sticker.
* Custom emoji entities can only be used by bots that purchased additional usernames on Fragment.
*/
export function tgEmoji(fallback: string, emojiId: string): string {
return `<tg-emoji emoji-id="${emojiId}">${fallback}</tg-emoji>`;
}

/** Format the input text as monospace */
export function monospace(text: string): string {
return `<code>${escape(text)}</code>`;
Expand Down
8 changes: 8 additions & 0 deletions source/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ test("underline", () => {
throws(() => format.underline("1337"));
});

test("blockquote", () => {
throws(() => format.blockquote("1337"));
});

test("spoiler", () => {
throws(() => format.spoiler("1337"));
});

test("tgEmoji", () => {
throws(() => format.tgEmoji("👍", "5368324170671202286"));
});
14 changes: 14 additions & 0 deletions source/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,27 @@ export function underline(_text: string): string {
);
}

/** unsupported by Telegram. Use MarkdownV2 or HTML instead @deprecated */
export function blockquote(_text: string): string {
throw new Error(
"blockquote is not supported by Markdown. Use MarkdownV2 instead.",
);
}

/** unsupported by Telegram. Use MarkdownV2 or HTML instead @deprecated */
export function spoiler(_text: string): string {
throw new Error(
"spoiler is not supported by Markdown. Use MarkdownV2 instead.",
);
}

/** unsupported by Telegram. Use MarkdownV2 or HTML instead @deprecated */
export function tgEmoji(_fallback: string, _emojiId: string): string {
throw new Error(
"tgEmoji is not supported by Markdown. Use MarkdownV2 instead.",
);
}

/** Format the input text as monospace */
export function monospace(text: string): string {
return "`" + text.replace(/`/g, "") + "`";
Expand Down
18 changes: 18 additions & 0 deletions source/markdownv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@ test("underline", () => {
strictEqual(format.underline("underline"), "__underline__");
});

test("blockquote", () => {
strictEqual(format.blockquote("blockquote"), ">blockquote");
});

test("blockquote multiline", () => {
strictEqual(
format.blockquote("blockquote\nmultiline"),
">blockquote\n>multiline",
);
});

test("spoiler", () => {
strictEqual(format.spoiler("spoiler"), "||spoiler||");
});

test("tgEmoji", () => {
strictEqual(
format.tgEmoji("👍", "5368324170671202286"),
"![👍](tg://emoji?id=5368324170671202286)",
);
});

test("url", () => {
strictEqual(
format.url("me", "https://edjopato.de"),
Expand Down
15 changes: 15 additions & 0 deletions source/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@ export function underline(text: string): string {
return `__${text}__`;
}

/** Format the input text as blockquote */
export function blockquote(text: string): string {
const lines = text.split("\n");
return lines.map((o) => ">" + o).join("\n");
}

/** Format the input text as spoiler */
export function spoiler(text: string): string {
return `||${text}||`;
}

/** Create a custom Telegram Emoji.
*
* A valid emoji must be used as the fallback. The emoji will be shown instead of the custom emoji in places where a custom emoji cannot be displayed (e.g., system notifications) or if the message is forwarded by a non-premium user. It is recommended to use the emoji from the emoji field of the custom emoji sticker.
* Custom emoji entities can only be used by bots that purchased additional usernames on Fragment.
*/
export function tgEmoji(fallback: string, emojiId: string): string {
return `![${fallback}](tg://emoji?id=${emojiId})`;
}

/** Format the input text as monospace */
export function monospace(text: string): string {
return "`" + escapeInternal(text, "`") + "`";
Expand Down
4 changes: 4 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
export interface Formatter {
/** parse_mode which can be used on sendMessage */
parse_mode: "HTML" | "Markdown" | "MarkdownV2";
/** Format the input text as blockquote */
blockquote: (text: string) => string;
/** Format the input text bold */
bold: (text: string) => string;
/** Escape the input text to be displayed as it is */
Expand All @@ -16,6 +18,8 @@ export interface Formatter {
spoiler: (text: string) => string;
/** Strikethrough the input text */
strikethrough: (text: string) => string;
/** Create a custom Telegram Emoji */
tgEmoji: (fallback: string, emojiId: string) => string;
/** Underline the input text */
underline: (text: string) => string;
/** Create an url with a label text */
Expand Down

0 comments on commit e1e7971

Please sign in to comment.