Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using message entities instead of converting to Telegram-specific Markdown #55

Open
monosans opened this issue Jan 9, 2025 · 9 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@monosans
Copy link
Contributor

monosans commented Jan 9, 2025

Instead of converting Markdown to Telegram-specific Markdown, you could remove all Markdown syntax from the original string and send message entities in the Telegram request. This would make the library more robust and simpler, I believe. There will no longer be 400 Bad Requests from Telegram. It will also allow for a more correct maximum message length, since Telegram counts the length after parsing the message entities. Let me know if you need more explanation with examples.

@neutron-nerve neutron-nerve bot added bug Something isn't working enhancement New feature or request labels Jan 9, 2025
@monosans monosans changed the title Consider using message entities instead of converting to Telegram-specific markdown Consider using message entities instead of converting to Telegram-specific Markdown Jan 9, 2025
@sudoskys
Copy link
Owner

sudoskys commented Jan 9, 2025

Can you explain further? I'm not sure which function you are suggesting

If you mean Telegram HTML rendering. It is good! But it will take some time.
The most important thing is to figure out mistletoe support.

@sudoskys
Copy link
Owner

sudoskys commented Jan 9, 2025

Have you ever encountered a 400 error when using the Markdown V2 format?

If so, we should probably maintain an additional HTML semantics

@monosans
Copy link
Contributor Author

monosans commented Jan 9, 2025

If you mean Telegram HTML rendering

No. Try this:

import asyncio

from aiogram import Bot
from aiogram.types import MessageEntity

TOKEN = ""
CHAT_ID = 0

async def main() -> None:
    async with Bot(TOKEN) as bot:
        await bot.send_message(
            CHAT_ID,
            "This text is bold\nThis text is strikethrough",
            entities=[
                MessageEntity(type="bold", offset=0, length=17),
                MessageEntity(type="strikethrough", offset=18, length=26),
            ],
        )


asyncio.run(main())

As you can see, it does all the formatting without any special syntax.

@monosans
Copy link
Contributor Author

monosans commented Jan 9, 2025

In fact, parse mode is just an abstraction on top of message entities

@monosans
Copy link
Contributor Author

monosans commented Jan 9, 2025

Have you ever encountered a 400 error when using the Markdown V2 format?

I've had this 1 time with telegramify, but I can't find the logs anymore unfortunately.

@sudoskys
Copy link
Owner

sudoskys commented Jan 9, 2025

I am thinking whether this is feasible. It stands to reason that after grammar parsing, many block tokens can be matched with message entities.

I'll investigate further when I have time.

@sudoskys
Copy link
Owner

sudoskys commented Feb 9, 2025

Some libraries do not support MarkdownV2 format. Perhaps this issue must be resolved.

@monosans
Copy link
Contributor Author

monosans commented Feb 9, 2025

Some libraries do not support MarkdownV2 format. Perhaps this issue must be resolved.

I started working on my own library, which will use pyromark for Markdown parsing and it will use message entities.

@sudoskys
Copy link
Owner

sudoskys commented Feb 9, 2025

Interesting!, maybe you'll solve the puzzle of the century

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants