Skip to content

Conversation

@pplulee
Copy link
Contributor

@pplulee pplulee commented Apr 5, 2025

Description

Add support for processing code language parameter in <pre> tag under message entities

Describe your tests

How did you test your change?

Python version: 3.12

OS: Windows 11

Send a message containing a code block to the bot, then see the result of the parsed HTML code.

image
image

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 5, 2025

Looks fine. Did you perform tests of this update?

@pplulee
Copy link
Contributor Author

pplulee commented Apr 6, 2025

I was unable to run some of the tests, but that seems to be the original problem, not caused by these updates.

For example, in test_telebot.py, I'm confused as to why empty data was used. This caused the tests to fail.
image
image

@pplulee
Copy link
Contributor Author

pplulee commented Apr 6, 2025

I updated the test file with some optimisations. For the issues mentioned above, environment variables are used directly. All tests now pass.

Also, I added tests for the apply_html_entities function.

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 6, 2025

For example, in test_telebot.py, I'm confused as to why empty data was used.

You need to pass YOUR bot token there.

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 6, 2025

For the issues mentioned above, environment variables are used directly.

Not absolutely correct due to TOKEN var may not exist if nothing in environment, but better then ''...

@pplulee
Copy link
Contributor Author

pplulee commented Apr 6, 2025

For example, in test_telebot.py, I'm confused as to why empty data was used.

You need to pass YOUR bot token there.

I saw environment variables for token, but some tests used them and some didn't, so I was confused. I updated to use the same environment variables for all tests so we don't have to manually edit the test files.

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 6, 2025

func is designed wrong.

    def func(upd_text, subst_type=None, url=None, user=None, custom_emoji_id=None, language=None):
        upd_text = upd_text.decode("utf-16-le")
        if subst_type == "text_mention":
            subst_type = "text_link"
            url = "tg://user?id={0}".format(user.id)
        elif subst_type == "mention":
            url = "https://t.me/{0}".format(upd_text[1:])
        upd_text = upd_text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
        if not subst_type or not _subs.get(subst_type):
            return upd_text
        subs = _subs.get(subst_type)
        if subst_type == "custom_emoji":
            return subs.format(text=upd_text, custom_emoji_id=custom_emoji_id)
        elif (subst_type == "pre") and language:
                return upd_text='<code class="language-{0}">{1}</code>'.format(language, upd_text)
        return subs.format(text=upd_text, url=url)

Otherwise symbols like < and > will break the markdown.

@pplulee
Copy link
Contributor Author

pplulee commented Apr 6, 2025

func is designed wrong.

    def func(upd_text, subst_type=None, url=None, user=None, custom_emoji_id=None, language=None):
        upd_text = upd_text.decode("utf-16-le")
        if subst_type == "text_mention":
            subst_type = "text_link"
            url = "tg://user?id={0}".format(user.id)
        elif subst_type == "mention":
            url = "https://t.me/{0}".format(upd_text[1:])
        upd_text = upd_text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
        if not subst_type or not _subs.get(subst_type):
            return upd_text
        subs = _subs.get(subst_type)
        if subst_type == "custom_emoji":
            return subs.format(text=upd_text, custom_emoji_id=custom_emoji_id)
        elif (subst_type == "pre") and language:
                return upd_text='<code class="language-{0}">{1}</code>'.format(language, upd_text)
        return subs.format(text=upd_text, url=url)

Otherwise symbols like < and > will break the markdown.

Thanks for pointing out the problem. I've modified the code along the lines of your idea and added a test for special characters.

@Badiboy Badiboy merged commit 7c3824e into eternnoir:master Apr 6, 2025
7 checks passed
@Badiboy
Copy link
Collaborator

Badiboy commented Apr 6, 2025

Thank you!

@Badiboy Badiboy requested a review from Copilot April 6, 2025 15:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

tests/test_telebot.py:47

  • Ensure that TOKEN is defined and imported in this test file to prevent an undefined variable error.
tb = telebot.TeleBot(TOKEN)

@Badiboy
Copy link
Collaborator

Badiboy commented Apr 6, 2025

Copilot reviewed

@pplulee It was just for test )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants