Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sudoskys/telegramify-markdown
🪄 Markdown To Telegram MarkdownV2 Converter Python| No more worrying about formatting.
https://github.com/sudoskys/telegramify-markdown
formatter markdown telegram-bot telegram-markdown-v2
Last synced: 2 days ago
JSON representation
🪄 Markdown To Telegram MarkdownV2 Converter Python| No more worrying about formatting.
- Host: GitHub
- URL: https://github.com/sudoskys/telegramify-markdown
- Owner: sudoskys
- License: mit
- Created: 2024-03-13T02:33:24.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-12-17T10:04:43.000Z (25 days ago)
- Last Synced: 2025-01-01T19:08:32.421Z (9 days ago)
- Topics: formatter, markdown, telegram-bot, telegram-markdown-v2
- Language: Python
- Homepage:
- Size: 1.4 MB
- Stars: 80
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# telegramify-markdown
![GitHub Repo stars](https://img.shields.io/github/stars/sudoskys/telegramify-markdown?style=social)
[![PyPI version](https://badge.fury.io/py/telegramify-markdown.svg)](https://badge.fury.io/py/telegramify-markdown)
[![Downloads](https://pepy.tech/badge/telegramify-markdown)](https://pepy.tech/project/telegramify-markdown)**Effortlessly Convert Raw Markdown to Telegram's [MarkdownV2](https://core.telegram.org/bots/api#markdownv2-style)
Style**Say goodbye to formatting issues! You no longer need to write parsers to convert raw Markdown text into Telegram's
MarkdownV2 format.This library is designed to make reading and sending Markdown content on Telegram easier and more efficient. It is ideal
for LLM responses, GitHub README files, and more.- No matter the format or length, it can be easily handled!
- Forget about formatting errors from the API. Our custom renderer has been tested in a real server environment to
ensure
its effectiveness.
- We also support Latex Visualization(escape) and Expanded Citation.
- (telegramify) Mermaid Diagrams render supported.> [!NOTE]
> If you're interested, there's also a Node.js version of the library
> available: [npm:telegramify-markdown](https://www.npmjs.com/package/telegramify-markdown).## 🪄 Quick Start
To install the library, run:
```bash
pip install telegramify-markdown
```or, if you use `pdm`:
```shell
pdm add telegramify-markdown
```### 🤔 What you want to do?
- If you just want to send *static text* and don't want to worry about formatting,
check:[playground/markdownify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/markdownify_case.py)- If you are developing an *LLM application* or need to send potentially **super-long text**, please
check:[playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)We have two main functions: `markdownify` and `telegramify`.
`markdownify`: Just converts raw Markdown text to Telegram's MarkdownV2 format.
`telegramify`: Spilt long text into multiple chunks, convert format and use Interpreter to render code block to File, Image etc.
>`Interpreter` can be easily customized to inspect the rendering process in `telegramify`.
## 👀 Use case
| markdownify_case 1 | markdownify_case 2 | telegramify_case |
|---------------------------------|---------------------------------|---------------------------------|
| ![result](.github/result-7.png) | ![result](.github/result-8.png) | ![result](.github/result-9.png) |### `markdownify`
````python3
import telegramify_markdown
from telegramify_markdown import customizecustomize.markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
customize.markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
customize.strict_markdown = True # If you want to use __underline__ as underline, set it to False, or it will be converted to bold as telegram does.
customize.cite_expandable = True # If you want to enable expandable citation, set it to True.# Use `r` to avoid escaping the backslash.
markdown_text = r"""
# Title
## Subtitle
### Subsubtitle
#### Subsubsubtitle\(TEST
\\(TEST
\\\(TEST
\\\\(TEST
\\\\\(TEST**Latex Math**
Function Change:
\(\Delta y = f(x_2) - f(x_1)\) can represent the change in the value of a function.
Average Rate of Change:
\(\frac{\Delta y}{\Delta x} = \frac{f(x_2) - f(x_1)}{x_2 - x_1}\) is used to denote the average rate of change of a function over the interval \([x_1, x_2]\).
- Slope:
\[
F = G\frac{{m_1m_2}}{{r^2}}
\]
- Inline: \(F = G\frac{{m_1m_2}}{{r^4}}\)There \frac{1}{2} not in the latex block.
**Table**
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |'\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'
_ , * , [ , ] , ( , ) , ~ , ` , > , # , + , - , = , | , { , } , . , !
We will remove the \ symbol from the original text.
**bold text**
*bold text*
_italic text_
__underline__
~no valid strikethrough~
~~strikethrough~~
||spoiler||
*bold _italic bold ~~italic bold strikethrough ||italic bold strikethrough spoiler||~~ __underline italic bold___ bold*
__underline italic bold__
[link](https://www.google.com)
- [ ] Uncompleted task list item
- [x] Completed task list item
> Quote>Multiline Quote In Markdown it's not possible to send multiline quote in telegram without using code block or html tag but telegramify_markdown can do it.
---
TextText
Text
> If you quote is too long, it will be automatically set in expandable citation.
> This is the second line of the quote.
> `This is the third line of the quote.`
> This is the fourth line of the quote.
> `This is the fifth line of the quote.`"""
converted = telegramify_markdown.markdownify(
markdown_text,
max_line_length=None, # If you want to change the max line length for links, images, set it to the desired value.
normalize_whitespace=False
)
print(converted)
# export Markdown to Telegram MarkdownV2 style.
````### `telegramify`
please check: [playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)
## 🔨 Supported Input
- [x] Headings (Levels 1-6)
- [x] `Links [text](url)`
- [x] `Images ![alt](url)`
- [x] Lists (Ordered and Unordered)
- [x] Tables `|-|-|`
- [x] Horizontal Rules `----`
- [x] Text Styles `*Italic*` and `**Bold**`
- [x] Underline `__Underline__` (if `customize.strict_markdown` is False)
- [x] Code Blocks
- [x] Inline Code
- [x] Block Quotes `>`
- [x] Strikethrough `~~Strikethrough~~`
- [x] Spoilers `||Spoiler||`
- [x] Task Lists
- [x] Expanded Citation
- [x] Telegram Emojis
- [ ] Telegram User Mentions
- [ ] Strikethrough `~Strikethrough~`> [!NOTE]
> Despite `~Strikethrough~` being mentioned in Telegram's official documentation, it can't be parsed as strikethrough.## 🔠Proper Usage
```python
import textwrapfrom telebot import TeleBot
import telegramify_markdown
import telegramify_markdown.customize as customizecustomize.strict_markdown = False
value1 = 52222
markdown_text = textwrap.dedent(
f"""
# Title
## Subtitle
value1: {value1}
||spoiler||
"""
)
can_be_sent = telegramify_markdown.markdownify(markdown_text)
TeleBot("TOKEN").send_message(
"CHAT_ID",
can_be_sent,
parse_mode="MarkdownV2"
)
```## 🧸 Acknowledgement
This library is inspired by [npm:telegramify-markdown](https://www.npmjs.com/package/telegramify-markdown).
latex escape is inspired by [latex2unicode](https://github.com/tomtung/latex2unicode) and @yym68686.
## 📜 License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.