Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
54ea342
Add User.link and update User.mention_*
jonowo Apr 27, 2018
0f4f695
Add Chat.link
jonowo Apr 27, 2018
466eaa2
Add Message.link
jonowo Apr 27, 2018
7986c74
Merge pull request #1 from Tr-Jono/Tr-Jono-patch-1
jonowo Apr 27, 2018
820c0e2
Tiny modification
jonowo Apr 27, 2018
5069b0e
Merge pull request #2 from Tr-Jono/Tr-Jono-patch-1-1
jonowo Apr 27, 2018
b584bba
Merge pull request #3 from Tr-Jono/Tr-Jono-patch-1-2
jonowo Apr 27, 2018
80a140e
Fixed nub mistakes I made
jonowo Apr 27, 2018
aca6eda
Fixed mistakes I made when I was fixing mistakes
jonowo Apr 27, 2018
ed52057
Link returns None on default
jonowo Apr 30, 2018
af8e909
Link returns None on default
jonowo Apr 30, 2018
27cbdbf
Link returns None on default
jonowo Apr 30, 2018
e18d076
Update chat.py
jonowo Apr 30, 2018
a2247b6
Fixed link and it returns None on default
jonowo Apr 30, 2018
aff64c3
Ooooooops
jonowo Apr 30, 2018
2016e75
Reverted Indent
jonowo Apr 30, 2018
5d45047
Update user.py
jonowo Apr 30, 2018
41f697e
Update message.py
jonowo Apr 30, 2018
1d622fe
Add the blank lines
jonowo Apr 30, 2018
dfae43b
Add https
jonowo May 7, 2018
38a1c12
Update chat.py
jonowo May 7, 2018
86df641
Add https
jonowo May 7, 2018
3db087a
Add test link
jonowo May 7, 2018
c844562
Add test link
jonowo May 7, 2018
cf0da15
Add test_link
jonowo May 8, 2018
3915bc5
Mistakes fixed
jonowo May 8, 2018
c5e8576
Update test_chat.py
jonowo May 8, 2018
6a49537
Fixed "trailing whitespace"
jonowo May 8, 2018
f0c45fc
Fixed "line too long"
jonowo May 8, 2018
03aa149
Fixed whitespace mistakes
jonowo May 8, 2018
0689616
Chat could be private/channel too so should not force check supergrou…
jeffffc May 9, 2018
9f336db
Merge pull request #4 from jeffffc/jono2
jonowo May 9, 2018
a8d8ac0
fix docstrings blank lines
jeffffc May 9, 2018
90d31ea
Merge pull request #5 from jeffffc/jono2
jonowo May 9, 2018
c0a254e
fix docstrings whitespaces in blank lines
jeffffc May 9, 2018
e2bc04b
Merge pull request #6 from jeffffc/jono2
jonowo May 9, 2018
ac13a76
Merge branch 'master' into 1092master
Eldinnie May 9, 2018
e2b4424
Some small fixes
Eldinnie May 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions telegram/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def __init__(self,
self.bot = bot
self._id_attrs = (self.id,)

@property
def link(self):
""":obj:`str`: Convenience property. If the chat has a :attr:`username`, returns a t.me
link of the chat."""
if self.username:
return "https://t.me/{}".format(self.username)
return None

@classmethod
def de_json(cls, data, bot):
if not data:
Expand Down
8 changes: 8 additions & 0 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ def chat_id(self):
""":obj:`int`: Shortcut for :attr:`telegram.Chat.id` for :attr:`chat`."""
return self.chat.id

@property
def link(self):
""":obj:`str`: Convenience property. If the chat of the message is a supergroup or a
channel and has a :attr:`Chat.username`, returns a t.me link of the message."""
if self.chat.type in (Chat.SUPERGROUP, Chat.CHANNEL) and self.chat.username:
return "https://t.me/{}/{}".format(self.chat.username, self.message_id)
return None

@classmethod
def de_json(cls, data, bot):
if not data:
Expand Down
38 changes: 21 additions & 17 deletions telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,30 @@ def __init__(self,

@property
def name(self):
"""
:obj:`str`: Convenience property. If available, returns the user's :attr:`username`
prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`.

"""
""":obj:`str`: Convenience property. If available, returns the user's :attr:`username`
prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`."""
if self.username:
return '@{}'.format(self.username)
return self.full_name

@property
def full_name(self):
"""
:obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if available)
:attr:`last_name`.
""":obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if
available) :attr:`last_name`."""

"""
if self.last_name:
return u'{} {}'.format(self.first_name, self.last_name)
return self.first_name

@property
def link(self):
""":obj:`str`: Convenience property. If :attr:`username` is available, returns a t.me link
of the user."""

if self.username:
return "https://t.me/{}".format(self.username)
return None

@classmethod
def de_json(cls, data, bot):
if not data:
Expand Down Expand Up @@ -124,28 +128,28 @@ def de_list(cls, data, bot):
def mention_markdown(self, name=None):
"""
Args:
name (:obj:`str`): If provided, will overwrite the user's name.
name (:obj:`str`): The name used as a link for the user. Defaults to :attr:`full_name`.

Returns:
:obj:`str`: The inline mention for the user as markdown.

"""
if not name:
return util_mention_markdown(self.id, self.name)
else:
if name:
Copy link
Member

Choose a reason for hiding this comment

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

This change is out of scope... we're more likely to merge simple self contained pull requests...
IMO this is fine this time though :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will separate into 2 PRs next time.

return util_mention_markdown(self.id, name)
return util_mention_markdown(self.id, self.full_name)

def mention_html(self, name=None):
"""
Args:
name (:obj:`str`): If provided, will overwrite the user's name.
name (:obj:`str`): The name used as a link for the user. Defaults to :attr:`full_name`.

Returns:
:obj:`str`: The inline mention for the user as HTML.

"""
if not name:
return util_mention_html(self.id, self.name)
else:
if name:
return util_mention_html(self.id, name)
return util_mention_html(self.id, self.full_name)

def send_message(self, *args, **kwargs):
"""Shortcut for::
Expand Down
11 changes: 10 additions & 1 deletion tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@pytest.fixture(scope='class')
def chat(bot):
return Chat(TestChat.id, TestChat.title, TestChat.type,
return Chat(TestChat.id, TestChat.title, TestChat.type, username=TestChat.username,
all_members_are_administrators=TestChat.all_members_are_administrators,
bot=bot, sticker_set_name=TestChat.sticker_set_name,
can_set_sticker_set=TestChat.can_set_sticker_set)
Expand All @@ -35,6 +35,7 @@ class TestChat(object):
id = -28767330
title = 'ToledosPalaceBot - Group'
type = 'group'
username = 'username'
all_members_are_administrators = False
sticker_set_name = 'stickers'
can_set_sticker_set = False
Expand All @@ -44,6 +45,7 @@ def test_de_json(self, bot):
'id': self.id,
'title': self.title,
'type': self.type,
'username': self.username,
'all_members_are_administrators': self.all_members_are_administrators,
'sticker_set_name': self.sticker_set_name,
'can_set_sticker_set': self.can_set_sticker_set
Expand All @@ -53,6 +55,7 @@ def test_de_json(self, bot):
assert chat.id == self.id
assert chat.title == self.title
assert chat.type == self.type
assert chat.username == self.username
assert chat.all_members_are_administrators == self.all_members_are_administrators
assert chat.sticker_set_name == self.sticker_set_name
assert chat.can_set_sticker_set == self.can_set_sticker_set
Expand All @@ -64,8 +67,14 @@ def test_to_dict(self, chat):
assert chat_dict['id'] == chat.id
assert chat_dict['title'] == chat.title
assert chat_dict['type'] == chat.type
assert chat_dict['username'] == chat.username
assert chat_dict['all_members_are_administrators'] == chat.all_members_are_administrators

def test_link(self, chat):
assert chat.link == 'https://t.me/{}'.format(chat.username)
chat.username = None
assert chat.link is None

def test_send_action(self, monkeypatch, chat):
def test(*args, **kwargs):
id = args[1] == chat.id
Expand Down
12 changes: 12 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ def test_parse_entities_url_emoji(self):
def test_chat_id(self, message):
assert message.chat_id == message.chat.id

def test_link(self, message):
assert message.link is None
message.chat.username = 'username'
message.chat.type = 'supergroup'
assert message.link == 'https://t.me/{}/{}'.format(message.chat.username,
message.message_id)
message.chat.type = 'channel'
assert message.link == 'https://t.me/{}/{}'.format(message.chat.username,
message.message_id)
message.chat.type = 'private'
assert message.link is None

def test_effective_attachment(self, message_params):
for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note',
'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def test_full_name(self, user):
user.last_name = None
assert user.full_name == u'first\u2022name'

def test_link(self, user):
assert user.link == 'https://t.me/{}'.format(user.username)
user.username = None
assert user.link is None

def test_get_profile_photos(self, monkeypatch, user):
def test(_, *args, **kwargs):
return args[0] == user.id
Expand Down