Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -37,6 +37,8 @@ class Chat(TelegramObject):
photo (:class:`telegram.ChatPhoto`): Optional. Chat photo.
description (:obj:`str`): Optional. Description, for supergroups and channel chats.
invite_link (:obj:`str`): Optional. Chat invite link, for supergroups and channel chats.
pinned_message (:class:`telegram.Message`): Optional. Pinned message, for supergroups.
Returned only in get_chat.

Args:
id (:obj:`int`): Unique identifier for this chat. This number may be greater than 32 bits
Expand All @@ -57,6 +59,8 @@ class Chat(TelegramObject):
Returned only in get_chat.
invite_link (:obj:`str`, optional): Chat invite link, for supergroups and channel chats.
Returned only in get_chat.
pinned_message (:class:`telegram.Message`, optional): Pinned message, for supergroups.
Returned only in get_chat.
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
**kwargs (:obj:`dict`): Arbitrary keyword arguments.

Expand All @@ -83,6 +87,7 @@ def __init__(self,
photo=None,
description=None,
invite_link=None,
pinned_message=None,
**kwargs):
# Required
self.id = int(id)
Expand All @@ -96,6 +101,7 @@ def __init__(self,
self.photo = photo
self.description = description
self.invite_link = invite_link
self.pinned_message = pinned_message

self.bot = bot
self._id_attrs = (self.id,)
Expand All @@ -106,6 +112,8 @@ def de_json(cls, data, bot):
return None

data['photo'] = ChatPhoto.de_json(data.get('photo'), bot)
from telegram import Message
data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)

return cls(bot=bot, **data)

Expand Down
12 changes: 12 additions & 0 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class Message(TelegramObject):
invoice (:class:`telegram.Invoice`): Optional. Information about the invoice.
successful_payment (:class:`telegram.SuccessfulPayment`): Optional. Information about the
payment.
forward_signature (:obj:`str`): Optional. Signature of the post author for messages
forwarded from channels.
author_signature (:obj:`str`): Optional. Signature of the post author for messages
in channels.
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.

Deprecated: 6.0
Expand Down Expand Up @@ -172,6 +176,10 @@ class Message(TelegramObject):
information about the invoice.
successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service
message about a successful payment, information about the payment.
forward_signature (:obj:`str`, optional): Signature of the post author for messages
forwarded from channels.
author_signature (:obj:`str`, optional): Signature of the post author for messages
in channels.
"""
_effective_attachment = _UNDEFINED

Expand Down Expand Up @@ -214,6 +222,8 @@ def __init__(self,
pinned_message=None,
invoice=None,
successful_payment=None,
forward_signature=None,
author_signature=None,
bot=None,
**kwargs):
# Required
Expand Down Expand Up @@ -256,6 +266,8 @@ def __init__(self,
self.forward_from_message_id = forward_from_message_id
self.invoice = invoice
self.successful_payment = successful_payment
self.forward_signature = forward_signature
self.author_signature = author_signature

self.bot = bot

Expand Down
32 changes: 32 additions & 0 deletions telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"""This module contains an object that represents a Telegram User."""

from telegram import TelegramObject
from telegram.utils.helpers import mention_markdown as util_mention_markdown
from telegram.utils.helpers import mention_html as util_mention_html


class User(TelegramObject):
Expand All @@ -28,6 +30,7 @@ class User(TelegramObject):

Attributes:
id (:obj:`int`): Unique identifier for this user or bot.
is_bot (:obj:`bool`): True, if this user is a bot
first_name (:obj:`str`): User's or bot's first name.
last_name (:obj:`str`): Optional. User's or bot's last name.
username (:obj:`str`): Optional. User's or bot's last name.
Expand All @@ -36,6 +39,7 @@ class User(TelegramObject):

Args:
id (:obj:`int`): Unique identifier for this user or bot.
is_bot (:obj:`bool`): True, if this user is a bot
first_name (:obj:`str`): User's or bot's first name.
last_name (:obj:`str`, optional): User's or bot's last name.
username (:obj:`str`, optional): User's or bot's username.
Expand All @@ -46,6 +50,7 @@ class User(TelegramObject):
def __init__(self,
id,
first_name,
is_bot,
last_name=None,
username=None,
language_code=None,
Expand All @@ -54,6 +59,7 @@ def __init__(self,
# Required
self.id = int(id)
self.first_name = first_name
self.is_bot = is_bot
# Optionals
self.last_name = last_name
self.username = username
Expand Down Expand Up @@ -105,3 +111,29 @@ def de_list(cls, data, bot):
users.append(cls.de_json(user, bot))

return users

def mention_markdown(self, name=None):
"""
Args:
name (:obj:`str`): If provided, will overwrite the user's name.

Returns:
:obj:`str`: The inline mention for the user as markdown.
"""
if not name:
return util_mention_markdown(self.id, self.name)
else:
return util_mention_markdown(self.id, name)

def mention_html(self, name=None):
"""
Args:
name (:obj:`str`): If provided, will overwrite the user's name.

Returns:
:obj:`str`: The inline mention for the user as HTML.
"""
if not name:
return util_mention_html(self.id, self.name)
else:
return util_mention_html(self.id, name)
26 changes: 26 additions & 0 deletions telegram/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,29 @@ def from_timestamp(unixtime):
return None

return datetime.fromtimestamp(unixtime)


def mention_html(user_id, name):
"""
Args:
user_id (:obj:`int`) The user's id which you want to mention.
name (:obj:`str`) The name the mention is showing.

Returns:
:obj:`str`: The inline mention for the user as html.
"""
if isinstance(user_id, int):
return '<a href="tg://user?id={}">{}</a>'.format(user_id, escape_html(name))


def mention_markdown(user_id, name):
"""
Args:
user_id (:obj:`int`) The user's id which you want to mention.
name (:obj:`str`) The name the mention is showing.

Returns:
:obj:`str`: The inline mention for the user as markdown.
"""
if isinstance(user_id, int):
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
4 changes: 2 additions & 2 deletions tests/test_callbackquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def callback_query(bot, request):

class TestCallbackQuery(object):
id = 'id'
from_user = User(1, 'test_user')
from_user = User(1, 'test_user', False)
chat_instance = 'chat_instance'
message = Message(3, User(5, 'bot'), None, Chat(4, 'private'))
message = Message(3, User(5, 'bot', False), None, Chat(4, 'private'))
data = 'data'
inline_message_id = 'inline_message_id'
game_short_name = 'the_game'
Expand Down
10 changes: 5 additions & 5 deletions tests/test_callbackqueryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
from telegram.ext import CallbackQueryHandler

message = Message(1, User(1, ''), None, Chat(1, ''), text='Text')
message = Message(1, User(1, '', False), None, Chat(1, ''), text='Text')

params = [
{'message': message},
{'edited_message': message},
{'channel_post': message},
{'edited_channel_post': message},
{'inline_query': InlineQuery(1, User(1, ''), '', '')},
{'chosen_inline_result': ChosenInlineResult('id', User(1, ''), '')},
{'shipping_query': ShippingQuery('id', User(1, ''), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')}
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
{'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '')},
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')}
]

ids = ('message', 'edited_message', 'channel_post',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_equality(self):
b = Chat(self.id, self.title, self.type)
c = Chat(self.id, '', '')
d = Chat(0, self.title, self.type)
e = User(self.id, '')
e = User(self.id, '', False)

assert a == b
assert hash(a) == hash(b)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_chatmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@pytest.fixture(scope='class')
def user():
return User(1, 'First name')
return User(1, 'First name', False)


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

def test_equality(self):
a = ChatMember(User(1, ''), ChatMember.ADMINISTRATOR)
b = ChatMember(User(1, ''), ChatMember.ADMINISTRATOR)
d = ChatMember(User(2, ''), ChatMember.ADMINISTRATOR)
d2 = ChatMember(User(1, ''), ChatMember.CREATOR)
a = ChatMember(User(1, '', False), ChatMember.ADMINISTRATOR)
b = ChatMember(User(1, '', False), ChatMember.ADMINISTRATOR)
d = ChatMember(User(2, '', False), ChatMember.ADMINISTRATOR)
d2 = ChatMember(User(1, '', False), ChatMember.CREATOR)

assert a == b
assert hash(a) == hash(b)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_choseninlineresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@pytest.fixture(scope='class')
def user():
return User(1, 'First name')
return User(1, 'First name', False)


@pytest.fixture(scope='class')
Expand Down
14 changes: 7 additions & 7 deletions tests/test_choseninlineresulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
InlineQuery, ShippingQuery, PreCheckoutQuery)
from telegram.ext import ChosenInlineResultHandler

message = Message(1, User(1, ''), None, Chat(1, ''), text='Text')
message = Message(1, User(1, '', False), None, Chat(1, ''), text='Text')

params = [
{'message': message},
{'edited_message': message},
{'callback_query': CallbackQuery(1, User(1, ''), 'chat', message=message)},
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message)},
{'channel_post': message},
{'edited_channel_post': message},
{'inline_query': InlineQuery(1, User(1, ''), '', '')},
{'shipping_query': ShippingQuery('id', User(1, ''), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')},
{'callback_query': CallbackQuery(1, User(1, ''), 'chat')}
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')},
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat')}
]

ids = ('message', 'edited_message', 'callback_query', 'channel_post',
Expand All @@ -50,7 +50,7 @@ def false_update(request):
@pytest.fixture(scope='class')
def chosen_inline_result():
return Update(1, chosen_inline_result=ChosenInlineResult('result_id',
User(1, 'test_user'),
User(1, 'test_user', False),
'query'))


Expand Down
14 changes: 7 additions & 7 deletions tests/test_commandhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
from telegram.ext import CommandHandler, Filters

message = Message(1, User(1, ''), None, Chat(1, ''), text='test')
message = Message(1, User(1, '', False), None, Chat(1, ''), text='test')

params = [
{'callback_query': CallbackQuery(1, User(1, ''), 'chat', message=message)},
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message)},
{'channel_post': message},
{'edited_channel_post': message},
{'inline_query': InlineQuery(1, User(1, ''), '', '')},
{'chosen_inline_result': ChosenInlineResult('id', User(1, ''), '')},
{'shipping_query': ShippingQuery('id', User(1, ''), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')},
{'callback_query': CallbackQuery(1, User(1, ''), 'chat')}
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
{'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '')},
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')},
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat')}
]

ids = ('callback_query', 'channel_post', 'edited_channel_post', 'inline_query',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

@pytest.fixture(scope='class')
def user1():
return User(first_name='Misses Test', id=123)
return User(first_name='Misses Test', id=123, is_bot=False)


@pytest.fixture(scope='class')
def user2():
return User(first_name='Mister Test', id=124)
return User(first_name='Mister Test', id=124, is_bot=False)


class TestConversationHandler(object):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def dp2(bot):


class TestDispatcher(object):
message_update = Update(1, message=Message(1, User(1, ''), None, Chat(1, ''), text='Text'))
message_update = Update(1, message=Message(1, User(1, '', False), None, Chat(1, ''), text='Text'))
received = None
count = 0

Expand Down
4 changes: 2 additions & 2 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@pytest.fixture(scope='function')
def message():
return Message(0, User(0, 'Testuser'), datetime.datetime.now(), Chat(0, 'private'))
return Message(0, User(0, 'Testuser', False), datetime.datetime.now(), Chat(0, 'private'))


@pytest.fixture(scope='function',
Expand All @@ -52,7 +52,7 @@ def test_filters_command(self, message):
assert Filters.command(message)

def test_filters_reply(self, message):
another_message = Message(1, User(1, 'TestOther'), datetime.datetime.now(),
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
Chat(0, 'private'))
message.text = 'test'
assert not Filters.reply(message)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gamehighscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def game_highscore():

class TestGameHighScore(object):
position = 12
user = User(2, 'test user')
user = User(2, 'test user', False)
score = 42

def test_de_json(self, bot):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_inlinequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def inline_query(bot):

class TestInlineQuery(object):
id = 1234
from_user = User(1, 'First name')
from_user = User(1, 'First name', False)
query = 'query text'
offset = 'offset'
location = Location(8.8, 53.1)
Expand Down Expand Up @@ -69,10 +69,10 @@ def test(*args, **kwargs):
assert inline_query.answer()

def test_equality(self):
a = InlineQuery(self.id, User(1, ''), '', '')
b = InlineQuery(self.id, User(1, ''), '', '')
c = InlineQuery(self.id, User(0, ''), '', '')
d = InlineQuery(0, User(1, ''), '', '')
a = InlineQuery(self.id, User(1, '', False), '', '')
b = InlineQuery(self.id, User(1, '', False), '', '')
c = InlineQuery(self.id, User(0, '', False), '', '')
d = InlineQuery(0, User(1, '', False), '', '')
e = Update(self.id)

assert a == b
Expand Down
Loading