Skip to content

Commit

Permalink
Merge pull request #48 from sudoskys/dev
Browse files Browse the repository at this point in the history
✨ feat: (0.3.0)refactor telegramify to async, add interpreter options
  • Loading branch information
sudoskys authored Dec 15, 2024
2 parents 4fb8b3b + c94000d commit 0d6b517
Show file tree
Hide file tree
Showing 11 changed files with 964 additions and 212 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for LLM responses, GitHub README files, and more.
ensure
its effectiveness.
- We also support Latex Visualization(escape) and Expanded Citation.
- Mermaid Diagrams render supported.
- (telegramify) Mermaid Diagrams render supported.

> [!NOTE]
> If you're interested, there's also a Node.js version of the library
Expand All @@ -41,10 +41,18 @@ 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)**
check:[playground/markdownify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/markdownify_case.py)

- If you are developing an *LLM application* and 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)**
- 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

Expand All @@ -62,7 +70,6 @@ customize.markdown_symbol.head_level_1 = "📌" # If you want, Customizing the
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.
customize.latex_escape = True # If you want to escape LaTeX symbols, set it to True.

# Use `r` to avoid escaping the backslash.
markdown_text = r"""
Expand Down Expand Up @@ -138,11 +145,9 @@ print(converted)
# export Markdown to Telegram MarkdownV2 style.
````

### `telegramify_case`
### `telegramify`

please check: *
*[playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)
**
please check: [playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)

## 🔨 Supported Input

Expand Down
739 changes: 738 additions & 1 deletion pdm.lock

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions playground/inspect_telegramify.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import pathlib
from time import sleep
Expand Down Expand Up @@ -25,16 +26,24 @@
markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
md = pathlib.Path(__file__).parent.joinpath("t_longtext.md").read_text(encoding="utf-8")
boxs = telegramify_markdown.telegramify(md)
for item in boxs:
print("Sent one item")
sleep(0.2)
if item.content_type == ContentTypes.TEXT:
print("TEXT")
print(item.content)
elif item.content_type == ContentTypes.PHOTO:
print("PHOTO")
print(item.caption)
elif item.content_type == ContentTypes.FILE:
print("FILE")
print(item.file_name)


async def main():
boxs = await telegramify_markdown.telegramify(md)
for item in boxs:
print("Sent one item")
sleep(0.2)
if item.content_type == ContentTypes.TEXT:
print("TEXT")
print(item.content)
elif item.content_type == ContentTypes.PHOTO:
print("PHOTO")
print(item.caption)
elif item.content_type == ContentTypes.FILE:
print("FILE")
print(item.file_name)


if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
8 changes: 4 additions & 4 deletions playground/markdownify_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

# Customize the markdownify
telegramify_markdown.customize.strict_markdown = False # we need send underline text
telegramify_markdown.customize.latex_escape = True # we need to escape latex

# Test html tags
html_t = telegramify_markdown.markdownify(
"Hello, World! HTML: <strong>Hello, World!</strong>"
"Hello, World! HTML: <strong>Hello, World!</strong>",
latex_escape=True
)
print(html_t)

Expand Down Expand Up @@ -118,7 +117,8 @@
converted = telegramify_markdown.markdownify(
md,
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
normalize_whitespace=False,
latex_escape=True
)
print(converted)
# export Markdown to Telegram MarkdownV2 style.
Expand Down
101 changes: 63 additions & 38 deletions playground/telegramify_case.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import pathlib
from time import sleep
Expand All @@ -6,8 +7,9 @@
from telebot import TeleBot

import telegramify_markdown
from telegramify_markdown.type import ContentTypes
from telegramify_markdown.customize import markdown_symbol
from telegramify_markdown.interpreters import BaseInterpreter, MermaidInterpreter
from telegramify_markdown.type import ContentTypes

tips = """
telegramify_markdown.telegramify
Expand All @@ -22,43 +24,66 @@
chat_id = os.getenv("TELEGRAM_CHAT_ID", None)
bot = TeleBot(telegram_bot_token)

# Customizing global rendering options
markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
md = pathlib.Path(__file__).parent.joinpath("t_longtext.md").read_text(encoding="utf-8")
boxs = telegramify_markdown.telegramify(md)
for item in boxs:
print("Sent one item")
sleep(0.2)
try:
if item.content_type == ContentTypes.TEXT:
print("TEXT")
bot.send_message(
chat_id,
item.content,
parse_mode="MarkdownV2"
)
elif item.content_type == ContentTypes.PHOTO:
print("PHOTO")
"""
bot.send_sticker(
chat_id,
(item.file_name, item.file_data),
)
"""
bot.send_photo(
chat_id,
(item.file_name, item.file_data),
caption=item.caption,
parse_mode="MarkdownV2"
)
elif item.content_type == ContentTypes.FILE:
print("FILE")
bot.send_document(
chat_id,
(item.file_name, item.file_data),
caption=item.caption,
parse_mode="MarkdownV2"
)
except Exception as e:
print(f"Error: {item}")
raise e


# Write an async function to send message
async def send_message():
boxs = await telegramify_markdown.telegramify(
content=md,
interpreters_use=[BaseInterpreter(), MermaidInterpreter()], # Render mermaid diagram
latex_escape=True,
max_word_count=4090 # The maximum number of words in a single message.
)
for item in boxs:
print("Sent one item")
sleep(0.2)
try:
if item.content_type == ContentTypes.TEXT:
print("TEXT")
bot.send_message(
chat_id,
item.content,
parse_mode="MarkdownV2"
)
elif item.content_type == ContentTypes.PHOTO:
print("PHOTO")
"""
bot.send_sticker(
chat_id,
(item.file_name, item.file_data),
)
"""
bot.send_photo(
chat_id,
(item.file_name, item.file_data),
caption=item.caption,
parse_mode="MarkdownV2"
)
elif item.content_type == ContentTypes.FILE:
print("FILE")
bot.send_document(
chat_id,
(item.file_name, item.file_data),
caption=item.caption,
parse_mode="MarkdownV2"
)
except Exception as e:
print(f"Error: {item}")
raise e


# Sync usage
loop = asyncio.new_event_loop()
result = loop.run_until_complete(
telegramify_markdown.telegramify(md)
)
print(f"Got {len(result)} items.")

# Async usage
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(send_message())
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "telegramify-markdown"
version = "0.2.3"
version = "0.3.0"
description = "Makes it easy to send Markdown in Telegram MarkdownV2 style"
authors = [
{ name = "sudoskys", email = "coldlando@hotmail.com" },
Expand All @@ -10,6 +10,8 @@ dependencies = [
"pytelegrambotapi>=4.22.0",
"docutils>=0.20.1",
"Pillow>=10.4.0",
"pydantic>=2.10.3",
"aiohttp>=3.10.11",
]
requires-python = ">=3.8"
readme = "README.md"
Expand Down
Loading

0 comments on commit 0d6b517

Please sign in to comment.