Python Telegraph API wrapper for creating and managing Telegraph pages and accounts
Telegraph page management including creation, editing, retrieval, and analytics. All page methods support both HTML and node-based content formats.
Create a new Telegraph page with content and metadata.
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:
"""
Create a new Telegraph page.
Parameters:
- title (str): Page title
- content (list, optional): Content in Telegraph nodes format
- html_content (str, optional): Content in HTML format (alternative to content)
- author_name (str, optional): Author name for this page
- author_url (str, optional): Author profile link for this page
- return_content (bool): Whether to include content in response
Returns:
dict: Page information including url, path, title, and optionally content
"""Usage examples:
from telegraph import Telegraph
telegraph = Telegraph(access_token='your_token')
# Create page with HTML content
response = telegraph.create_page(
title='My Article',
html_content='<p>This is <strong>bold</strong> text.</p>',
author_name='John Doe'
)
print(f"Page URL: {response['url']}")
# Create page with node content
nodes = [
{'tag': 'p', 'children': ['Hello, world!']}
]
response = telegraph.create_page(
title='Node Example',
content=nodes
)Edit an existing Telegraph page with new content or metadata.
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:
"""
Edit an existing Telegraph page.
Parameters:
- path (str): Page path (from URL after telegra.ph/)
- title (str): New page title
- content (list, optional): New content in Telegraph nodes format
- html_content (str, optional): New content in HTML format
- author_name (str, optional): New author name
- author_url (str, optional): New author profile link
- return_content (bool): Whether to include content in response
Returns:
dict: Updated page information
"""Usage example:
# Edit existing page
response = telegraph.edit_page(
path='My-Article-12-31',
title='Updated Article',
html_content='<p>Updated content with <em>emphasis</em>.</p>'
)Retrieve a Telegraph page with optional content.
def get_page(path: str, return_content: bool = True, return_html: bool = True) -> dict:
"""
Get a Telegraph page.
Parameters:
- path (str): Page path (from URL after telegra.ph/)
- return_content (bool): Whether to include page content
- return_html (bool): Return content as HTML (True) or nodes (False)
Returns:
dict: Page information including title, author, and optionally content
"""Usage examples:
# Get page with HTML content
page = telegraph.get_page('My-Article-12-31')
print(f"Title: {page['title']}")
print(f"Content: {page['content']}")
# Get page with node content
page = telegraph.get_page('My-Article-12-31', return_html=False)
print(f"Nodes: {page['content']}")
# Get page metadata only
page = telegraph.get_page('My-Article-12-31', return_content=False)
print(f"Views: {page['views']}")Retrieve a list of pages belonging to the account.
def get_page_list(offset: int = 0, limit: int = 50) -> dict:
"""
Get list of pages belonging to Telegraph account.
Parameters:
- offset (int): Sequential number of first page to return (default: 0)
- limit (int): Number of pages to retrieve (0-200, default: 50)
Returns:
dict: Contains 'total_count' and 'pages' list with page information
"""Usage example:
# Get first 10 pages
result = telegraph.get_page_list(limit=10)
print(f"Total pages: {result['total_count']}")
for page in result['pages']:
print(f"- {page['title']} ({page['url']})")
# Get next 10 pages
result = telegraph.get_page_list(offset=10, limit=10)Get view statistics for a Telegraph page with optional time filtering.
def get_views(path: str, year: int = None, month: int = None, day: int = None, hour: int = None) -> dict:
"""
Get view statistics for a Telegraph page.
Parameters:
- path (str): Page path (from URL after telegra.ph/)
- year (int, optional): Filter by year (required if month specified)
- month (int, optional): Filter by month (required if day specified)
- day (int, optional): Filter by day (required if hour specified)
- hour (int, optional): Filter by specific hour
Returns:
dict: View statistics with 'views' count
"""Usage examples:
# Get total views
views = telegraph.get_views('My-Article-12-31')
print(f"Total views: {views['views']}")
# Get views for specific year
views = telegraph.get_views('My-Article-12-31', year=2024)
print(f"2024 views: {views['views']}")
# Get views for specific day
views = telegraph.get_views('My-Article-12-31', year=2024, month=3, day=15)
print(f"March 15, 2024 views: {views['views']}")
# Get views for specific hour
views = telegraph.get_views('My-Article-12-31', year=2024, month=3, day=15, hour=14)
print(f"March 15, 2024 2:00 PM views: {views['views']}")Telegraph supports two content formats:
Standard HTML with restricted tag set. Use html_content parameter.
Allowed tags: a, aside, b, blockquote, br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video
Telegraph's internal JSON format. Use content parameter.
nodes = [
{'tag': 'p', 'children': ['Simple paragraph']},
{'tag': 'p', 'children': [
'Text with ',
{'tag': 'strong', 'children': ['bold']},
' formatting'
]},
{'tag': 'img', 'attrs': {'src': '/file/image.jpg'}}
]Page operations may raise these exceptions:
TelegraphException: API errors, invalid parameters, or authentication failuresRetryAfterError: Rate limiting with retry timingNotAllowedTag: HTML contains disallowed tagsInvalidHTML: Malformed HTML contentfrom telegraph.exceptions import TelegraphException, NotAllowedTag, InvalidHTML
try:
response = telegraph.create_page(
title='Test',
html_content='<script>alert("bad")</script>'
)
except NotAllowedTag as e:
print(f"Disallowed tag: {e}")
except InvalidHTML as e:
print(f"Invalid HTML: {e}")
except TelegraphException as e:
print(f"API error: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-telegraph