Telegra.ph API wrapper for NodeJS
Forked from Deno version
This is a tiny Telegra.ph API wrapper for NodeJS written in
TypeScript. All methods as listed
here are available. See
Usage for usage examples for each methods.
See the Npm module and the Deno module.
Table of Contents
What is Telegra.ph?
Telegra.ph is a minimalist publishing tool that allows you to create richly
formatted posts and push them to the Web in just a click. Telegraph posts also
get beautiful Instant View pages on
Telegram.
It is recommended to read the official Telegra.ph API
docs @https://telegra.ph/api
This module contains all methods in the official Telegra.ph API and a unofficial
Telegra.ph file upload module (Only .jpg
, .jpeg
, .png
, .gif
and .mp4
files). See example usage.
Usage
Install the module
npm install better-telegraph
or
yarn add better-telegraph
Import to your project file and connect or create a Telegraph account:
import { Telegraph } from "better-telegraph";
const tphOldAccount = new Telegraph({
access_token: "<your account access token>",
});
const tphNew = new Telegraph({
short_name: "Deno",
author_name: "Deno.land",
author_url: "https://deno.land/",
});
const tphUpdatedOld = new Telegraph({
access_token: "<the account access token>",
short_name: "Ryan Dahl",
author_url: "https://github.com/ry",
});
Now you can use any methods. See the examples for each methods:
Creates a new Telegraph account, and returns the newly created account's details
including auth_url
and access_token
.
Read more: https://telegra.ph/api#createAccount
Usage:
tph.createAccount({
short_name: "Sandbox",
author_name: "Anonymous",
author_url: "",
});
Outputs:
{
"short_name": "Sandbox",
"author_name": "Anonymous",
"author_url": "",
"access_token": "xxxxxxxx",
"auth_url": "https://edit.telegra.ph/auth/xxxxxxx"
}
Edits the details of the connected Telegraph account, and returns the edited
account details. Does not includes access_token
or auth_url
. Requires
atleast one of the parameters.
Read more: https://telegra.ph/api#editAccountInfo
Usage:
tph.editAccount({
short_name: "BoxSand",
author_name: "Nonymous",
author_url: "https://minecraft.net",
});
Outputs:
{
"short_name": "BoxSand",
"author_name": "Nonymous",
"author_url": "https://minecraft.net/"
}
Revokes access_token
of the connected Telegraph account, and returns the new
access_token
and auth_url
.
Read more: https://telegra.ph/api#revokeAccessToken
Takes in one optional parameter,
save
- Type:
boolean
. - Defaults to
true
. - If
true
, the new access_token
will be set to the connected account.
Usage:
tph.revokeAccessToken();
tph.revokeAccessToken(false);
Outputs:
{
"access_token": "xxxxxxxx",
"auth_url": "https://edit.telegra.ph/auth/xxxxxxx"
}
Returns the account details.
Read more: https://telegra.ph/api#getAccountInfo
Parameter:
fields
: Fields to return.
- Type:
("short_name" | "author_name" | "author_url" | "auth_url" | "page_count")[]
. - Defaults to
[ "short_name", "author_name", "author_url", "auth_url", "page_count" ]
.
Usage:
tph.getAccount();
tph.getAccount(["author_url"]);
tph.getAccount(["author_name", "author_url"]);
tph.getAccount(["page_count"]);
Outputs (Or only specified fields):
{
"short_name": "BoxSand",
"author_name": "Nonymous",
"author_url": "https://minecraft.net/"
}
Creates a new Telegraph Page (Post), and returns the details of the created
page.
Read more: https://telegra.ph/api#createPage
In parameter options
, the property content
can be a string
or string[]
or HTML or Markdown or an array of Node
.
See Content Formatting for more.
Example for content:
"Telegraph is cool!"[("Foo", "Bar")];
Passing Markdown
as content:
const content = parseMarkdown("## Jurassic Deno\n\n" + `![image](${await Telegraph.upload("jurassicDeno.jpg")})`)!;
Or the same thing by using HTML
as content:
const content = parseHtml("<h3>Jurassic Deno</h3><br>" + `<img src="${await Telegraph.upload("jurassicDeno.jpg")}">`)!;
Passing Node
as content:
[
{
tag: "a",
attrs: {
href: "https://github.com",
},
children: ["GitHub"],
},
{
tag: "br",
},
{
tag: "p",
children: ["GitHub is where over 73 million developers shape the future of software, together."],
},
{
tag: "img",
attrs: {
src: "https://github.githubassets.com/images/modules/site/social-cards/github-social.png",
},
},
{
tag: "pre",
children: [
`const tph = new Telegraph({
accessToken: ""
});
tph.getAccount();`,
],
},
];
Usage:
tph.create({
title: "Telegraph is cool!",
content: "Telegraph is cool!",
author_name: "Telegram",
author_url: "https://telegram.org",
return_content: true,
});
Outputs:
{
"path": "Telegraph-is-cool-12-24",
"url": "https://telegra.ph/Telegraph-is-cool-12-24",
"title": "Telegraph is cool!",
"description": "",
"author_name": "Telegram",
"author_url": "https://telegram.org/",
"content": ["Telegraph is cool!"],
"views": 0,
"can_edit": true
}
Edits a Telegraph page (post), and on success, returns the edited page details.
See Content Formatting to know more about the values can
be given to content
parameter.
Read more: https://telegra.ph/api#editPage
Usage:
tph.edit("Telegraph-is-cool-12-24", {
content: "Telegraph is simple!",
title: "Telegraph is simple, but cool!",
author_name: "Telegram Team",
author_url: "https://telegram.org/",
return_content: false,
});
Outputs:
{
"path": "Telegraph-is-cool-12-24",
"url": "https://telegra.ph/Telegraph-is-cool-12-24",
"title": "Telegraph is simple, but cool!",
"description": "",
"author_name": "Telegram Team",
"author_url": "https://telegram.org/",
"views": 0,
"can_edit": true
}
Page > get
Returns the details of a Telegraph page (post).
Read more: https://telegra.ph/api#getPage
Parameters
path
(string
): Path of the page.returnContent
(boolean
): Optional. If true
, the page content will also
be returned. Defaults to false
.
Usage:
tph.get("Telegraph-is-cool-12-24");
Outputs:
{
"path": "Telegraph-is-cool-12-24",
"url": "https://telegra.ph/Telegraph-is-cool-12-24",
"title": "Telegraph is simple, but cool!",
"description": "",
"author_name": "Telegram Team",
"author_url": "https://telegram.org/",
"views": 0
}
Returns a list of pages belonging to the connected Telegraph account.
Read more: https://telegra.ph/api#getPageList
Usage:
tph.getPages();
tph.getPages({
offset: 1,
limit: 2,
});
Outputs:
{
"total_count": 14,
"pages": [
{
"path": "GitHub-12-24-7",
"url": "https://telegra.ph/GitHub-12-24-7",
"title": "GitHub",
"description": "GitHub is where over 73 million developers shape the future of software, together.\nconst tph = new T...",
"author_name": "GitHub",
"author_url": "https://github.com/",
"views": 0,
"can_edit": true
},
{
"path": "Telegraph-is-cool-12-24",
"url": "https://telegra.ph/Telegraph-is-cool-12-24",
"title": "Telegraph is simple, but cool!",
"description": "",
"author_name": "Telegram Team",
"author_url": "https://telegram.org/",
"views": 0,
"can_edit": true
}
]
}
Returns the number of views of the specified page. You can pass in some
optional date options to get the views of that time.
Read more: https://telegra.ph/api#getViews
Usage:
tph.getPageViews("Telegraph-is-cool-12-24");
tph.getPageViews({
year: 2021,
});
tph.getPageViews({
hour: 15,
day: 24,
month: 12,
year: 2021,
});
Outputs:
{ "views": 0 }
Upload
This is not a part of the official Telegra.ph API.
This function helps you to upload .jpg
, .jpeg
, .png
, .gif
and .mp4
files upto ~6MB (I think so) to Telegra.ph.
import { parseMarkdown, Telegraph } from "better-telegraph";
const localFile = await Telegraph.upload("video.mp4");
const url = await Telegraph.upload("https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_640_3MG.mp4");
const tph = new Telegraph({
access_token: "<your access token>",
});
const content = parseMarkdown("Jurassic Deno\n\n" + `![image](${await Telegraph.upload("jurassicDeno.jpg")})`)!;
await tph
.create({
title: "Jurassic Deno",
content,
})
.then((page) => console.log(page.url));
await tph.create({
title: "Jurassic Deno",
content: [
"Jurassic Deno:",
{
tag: "img",
attrs: {
src: await Telegraph.upload("jurassicDeno.jpg"),
},
},
],
});
await tph.create({
title: "Deno.land",
content: [
{
tag: "img",
attrs: {
src: await Telegraph.upload("jurassicDeno.jpg"),
},
},
{
tag: "img",
attrs: {
src: await Telegraph.upload("https://deno.land/images/deno_logo.png"),
},
},
{
tag: "img",
attrs: {
src: await Telegraph.upload("https://github.com/denolib/animated-deno-logo/raw/master/deno-rect-24fps.gif"),
},
},
"Example video:",
{
tag: "video",
attrs: {
src: await Telegraph.upload("https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_640_3MG.mp4"),
},
},
],
});
Content Formatting
Content can be Markdown, HTML, or just String or Array of
strings or Node or an Array of both strings
and Nodes. To use HTML and Markdown you need to import two parser functions from
the module.
import { parseHtml, parseMarkdown } from "better-telegraph";
Here are basic examples for each type. See the README of the official Repository
to find more of them.
const content = "With just a string";
const content = ["Array of strings.", " I am number one."];
HTML, parseHtml(htmlContent)
will convert the HTML string to Node.
import { parseHtml } from "better-telegraph";
const content = parseHtml(`<h1>Pure HTML, boys</h1><br>
<p><b>Be bold</b></p>`);
Markdown, parseMarkdown(mdContent)
will parse the Markdown down to Node
for the content.
import { parseMarkdown } from "better-telegraph";
const content = parseMarkdown(`## Heading 2\n\nThis is so **cool**!`);
Node, a bit complicated one to create (That's why MD and HTML methods
exists), if there is a lot of formatting and all. And you have to do this in
const content = [
{
tag: "a",
attrs: {
href: "https://github.com",
},
children: ["GitHub"],
},
{ tag: "br" },
{
tag: "p",
children: ["GitHub is where over 73 million developers shape the future of software, together."],
},
{
tag: "img",
attrs: {
src: "https://github.githubassets.com/images/modules/site/social-cards/github-social.png",
},
},
];
If you are having issues with this library, or if you like to suggest something
that can make this library better, please open a
issue here. Or if you'd like to
contribute, please open a pull request.
MIT License. Copyright (c) 2022
dcdunkan.