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
6 changes: 6 additions & 0 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ def send_contact(self,
reply_markup=None,
timeout=None,
contact=None,
vcard=None,
**kwargs):
"""Use this method to send phone contacts.

Expand All @@ -1111,6 +1112,8 @@ def send_contact(self,
phone_number (:obj:`str`, optional): Contact's phone number.
first_name (:obj:`str`, optional): Contact's first name.
last_name (:obj:`str`, optional): Contact's last name.
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
0-2048 bytes.
contact (:class:`telegram.Contact`, optional): The contact to send.
disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
receive a notification with no sound.
Expand Down Expand Up @@ -1141,11 +1144,14 @@ def send_contact(self,
phone_number = contact.phone_number
first_name = contact.first_name
last_name = contact.last_name
vcard = contact.vcard

data = {'chat_id': chat_id, 'phone_number': phone_number, 'first_name': first_name}

if last_name:
data['last_name'] = last_name
if vcard:
data['vcard'] = vcard

return url, data

Expand Down
6 changes: 5 additions & 1 deletion telegram/files/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ class Contact(TelegramObject):
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`): Optional. Contact's last name.
user_id (:obj:`int`): Optional. Contact's user identifier in Telegram.
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard.

Args:
phone_number (:obj:`str`): Contact's phone number.
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`, optional): Contact's last name.
user_id (:obj:`int`, optional): Contact's user identifier in Telegram.
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard.
**kwargs (:obj:`dict`): Arbitrary keyword arguments.

"""

def __init__(self, phone_number, first_name, last_name=None, user_id=None, **kwargs):
def __init__(self, phone_number, first_name, last_name=None, user_id=None, vcard=None,
**kwargs):
# Required
self.phone_number = str(phone_number)
self.first_name = first_name
# Optionals
self.last_name = last_name
self.user_id = user_id
self.vcard = vcard

self._id_attrs = (self.phone_number,)

Expand Down
7 changes: 7 additions & 0 deletions telegram/inline/inlinequeryresultcontact.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class InlineQueryResultContact(InlineQueryResult):
phone_number (:obj:`str`): Contact's phone number.
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`): Optional. Contact's last name.
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard,
0-2048 bytes.
reply_markup (:class:`telegram.InlineKeyboardMarkup`): Optional. Inline keyboard attached
to the message.
input_message_content (:class:`telegram.InputMessageContent`): Optional. Content of the
Expand All @@ -46,6 +48,8 @@ class InlineQueryResultContact(InlineQueryResult):
phone_number (:obj:`str`): Contact's phone number.
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`, optional): Contact's last name.
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
0-2048 bytes.
reply_markup (:class:`telegram.InlineKeyboardMarkup`, optional): Inline keyboard attached
to the message.
input_message_content (:class:`telegram.InputMessageContent`, optional): Content of the
Expand All @@ -67,6 +71,7 @@ def __init__(self,
thumb_url=None,
thumb_width=None,
thumb_height=None,
vcard=None,
**kwargs):
# Required
super(InlineQueryResultContact, self).__init__('contact', id)
Expand All @@ -76,6 +81,8 @@ def __init__(self,
# Optionals
if last_name:
self.last_name = last_name
if vcard:
self.vcard = vcard
if reply_markup:
self.reply_markup = reply_markup
if input_message_content:
Expand Down
7 changes: 6 additions & 1 deletion telegram/inline/inputcontactmessagecontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ class InputContactMessageContent(InputMessageContent):
phone_number (:obj:`str`): Contact's phone number.
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`): Optional. Contact's last name.
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard,
0-2048 bytes.

Args:
phone_number (:obj:`str`): Contact's phone number.
first_name (:obj:`str`): Contact's first name.
last_name (:obj:`str`, optional): Contact's last name.
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
0-2048 bytes.
**kwargs (:obj:`dict`): Arbitrary keyword arguments.

"""

def __init__(self, phone_number, first_name, last_name=None, **kwargs):
def __init__(self, phone_number, first_name, last_name=None, vcard=None, **kwargs):
# Required
self.phone_number = phone_number
self.first_name = first_name
# Optionals
self.last_name = last_name
self.vcard = vcard
4 changes: 3 additions & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ def test_send_contact(self, bot, chat_id):
phone_number = '+11234567890'
first_name = 'Leandro'
last_name = 'Toledo'
vcard = 'vCard'
message = bot.send_contact(chat_id=chat_id, phone_number=phone_number,
first_name=first_name, last_name=last_name)
first_name=first_name, last_name=last_name, vcard=vcard)

assert message.contact
assert message.contact.phone_number == phone_number
assert message.contact.first_name == first_name
assert message.contact.last_name == last_name
assert message.contact.vcard == vcard

@pytest.mark.skipif(os.getenv('APPVEYOR'), reason='No game made for Appveyor bot (''yet)')
@flaky(3, 1)
Expand Down
11 changes: 8 additions & 3 deletions tests/test_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
@pytest.fixture(scope='class')
def contact():
return Contact(TestContact.phone_number, TestContact.first_name, TestContact.last_name,
TestContact.user_id)
TestContact.user_id, TestContact.vcard)


class TestContact(object):
phone_number = '+11234567890'
first_name = 'Leandro'
last_name = 'Toledo'
user_id = 23
vcard = 'vCard'

def test_de_json_required(self, bot):
json_dict = {'phone_number': self.phone_number, 'first_name': self.first_name}
Expand All @@ -43,20 +44,23 @@ def test_de_json_required(self, bot):

def test_de_json_all(self, bot):
json_dict = {'phone_number': self.phone_number, 'first_name': self.first_name,
'last_name': self.last_name, 'user_id': self.user_id}
'last_name': self.last_name, 'user_id': self.user_id,
'vcard': self.vcard}
contact = Contact.de_json(json_dict, bot)

assert contact.phone_number == self.phone_number
assert contact.first_name == self.first_name
assert contact.last_name == self.last_name
assert contact.user_id == self.user_id
assert contact.vcard == self.vcard

def test_send_with_contact(self, monkeypatch, bot, chat_id, contact):
def test(_, url, data, **kwargs):
phone = data['phone_number'] == contact.phone_number
first = data['first_name'] == contact.first_name
last = data['last_name'] == contact.last_name
return phone and first and last
vcard = data['vcard'] == contact.vcard
return phone and first and last and vcard

monkeypatch.setattr('telegram.utils.request.Request.post', test)
message = bot.send_contact(contact=contact, chat_id=chat_id)
Expand All @@ -74,6 +78,7 @@ def test_to_dict(self, contact):
assert contact_dict['first_name'] == contact.first_name
assert contact_dict['last_name'] == contact.last_name
assert contact_dict['user_id'] == contact.user_id
assert contact_dict['vcard'] == contact.vcard

def test_equality(self):
a = Contact(self.phone_number, self.first_name)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_inlinequeryresultcontact.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def inline_query_result_contact():
TestInlineQueryResultContact.phone_number,
TestInlineQueryResultContact.first_name,
last_name=TestInlineQueryResultContact.last_name,
vcard=TestInlineQueryResultContact.vcard,
thumb_url=TestInlineQueryResultContact.thumb_url,
thumb_width=TestInlineQueryResultContact.thumb_width,
thumb_height=TestInlineQueryResultContact.thumb_height,
Expand All @@ -43,6 +44,7 @@ class TestInlineQueryResultContact(object):
phone_number = 'phone_number'
first_name = 'first_name'
last_name = 'last_name'
vcard = 'vCard'
thumb_url = 'thumb url'
thumb_width = 10
thumb_height = 15
Expand All @@ -55,6 +57,7 @@ def test_expected_values(self, inline_query_result_contact):
assert inline_query_result_contact.phone_number == self.phone_number
assert inline_query_result_contact.first_name == self.first_name
assert inline_query_result_contact.last_name == self.last_name
assert inline_query_result_contact.vcard == self.vcard
assert inline_query_result_contact.thumb_url == self.thumb_url
assert inline_query_result_contact.thumb_width == self.thumb_width
assert inline_query_result_contact.thumb_height == self.thumb_height
Expand All @@ -74,6 +77,8 @@ def test_to_dict(self, inline_query_result_contact):
inline_query_result_contact.first_name)
assert (inline_query_result_contact_dict['last_name'] ==
inline_query_result_contact.last_name)
assert (inline_query_result_contact_dict['vcard'] ==
inline_query_result_contact.vcard)
assert (inline_query_result_contact_dict['thumb_url'] ==
inline_query_result_contact.thumb_url)
assert (inline_query_result_contact_dict['thumb_width'] ==
Expand Down
7 changes: 6 additions & 1 deletion tests/test_inputcontactmessagecontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
def input_contact_message_content():
return InputContactMessageContent(TestInputContactMessageContent.phone_number,
TestInputContactMessageContent.first_name,
last_name=TestInputContactMessageContent.last_name)
last_name=TestInputContactMessageContent.last_name,
vcard=TestInputContactMessageContent.vcard)


class TestInputContactMessageContent(object):
phone_number = 'phone number'
first_name = 'first name'
last_name = 'last name'
vcard = 'vCard'

def test_expected_values(self, input_contact_message_content):
assert input_contact_message_content.first_name == self.first_name
assert input_contact_message_content.phone_number == self.phone_number
assert input_contact_message_content.last_name == self.last_name
assert input_contact_message_content.vcard == self.vcard

def test_to_dict(self, input_contact_message_content):
input_contact_message_content_dict = input_contact_message_content.to_dict()
Expand All @@ -49,3 +52,5 @@ def test_to_dict(self, input_contact_message_content):
input_contact_message_content.first_name)
assert (input_contact_message_content_dict['last_name'] ==
input_contact_message_content.last_name)
assert (input_contact_message_content_dict['vcard'] ==
input_contact_message_content.vcard)