Skip to content

Commit

Permalink
add property methods for User
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Jul 7, 2024
1 parent 3a23043 commit d8440d3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
47 changes: 45 additions & 2 deletions tgram/bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import tgram

from typing import Union, List, Optional
from typing import Union, List, Optional, Literal
from tgram import sync

from io import BytesIO
Expand Down Expand Up @@ -496,7 +496,20 @@ def id(self: "tgram.types.Message") -> int:
return self.message_id

@property
def media(self: "tgram.types.Message") -> Optional["str"]:
def media(
self: "tgram.types.Message",
) -> Optional[
Literal[
"audio",
"video",
"photo",
"animation",
"voice",
"video_note",
"sticker",
"document",
]
]:
for media_type in MEDIA_TYPES:
if getattr(self, media_type):
return media_type
Expand Down Expand Up @@ -525,6 +538,36 @@ async def answer(
)


class UserB:
@property
def mention(
self: "tgram.types.User",
name: str = None,
parse_mode: Literal["HTML", "Markdown", "MarkdownV2"] = "HTML",
) -> str:
return (
"[{name}](tg://user?id={id})"
if parse_mode.lower() != "html"
else '<a href="tg://user?id={id}">{name}</a>'
).format(name=name or self.first_name, id=self.id)

@property
def full_name(self: "tgram.types.User") -> str:
return (
self.first_name
if not self.last_name
else f"{self.first_name} {self.last_name}"
)

@property
def profile_link(self: "tgram.types.User") -> str:
return (
f"tg://user?id={self.id}"
if not self.username
else f"https://t.me/{self.username}"
)


for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
sync.wrap(obj)
4 changes: 2 additions & 2 deletions tgram/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from json import dumps

from .bound import MessageB, CallbackB
from .bound import MessageB, CallbackB, UserB


class Type_:
Expand Down Expand Up @@ -219,7 +219,7 @@ def _parse(me: "tgram.TgBot" = None, d: dict = None) -> Optional["WebhookInfo"]:
)


class User(Type_):
class User(Type_, UserB):
def __init__(
self,
id: "int",
Expand Down

0 comments on commit d8440d3

Please sign in to comment.