Python Telegraph API wrapper for creating and managing Telegraph pages and accounts
npx @tessl/cli install tessl/pypi-telegraph@2.2.0A Python wrapper for the Telegraph API that enables developers to programmatically create and manage Telegraph pages and accounts. It provides both synchronous and asynchronous interfaces through a clean, intuitive API that simplifies Telegraph's publishing platform integration.
pip install telegraphpip install 'telegraph[aio]'from telegraph import Telegraph, TelegraphExceptionFor asynchronous operations:
from telegraph.aio import TelegraphFor HTML utilities:
from telegraph.utils import (
html_to_nodes, nodes_to_html, json_dumps, FilesOpener,
ALLOWED_TAGS, VOID_ELEMENTS, BLOCK_ELEMENTS
)For exception handling:
from telegraph.exceptions import (
TelegraphException, RetryAfterError,
ParsingException, NotAllowedTag, InvalidHTML
)from telegraph import Telegraph
# Create a Telegraph client
telegraph = Telegraph()
# Create an account
response = telegraph.create_account(
short_name='my-account',
author_name='My Name',
author_url='https://example.com'
)
# Create a page
response = telegraph.create_page(
title='Hello World',
html_content='<p>Hello, Telegraph!</p>'
)
print(f"Page URL: {response['url']}")import asyncio
from telegraph.aio import Telegraph
async def main():
telegraph = Telegraph()
# Create an account
await telegraph.create_account(short_name='async-account')
# Create a page
response = await telegraph.create_page(
title='Async Hello',
html_content='<p>Hello from async!</p>'
)
print(f"Page URL: {response['url']}")
asyncio.run(main())Telegraph provides two API layers:
Telegraph class with convenient methods for account and page managementTelegraphApi class for direct API calls with custom parametersComprehensive Telegraph account operations including creation, editing, token management, and information retrieval.
def create_account(short_name: str, author_name: str = None, author_url: str = None, replace_token: bool = True) -> dict
def edit_account_info(short_name: str = None, author_name: str = None, author_url: str = None) -> dict
def get_account_info(fields: list = None) -> dict
def revoke_access_token() -> dict
def get_access_token() -> strFull page lifecycle management including creation, editing, retrieval, and view analytics.
def create_page(title: str, content: list = None, html_content: str = None, author_name: str = None, author_url: str = None, return_content: bool = False) -> dict
def edit_page(path: str, title: str, content: list = None, html_content: str = None, author_name: str = None, author_url: str = None, return_content: bool = False) -> dict
def get_page(path: str, return_content: bool = True, return_html: bool = True) -> dict
def get_page_list(offset: int = 0, limit: int = 50) -> dict
def get_views(path: str, year: int = None, month: int = None, day: int = None, hour: int = None) -> dictFile upload functionality for images and media content (unofficial API).
def upload_file(f) -> listComplete async/await interface mirroring all synchronous functionality.
async def create_account(short_name: str, author_name: str = None, author_url: str = None, replace_token: bool = True) -> dict
async def create_page(title: str, content: list = None, html_content: str = None, author_name: str = None, author_url: str = None, return_content: bool = False) -> dict
# ... all other methods with async signaturesHTML parsing and Telegraph node format conversion utilities.
def html_to_nodes(html_content: str) -> list
def nodes_to_html(nodes: list) -> strDirect Telegraph API access for advanced use cases and custom implementations.
class TelegraphApi:
def __init__(access_token: str = None, domain: str = 'telegra.ph')
def method(method: str, values: dict = None, path: str = '') -> dict
def upload_file(f) -> listclass TelegraphException(Exception):
"""Base exception for Telegraph API errors"""
pass
class RetryAfterError(TelegraphException):
"""Raised when rate limiting occurs"""
def __init__(self, retry_after: int):
self.retry_after = retry_after
class ParsingException(Exception):
"""Base exception for HTML parsing errors"""
pass
class NotAllowedTag(ParsingException):
"""Raised when HTML contains disallowed tags"""
pass
class InvalidHTML(ParsingException):
"""Raised when HTML is malformed"""
pass