Telegramify Markdown
telegramify-markdown is a Python library designed to convert standard Markdown text into a format compatible with Telegram's MessageEntity system. It processes Markdown syntax and outputs a plain text string along with a list of `MessageEntity` objects, suitable for sending via the Telegram Bot API. The current version is 1.1.2, and it maintains a moderate release cadence based on feature additions and bug fixes.
Common errors
-
TypeError: 'tuple' object is not callable
cause Attempting to call the result of `telegramify_markdown` as if it were a function, or misinterpreting its return type.fixRemember `telegramify_markdown` returns a tuple `(text, entities)`. You need to destructure it before using the components. Example: `text, entities = telegramify_markdown(your_md_string)`. -
ModuleNotFoundError: No module named 'telegramify_markdown'
cause The package is not installed in the current Python environment, or the import statement is incorrect.fixInstall the package using `pip install telegramify-markdown`. Verify your import statement: `from telegramify_markdown import telegramify_markdown`. -
SyntaxError: 'annotations' must be a from __future__ import, not a statement
cause Running the library on a Python version older than 3.10, which does not support the required type hint syntax.fixUpgrade your Python interpreter to version 3.10 or newer. This library explicitly states `requires_python='>=3.10'`.
Warnings
- gotcha The `telegramify_markdown` function returns a tuple: `(plain_text_string, list_of_message_entities)`. Many users mistakenly expect only a string.
- gotcha This library aims for compatibility with Telegram's MarkdownV2, which is not identical to CommonMark or GitHub Flavored Markdown. Certain constructs (e.g., `_` for italic within words, complex nested formatting) may not render as expected or may require careful input escaping.
- breaking The library explicitly requires Python 3.10 or newer. Installing or running on older Python versions will lead to `SyntaxError` or `ModuleNotFoundError` due to modern language features used.
Install
-
pip install telegramify-markdown
Imports
- telegramify_markdown
from telegramify_markdown import telegramify_markdown
Quickstart
from telegramify_markdown import telegramify_markdown
markdown_text = "Hello, *world*! This is some _italic_ text and `inline code`."
plain_text, entities = telegramify_markdown(markdown_text)
print("Plain Text:", plain_text)
print("Entities:", entities)
# Example of how you might use it with a Telegram bot library (concept)
# bot.send_message(chat_id=12345, text=plain_text, entities=entities)