mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 07:50:32 +00:00
add method for get user mention (#1049)
* add new method * Create 1049.feature.rst * update test_user * after black and isort using * after black and isort using * adding new tests * fix test * update tests * update test * using create_tg_link function instead of new func * Update user.py * Update aiogram/types/user.py Co-authored-by: Alex Root Junior <jroot.junior@gmail.com>
This commit is contained in:
parent
45705faf12
commit
b8cd06fd6f
3 changed files with 52 additions and 1 deletions
4
CHANGES/1049.feature.rst
Normal file
4
CHANGES/1049.feature.rst
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Added following methods to ``User`` class:
|
||||
|
||||
- ``User.mention_markdown(...)``
|
||||
- ``User.mention_html(...)``
|
||||
|
|
@ -2,13 +2,14 @@ from __future__ import annotations
|
|||
|
||||
from typing import Optional
|
||||
|
||||
from ..utils.link import create_tg_link
|
||||
from ..utils import markdown
|
||||
from .base import TelegramObject
|
||||
|
||||
|
||||
class User(TelegramObject):
|
||||
"""
|
||||
This object represents a Telegram user or bot.
|
||||
|
||||
Source: https://core.telegram.org/bots/api#user
|
||||
"""
|
||||
|
||||
|
|
@ -40,3 +41,17 @@ class User(TelegramObject):
|
|||
if self.last_name:
|
||||
return f"{self.first_name} {self.last_name}"
|
||||
return self.first_name
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
return create_tg_link("user", id=self.id)
|
||||
|
||||
def mention_markdown(self, name: Optional[str] = None) -> str:
|
||||
if name is None:
|
||||
name = self.full_name
|
||||
return markdown.link(name, self.url)
|
||||
|
||||
def mention_html(self, name: Optional[str] = None) -> str:
|
||||
if name is None:
|
||||
name = self.full_name
|
||||
return markdown.hlink(name, self.url)
|
||||
|
|
|
|||
|
|
@ -18,3 +18,35 @@ class TestUser:
|
|||
def test_full_name(self, first: str, last: str, result: bool):
|
||||
user = User(id=42, is_bot=False, first_name=first, last_name=last)
|
||||
assert user.full_name == result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"first,last,name",
|
||||
[
|
||||
["User", None, "bebra"],
|
||||
["", None, "telegram"],
|
||||
[" ", None, "queue🤬"],
|
||||
["User", "Name", "Alex"],
|
||||
["User", " ", "aslo "],
|
||||
[" ", " ", "telebot"],
|
||||
],
|
||||
)
|
||||
def test_get_mention_markdown(self, first: str, last: str, name: str):
|
||||
user = User(id=42, is_bot=False, first_name=first, last_name=last)
|
||||
assert user.mention_markdown() == f"[{user.full_name}](tg://user?id=42)"
|
||||
assert user.mention_markdown(name=name) == f"[{name}](tg://user?id=42)"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"first,last,name",
|
||||
[
|
||||
["User", None, "bebra"],
|
||||
["", None, "telegram"],
|
||||
[" ", None, "queue🤬"],
|
||||
["User", "Name", "Alex"],
|
||||
["User", " ", "aslo "],
|
||||
[" ", " ", "telebot"],
|
||||
],
|
||||
)
|
||||
def test_get_mention_html(self, first: str, last: str, name: str):
|
||||
user = User(id=42, is_bot=False, first_name=first, last_name=last)
|
||||
assert user.mention_html() == f'<a href="tg://user?id=42">{user.full_name}</a>'
|
||||
assert user.mention_html(name=name) == f'<a href="tg://user?id=42">{name}</a>'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue