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

HTML tag in user's profile name makes problem in parse html #4257

Closed
RanaUniverse opened this issue May 14, 2024 · 3 comments
Closed

HTML tag in user's profile name makes problem in parse html #4257

RanaUniverse opened this issue May 14, 2024 · 3 comments

Comments

@RanaUniverse
Copy link

RanaUniverse commented May 14, 2024

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

Hello Admins Here, Please don't scold me much much, please Listen to me with ♥️💖💝

See My Issue which is related to HTML tags in the Telegram Profile User Name, this issue comes when the user name contains invalid character like < >,

this makes the normal PTB beheaviour to throw a telegram.BadRequest Error,
see the image here
image

You can see the code example in this PTB Example code where i have write the full ready to use main.py code, so that you can fully run this main.py just by replacing the Bot Token for testing purpose.
Link of the main.py code is: https://github.com/RanaUniverse/ptb_02_html/blob/main/main.py

As i understand, i found when a user name contains < or > or both the problem occurs in the line 56 in the main.py, which got replaced user.full_name = name which contains < or >
as a result the parse_mode = "HTML" not support the < with different character,

see as a example i checked with this bot testing, and as my code says when i will send /start to bot, it will send my my name in bold + underline formatting as a reply as you can see the image below that the response shows here after i rename my account several time without any < or > , and the bot response to me without any error in the terminal.

image
image

as you can see when my name has not < > the response was with bold and underline and at the moment i add < > in my profile name then bot stops response me and it cause shows the error in the temrinal as i mentioned above in the first image.

So here what i means to say is, in most case we use

user = update.message.from_user

then to get the user full name we use user.full_name and then when we want some design in the reply text we directly use this inside text parameter and pass parsemode = "HTML",
so most of the cases user's name contains no < & > so it does not throw any error, so it looks like our bold+underline design will work completely fines for all cases.

But as the example i shows you in this upper i described that if a user name contains < > it will show error, and my code flow of execution will breake, so to solve this problem we need to use
import html
html.escape(user.full_name)

But writing this each time in all the other places where i used the users name in the text i need to import and use this html library in all the places,
so what i means to say that we need a new property which will use the html.escape in background and return the full name without < & >,

Thanks For teach me this how to use the escape to use proper functionality as i expected,

Describe the solution you'd like

So as i think to add a new property in the user class in this section , like this:::

C:\Users\RanaUniverse\AppData\Local\Programs\Python\Python312\Lib\site-packages\telegram_user.py

in this module of the PTB library if we will make a new class property like this, so we can directly call this function using the user class object and got no error,

@property
def full_name_html(self) -> str:
    """
    this will use full name as a html escaped so that if any < > will present in the
    user name it will use it as normal string
    """
    real_name = self.full_name
    escaped_name = html.escape(real_name)
    return escaped_name

i checked with change it in the source code in my vs code directly and use it and got proper result
image
image

and below image you can see after i add a new property and then use it in my start funciton you see the below image which shows correct response to user even if the user name contains the unsupported tags

image

so what i means to say that if you will add this new property in this PTB, it can help us a lot by callig this new property all time, without we need to take care of user name contains any invalid character or not,
please look into the matter,

Describe alternatives you've considered

I want You will see into the matter, and having a new property for this, will help us in future, without making any problem to our existing PTB code

Additional context

Please dont say to use me

html.escape(user.full_name)

in all the lines rather it will easy if we use directly
user.full_name_html
like this or any other related property name

@RanaUniverse
Copy link
Author

as i got to know that i should use a new code which will enlarged the fucntionality of the User class,

https://t.me/pythontelegrambottalk/254801,
i have wrote this with details, but still it looks like , if you will not insert this in static methods in the PTB, user can face problem for those whose name contains < > , so i say to do this,
or do you guys suggest to use the on the fly generate new property, what will you suggest, pls say me by helping how i can do this also

@RanaUniverse
Copy link
Author

image

i am saying about this type of type hint, as after i defines,
user = update.message.from_user
which is a user class obj, so after this when i write user. i use dot(.) i got the available methods and property which belongs to a user class, so like this i got full_name, first_name like the, but when i add a new child class with reference to this User class, like i make the monkey 🍌🍌🍌 patching, the property i added there i dont get them in the type hint, example in the monkey patch i add full_name_html but in the type hint i dont get this, as this is not available in the real source User class, like this, how i say the new obj i will add, i want those to show in the type hint sugession in any way, though this is not necessary, i am asking if this is possible by some external codes part
see the property i added here,

see the image where i added the property in this,
image

@Bibo-Joshi
Copy link
Member

Thanks for reaching out and taking the time to write up this feature request.

In the future, when writing comments on GitHub threads (not only on the ptb repositories but everywher), please post code and traceback as monospaced text rather than taking screenshots. You can highlight sections of that by adding comments.

We have considered your feature request in the dev team and have decided to reject it for two reasions:

  1. Escaping text is a rather general use case, since inserting strings that are determined at runtime into a text message comes up in various scenarios. It doesn't just have to be the full_name, it could also be only the first or last name, some part of the users biography, some text that the user has entered, some text from an external source - you name it. The point is that the case were you need to escape User.full_name is a rather special one and it doesn't even have to be the HTML parse mode - you could also need escaping for Markdown formatting. We don't see a need to add this very specific functionality to a general purpose library like PTB.
  2. The convenience properties used in PTB mostly serve two purposes. Option 1: They save you from the question "Where was this attribute again?". Examples for this are Message.chat_id and Bot.username. Option 2: They save you from a simple but necessary if-clause. Examples for that are User.full_name or Message.link. However, User.full_name_html would server neither of these purposes. Instead it would abstract away only a single rather obvious function call. We don't see a big advantage of having such a convenience property.

I will hence reject & close this feature request.

Please refrain elaborating your request any further.

@Bibo-Joshi Bibo-Joshi closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants