Markup submodule support
SKY-ALIN opened this issue · 3 comments
Hello! I want to table a proposal to add a special submodule to generate Telegram-specific markup easily.
Motivation
A common markdown format that every programmer uses is really simple and doesn’t require special utils for that, but MarkdownV2 and HTML which let include spoiler, strikethrough and other telegram innovations are different. For example, MarkdownV2 also requires to escape 18 characters, and if it’s not, reject the request. This may make the task of designing markup generation for bots that include business logic complicated enough and I suppose it’s better to have a ready-made out-of-the-box solution for this.
Implementation suggestion
General explanation
I have recently worked on a related challenge and I’m writing this to propose to re-use my solution. It’s the telegram-text
module. The general structure in the context of the python-telegram
module I propose to implement is the following:
- Add
telegram-text
as a dependency (maybe even optional) - Add support for message objects rendering depending on format mode (Markdown / MarkdownV2 / HTML) inside the message sending function
- Add imports into the
python-telegram
module core to import styles and other needed classes asfrom telegram import ...
. Or it’s quite pretty to add a separated file (text.py
ormarkup.py
for example) to import asfrom telegram.text import ...
Why telegram-text
module?
- Include all rules for all markup modes
- Almost entirely covered by tests
- Fully documented
- Open to contribute
- Has a few presets and is easily expandable to add new
Example of the module:
from telegram_text import Bold, Italic, Underline
text = Underline(Bold("Bold") + "and" + Italic("italic") + "with underline.")
Usage example in the python-telegram
context
from telegram.client import Telegram
from telegram.text import Spoiler
CHAT_ID: str
tg = Telegram(
api_id='api_id',
api_hash='api_hash',
phone='+31611111111',
)
tg.login()
tg.send_message(CHAT_ID, Spoiler("Hello world!"))
tg.stop()
Hi! Thank you for the suggestion, I think it would be great to be able to do something like this. I'll have a look at the module a bit later.
Hi! What have you decided on this issue? If it's possible, I can implement the suggestion above
Hi! Sorry for not getting back earlier and thank you for your proposal and detailed explanation. I think it would be a useful feature. Feel free to make a PR and let me know if there's anything I can do to help you get it ready.
It may be a bit tricky, as in the past tdlib required a list of 'formatted entities' with length, offset, etc. Maybe something changed. If not, I think there is a method parseTextEntities that can parse the text and return the entities for the sendMessage
method.