Skip to content

Commit b6a0853

Browse files
jeffffcEldinnie
authored andcommitted
Add Bot API 3.3 (python-telegram-bot#806)
* Bot API 3.3 * simpler helper methods (accept only custom names now) attempt to fix circular import (not sure if its the correct way tho) added helper methods into User object * fix User objects in tests to contain is_bot required value * fix User objects in tests to contain is_bot required value * delete extra line that caused flake8 error * fix swapped arguments
1 parent 16a49ec commit b6a0853

30 files changed

+198
-113
lines changed

telegram/chat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Chat(TelegramObject):
3737
photo (:class:`telegram.ChatPhoto`): Optional. Chat photo.
3838
description (:obj:`str`): Optional. Description, for supergroups and channel chats.
3939
invite_link (:obj:`str`): Optional. Chat invite link, for supergroups and channel chats.
40+
pinned_message (:class:`telegram.Message`): Optional. Pinned message, for supergroups.
41+
Returned only in get_chat.
4042
4143
Args:
4244
id (:obj:`int`): Unique identifier for this chat. This number may be greater than 32 bits
@@ -57,6 +59,8 @@ class Chat(TelegramObject):
5759
Returned only in get_chat.
5860
invite_link (:obj:`str`, optional): Chat invite link, for supergroups and channel chats.
5961
Returned only in get_chat.
62+
pinned_message (:class:`telegram.Message`, optional): Pinned message, for supergroups.
63+
Returned only in get_chat.
6064
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
6165
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
6266
@@ -83,6 +87,7 @@ def __init__(self,
8387
photo=None,
8488
description=None,
8589
invite_link=None,
90+
pinned_message=None,
8691
**kwargs):
8792
# Required
8893
self.id = int(id)
@@ -96,6 +101,7 @@ def __init__(self,
96101
self.photo = photo
97102
self.description = description
98103
self.invite_link = invite_link
104+
self.pinned_message = pinned_message
99105

100106
self.bot = bot
101107
self._id_attrs = (self.id,)
@@ -106,6 +112,8 @@ def de_json(cls, data, bot):
106112
return None
107113

108114
data['photo'] = ChatPhoto.de_json(data.get('photo'), bot)
115+
from telegram import Message
116+
data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)
109117

110118
return cls(bot=bot, **data)
111119

telegram/message.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class Message(TelegramObject):
8686
invoice (:class:`telegram.Invoice`): Optional. Information about the invoice.
8787
successful_payment (:class:`telegram.SuccessfulPayment`): Optional. Information about the
8888
payment.
89+
forward_signature (:obj:`str`): Optional. Signature of the post author for messages
90+
forwarded from channels.
91+
author_signature (:obj:`str`): Optional. Signature of the post author for messages
92+
in channels.
8993
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
9094
9195
Deprecated: 6.0
@@ -172,6 +176,10 @@ class Message(TelegramObject):
172176
information about the invoice.
173177
successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service
174178
message about a successful payment, information about the payment.
179+
forward_signature (:obj:`str`, optional): Signature of the post author for messages
180+
forwarded from channels.
181+
author_signature (:obj:`str`, optional): Signature of the post author for messages
182+
in channels.
175183
"""
176184
_effective_attachment = _UNDEFINED
177185

@@ -214,6 +222,8 @@ def __init__(self,
214222
pinned_message=None,
215223
invoice=None,
216224
successful_payment=None,
225+
forward_signature=None,
226+
author_signature=None,
217227
bot=None,
218228
**kwargs):
219229
# Required
@@ -256,6 +266,8 @@ def __init__(self,
256266
self.forward_from_message_id = forward_from_message_id
257267
self.invoice = invoice
258268
self.successful_payment = successful_payment
269+
self.forward_signature = forward_signature
270+
self.author_signature = author_signature
259271

260272
self.bot = bot
261273

telegram/user.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"""This module contains an object that represents a Telegram User."""
2121

2222
from telegram import TelegramObject
23+
from telegram.utils.helpers import mention_markdown as util_mention_markdown
24+
from telegram.utils.helpers import mention_html as util_mention_html
2325

2426

2527
class User(TelegramObject):
@@ -28,6 +30,7 @@ class User(TelegramObject):
2830
2931
Attributes:
3032
id (:obj:`int`): Unique identifier for this user or bot.
33+
is_bot (:obj:`bool`): True, if this user is a bot
3134
first_name (:obj:`str`): User's or bot's first name.
3235
last_name (:obj:`str`): Optional. User's or bot's last name.
3336
username (:obj:`str`): Optional. User's or bot's last name.
@@ -36,6 +39,7 @@ class User(TelegramObject):
3639
3740
Args:
3841
id (:obj:`int`): Unique identifier for this user or bot.
42+
is_bot (:obj:`bool`): True, if this user is a bot
3943
first_name (:obj:`str`): User's or bot's first name.
4044
last_name (:obj:`str`, optional): User's or bot's last name.
4145
username (:obj:`str`, optional): User's or bot's username.
@@ -46,6 +50,7 @@ class User(TelegramObject):
4650
def __init__(self,
4751
id,
4852
first_name,
53+
is_bot,
4954
last_name=None,
5055
username=None,
5156
language_code=None,
@@ -54,6 +59,7 @@ def __init__(self,
5459
# Required
5560
self.id = int(id)
5661
self.first_name = first_name
62+
self.is_bot = is_bot
5763
# Optionals
5864
self.last_name = last_name
5965
self.username = username
@@ -105,3 +111,29 @@ def de_list(cls, data, bot):
105111
users.append(cls.de_json(user, bot))
106112

107113
return users
114+
115+
def mention_markdown(self, name=None):
116+
"""
117+
Args:
118+
name (:obj:`str`): If provided, will overwrite the user's name.
119+
120+
Returns:
121+
:obj:`str`: The inline mention for the user as markdown.
122+
"""
123+
if not name:
124+
return util_mention_markdown(self.id, self.name)
125+
else:
126+
return util_mention_markdown(self.id, name)
127+
128+
def mention_html(self, name=None):
129+
"""
130+
Args:
131+
name (:obj:`str`): If provided, will overwrite the user's name.
132+
133+
Returns:
134+
:obj:`str`: The inline mention for the user as HTML.
135+
"""
136+
if not name:
137+
return util_mention_html(self.id, self.name)
138+
else:
139+
return util_mention_html(self.id, name)

telegram/utils/helpers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,29 @@ def from_timestamp(unixtime):
7272
return None
7373

7474
return datetime.fromtimestamp(unixtime)
75+
76+
77+
def mention_html(user_id, name):
78+
"""
79+
Args:
80+
user_id (:obj:`int`) The user's id which you want to mention.
81+
name (:obj:`str`) The name the mention is showing.
82+
83+
Returns:
84+
:obj:`str`: The inline mention for the user as html.
85+
"""
86+
if isinstance(user_id, int):
87+
return '<a href="tg://user?id={}">{}</a>'.format(user_id, escape_html(name))
88+
89+
90+
def mention_markdown(user_id, name):
91+
"""
92+
Args:
93+
user_id (:obj:`int`) The user's id which you want to mention.
94+
name (:obj:`str`) The name the mention is showing.
95+
96+
Returns:
97+
:obj:`str`: The inline mention for the user as markdown.
98+
"""
99+
if isinstance(user_id, int):
100+
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id)

tests/test_callbackquery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def callback_query(bot, request):
3939

4040
class TestCallbackQuery(object):
4141
id = 'id'
42-
from_user = User(1, 'test_user')
42+
from_user = User(1, 'test_user', False)
4343
chat_instance = 'chat_instance'
44-
message = Message(3, User(5, 'bot'), None, Chat(4, 'private'))
44+
message = Message(3, User(5, 'bot', False), None, Chat(4, 'private'))
4545
data = 'data'
4646
inline_message_id = 'inline_message_id'
4747
game_short_name = 'the_game'

tests/test_callbackqueryhandler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
2323
from telegram.ext import CallbackQueryHandler
2424

25-
message = Message(1, User(1, ''), None, Chat(1, ''), text='Text')
25+
message = Message(1, User(1, '', False), None, Chat(1, ''), text='Text')
2626

2727
params = [
2828
{'message': message},
2929
{'edited_message': message},
3030
{'channel_post': message},
3131
{'edited_channel_post': message},
32-
{'inline_query': InlineQuery(1, User(1, ''), '', '')},
33-
{'chosen_inline_result': ChosenInlineResult('id', User(1, ''), '')},
34-
{'shipping_query': ShippingQuery('id', User(1, ''), '', None)},
35-
{'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')}
32+
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
33+
{'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '')},
34+
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
35+
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')}
3636
]
3737

3838
ids = ('message', 'edited_message', 'channel_post',

tests/test_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_equality(self):
122122
b = Chat(self.id, self.title, self.type)
123123
c = Chat(self.id, '', '')
124124
d = Chat(0, self.title, self.type)
125-
e = User(self.id, '')
125+
e = User(self.id, '', False)
126126

127127
assert a == b
128128
assert hash(a) == hash(b)

tests/test_chatmember.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@pytest.fixture(scope='class')
2828
def user():
29-
return User(1, 'First name')
29+
return User(1, 'First name', False)
3030

3131

3232
@pytest.fixture(scope='class')
@@ -89,10 +89,10 @@ def test_to_dict(self, chat_member):
8989
assert chat_member['status'] == chat_member.status
9090

9191
def test_equality(self):
92-
a = ChatMember(User(1, ''), ChatMember.ADMINISTRATOR)
93-
b = ChatMember(User(1, ''), ChatMember.ADMINISTRATOR)
94-
d = ChatMember(User(2, ''), ChatMember.ADMINISTRATOR)
95-
d2 = ChatMember(User(1, ''), ChatMember.CREATOR)
92+
a = ChatMember(User(1, '', False), ChatMember.ADMINISTRATOR)
93+
b = ChatMember(User(1, '', False), ChatMember.ADMINISTRATOR)
94+
d = ChatMember(User(2, '', False), ChatMember.ADMINISTRATOR)
95+
d2 = ChatMember(User(1, '', False), ChatMember.CREATOR)
9696

9797
assert a == b
9898
assert hash(a) == hash(b)

tests/test_choseninlineresult.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
@pytest.fixture(scope='class')
2626
def user():
27-
return User(1, 'First name')
27+
return User(1, 'First name', False)
2828

2929

3030
@pytest.fixture(scope='class')

tests/test_choseninlineresulthandler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
InlineQuery, ShippingQuery, PreCheckoutQuery)
2424
from telegram.ext import ChosenInlineResultHandler
2525

26-
message = Message(1, User(1, ''), None, Chat(1, ''), text='Text')
26+
message = Message(1, User(1, '', False), None, Chat(1, ''), text='Text')
2727

2828
params = [
2929
{'message': message},
3030
{'edited_message': message},
31-
{'callback_query': CallbackQuery(1, User(1, ''), 'chat', message=message)},
31+
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message)},
3232
{'channel_post': message},
3333
{'edited_channel_post': message},
34-
{'inline_query': InlineQuery(1, User(1, ''), '', '')},
35-
{'shipping_query': ShippingQuery('id', User(1, ''), '', None)},
36-
{'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')},
37-
{'callback_query': CallbackQuery(1, User(1, ''), 'chat')}
34+
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
35+
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
36+
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')},
37+
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat')}
3838
]
3939

4040
ids = ('message', 'edited_message', 'callback_query', 'channel_post',
@@ -50,7 +50,7 @@ def false_update(request):
5050
@pytest.fixture(scope='class')
5151
def chosen_inline_result():
5252
return Update(1, chosen_inline_result=ChosenInlineResult('result_id',
53-
User(1, 'test_user'),
53+
User(1, 'test_user', False),
5454
'query'))
5555

5656

0 commit comments

Comments
 (0)