Skip to content

Instantly share code, notes, and snippets.

@dcdunkan
Last active February 3, 2022 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcdunkan/f3939b429ce783d524544b1abebfb69b to your computer and use it in GitHub Desktop.
Save dcdunkan/f3939b429ce783d524544b1abebfb69b to your computer and use it in GitHub Desktop.
Telegraph API wrapper different parse mode examples
/*
* This is just for demonstrational purposes. A minimal one, haven't used so many
* available tags, markdown syntaxes. See the library and documentation here:
* https://github.com/dcdunkan/telegraph.
*/
import { Telegraph, parse } from "https://deno.land/x/telegraph/mod.ts";
const tph = new Telegraph({
// SANDBOX access token from Official API Docs: https://telegra.ph/api
accessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
});
// Markdown example from https://core.telegram.org/bots/api#markdownv2-style
const tgMarkdownEscaped = parse(
"*bold \*text*" +
"\n_italic \_text_" +
"\n__under\__line__" +
"\n~st\~riketh\~rough~" +
"\n||spoiler||" +
"\n*bold _italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ __underline italic bold___ bold*" +
"\n[inline URL](http://www.example.com/)" +
"\n[inline mention of a user](tg://user?id=123456789)" +
"\n`inline fixed-width code`" +
"\n\n```" +
"\npre-formatted fixed-width code block" +
"\n```" +
"\n\nSeperator" +
"\n\n```python" +
"\npre-formatted fixed-width code block written in the Python programming language" +
"\n```",
"TGMarkdown",
);
const tgMarkdown = parse(
"*bold text*" +
"\n_italic text_" +
"\n__underline__" +
"\n~strikethrough~" +
"\n*bold _italic bold ~italic bold strikethrough~ __underline italic bold___ bold*" +
"\n[inline URL](http://www.example.com/)" +
"\n[inline mention of a user](tg://user?id=123456789)" +
"\n`inline fixed-width code`" +
"\n\n```" +
"\npre-formatted fixed-width code block" +
"\n```" +
"\n\nSeperator" +
"\n\n```python" +
"\npre-formatted fixed-width code block written in the Python programming language" +
"\n```",
"TGMarkdown",
);
const markdown = parse(
"**bold text**" +
"\n_italic text_" +
"\n__underline__" +
"\n~~strikethrough~~" +
"\n**bold _italic bold ~~italic bold strikethrough ~~ __underline italic bold___ bold**" +
"\n[inline URL](http://www.example.com/)" +
"\n`inline fixed-width code`" +
"\n\n```" +
"\npre-formatted fixed-width code block" +
"\n```" +
"\n\nSeperator" +
"\n\n```python" +
"\npre-formatted fixed-width code block written in the Python programming language" +
"\n```",
"Markdown",
);
const html = parse(
"<b>bold text</b>" +
"\n<i>italic text</i>" +
"\n<u>underline</u>" +
"\n<s>strikethrough</s>" +
"\n<b>bold <i>italic bold <s>italic bold strikethrough</s> <u>underline italic bold</u></i> bold</b>" +
'\n<a href="http://www.example.com/">inline URL</a>' +
"\n<code>inline fixed-width code</code>" +
"\n<pre>" +
"\npre-formatted fixed-width code block\nevfefvv\nefwfv" +
"\n</pre>" +
"<p>Seperator</p>" +
'<pre><code class="language-typescript">\nimport { Bot } from "https://deno.land/x/grammy/mod.ts";\nconst bot = new Bot("token");\nbot.start();',
"HTML",
);
const tgMdEscapedPost = await tph.create({
content: tgMarkdownEscaped,
title: "TG Markdown Example (Escaped)",
});
console.log(tgMdEscapedPost);
const tgMdPost = await tph.create({
content: tgMarkdown,
title: "TG Markdown Example",
});
console.log(tgMdPost);
const mdPost = await tph.create({
content: markdown,
title: "Markdown Example",
});
console.log(mdPost);
const htmlPost = await tph.create({
content: html,
title: "HTML Example",
});
console.log(htmlPost);
{
"outputs": {
"TGMarkdownEscaped": {
"path": "TG-Markdown-Example-Escaped-02-02",
"url": "https://telegra.ph/TG-Markdown-Example-Escaped-02-02",
"title": "TG Markdown Example (Escaped)",
"description": "",
"views": 0,
"can_edit": true
},
"TGMarkdown": {
"path": "TG-Markdown-Example-02-02",
"url": "https://telegra.ph/TG-Markdown-Example-02-02",
"title": "TG Markdown Example",
"description": "",
"views": 0,
"can_edit": true
},
"Markdown": {
"path": "Markdown-Example-02-02",
"url": "https://telegra.ph/Markdown-Example-02-02",
"title": "Markdown Example",
"description": "",
"views": 0,
"can_edit": true
},
"HTML": {
"path": "HTML-Example-02-02",
"url": "https://telegra.ph/HTML-Example-02-02",
"title": "HTML Example",
"description": "",
"views": 0,
"can_edit": true
}
}
}

TG Markdown Example (Escaped)

image

TG Markdown Example

image

Markdown Example

image

HTML Example

image

@dcdunkan
Copy link
Author

dcdunkan commented Feb 2, 2022

You can run the file and create your own by running

deno run --allow-net https://gist.githubusercontent.com/dcdunkan/f3939b429ce783d524544b1abebfb69b/raw/example.ts
or,
deno run --allow-net https://0x0.st/oH6b.ts

You need to have Deno installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment