Skip to content

Commit bc4ded8

Browse files
authored
Merge pull request python-telegram-bot#1169 from code1mountain/vCard_field
Add vCard Field to Contact and related
2 parents a34efa5 + 56eb69c commit bc4ded8

File tree

8 files changed

+46
-7
lines changed

8 files changed

+46
-7
lines changed

telegram/bot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ def send_contact(self,
12251225
reply_markup=None,
12261226
timeout=None,
12271227
contact=None,
1228+
vcard=None,
12281229
**kwargs):
12291230
"""Use this method to send phone contacts.
12301231
@@ -1238,6 +1239,8 @@ def send_contact(self,
12381239
phone_number (:obj:`str`, optional): Contact's phone number.
12391240
first_name (:obj:`str`, optional): Contact's first name.
12401241
last_name (:obj:`str`, optional): Contact's last name.
1242+
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
1243+
0-2048 bytes.
12411244
contact (:class:`telegram.Contact`, optional): The contact to send.
12421245
disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
12431246
receive a notification with no sound.
@@ -1268,11 +1271,14 @@ def send_contact(self,
12681271
phone_number = contact.phone_number
12691272
first_name = contact.first_name
12701273
last_name = contact.last_name
1274+
vcard = contact.vcard
12711275

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

12741278
if last_name:
12751279
data['last_name'] = last_name
1280+
if vcard:
1281+
data['vcard'] = vcard
12761282

12771283
return url, data
12781284

telegram/files/contact.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,27 @@ class Contact(TelegramObject):
2929
first_name (:obj:`str`): Contact's first name.
3030
last_name (:obj:`str`): Optional. Contact's last name.
3131
user_id (:obj:`int`): Optional. Contact's user identifier in Telegram.
32+
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard.
3233
3334
Args:
3435
phone_number (:obj:`str`): Contact's phone number.
3536
first_name (:obj:`str`): Contact's first name.
3637
last_name (:obj:`str`, optional): Contact's last name.
3738
user_id (:obj:`int`, optional): Contact's user identifier in Telegram.
39+
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard.
3840
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
3941
4042
"""
4143

42-
def __init__(self, phone_number, first_name, last_name=None, user_id=None, **kwargs):
44+
def __init__(self, phone_number, first_name, last_name=None, user_id=None, vcard=None,
45+
**kwargs):
4346
# Required
4447
self.phone_number = str(phone_number)
4548
self.first_name = first_name
4649
# Optionals
4750
self.last_name = last_name
4851
self.user_id = user_id
52+
self.vcard = vcard
4953

5054
self._id_attrs = (self.phone_number,)
5155

telegram/inline/inlinequeryresultcontact.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class InlineQueryResultContact(InlineQueryResult):
3333
phone_number (:obj:`str`): Contact's phone number.
3434
first_name (:obj:`str`): Contact's first name.
3535
last_name (:obj:`str`): Optional. Contact's last name.
36+
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard,
37+
0-2048 bytes.
3638
reply_markup (:class:`telegram.InlineKeyboardMarkup`): Optional. Inline keyboard attached
3739
to the message.
3840
input_message_content (:class:`telegram.InputMessageContent`): Optional. Content of the
@@ -46,6 +48,8 @@ class InlineQueryResultContact(InlineQueryResult):
4648
phone_number (:obj:`str`): Contact's phone number.
4749
first_name (:obj:`str`): Contact's first name.
4850
last_name (:obj:`str`, optional): Contact's last name.
51+
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
52+
0-2048 bytes.
4953
reply_markup (:class:`telegram.InlineKeyboardMarkup`, optional): Inline keyboard attached
5054
to the message.
5155
input_message_content (:class:`telegram.InputMessageContent`, optional): Content of the
@@ -67,6 +71,7 @@ def __init__(self,
6771
thumb_url=None,
6872
thumb_width=None,
6973
thumb_height=None,
74+
vcard=None,
7075
**kwargs):
7176
# Required
7277
super(InlineQueryResultContact, self).__init__('contact', id)
@@ -76,6 +81,8 @@ def __init__(self,
7681
# Optionals
7782
if last_name:
7883
self.last_name = last_name
84+
if vcard:
85+
self.vcard = vcard
7986
if reply_markup:
8087
self.reply_markup = reply_markup
8188
if input_message_content:

telegram/inline/inputcontactmessagecontent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ class InputContactMessageContent(InputMessageContent):
2828
phone_number (:obj:`str`): Contact's phone number.
2929
first_name (:obj:`str`): Contact's first name.
3030
last_name (:obj:`str`): Optional. Contact's last name.
31+
vcard (:obj:`str`): Optional. Additional data about the contact in the form of a vCard,
32+
0-2048 bytes.
3133
3234
Args:
3335
phone_number (:obj:`str`): Contact's phone number.
3436
first_name (:obj:`str`): Contact's first name.
3537
last_name (:obj:`str`, optional): Contact's last name.
38+
vcard (:obj:`str`, optional): Additional data about the contact in the form of a vCard,
39+
0-2048 bytes.
3640
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
3741
3842
"""
3943

40-
def __init__(self, phone_number, first_name, last_name=None, **kwargs):
44+
def __init__(self, phone_number, first_name, last_name=None, vcard=None, **kwargs):
4145
# Required
4246
self.phone_number = phone_number
4347
self.first_name = first_name
4448
# Optionals
4549
self.last_name = last_name
50+
self.vcard = vcard

tests/test_bot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,15 @@ def test_send_contact(self, bot, chat_id):
133133
phone_number = '+11234567890'
134134
first_name = 'Leandro'
135135
last_name = 'Toledo'
136+
vcard = 'vCard'
136137
message = bot.send_contact(chat_id=chat_id, phone_number=phone_number,
137-
first_name=first_name, last_name=last_name)
138+
first_name=first_name, last_name=last_name, vcard=vcard)
138139

139140
assert message.contact
140141
assert message.contact.phone_number == phone_number
141142
assert message.contact.first_name == first_name
142143
assert message.contact.last_name == last_name
144+
assert message.contact.vcard == vcard
143145

144146
@pytest.mark.skipif(os.getenv('APPVEYOR'), reason='No game made for Appveyor bot (''yet)')
145147
@flaky(3, 1)

tests/test_contact.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
@pytest.fixture(scope='class')
2626
def contact():
2727
return Contact(TestContact.phone_number, TestContact.first_name, TestContact.last_name,
28-
TestContact.user_id)
28+
TestContact.user_id, TestContact.vcard)
2929

3030

3131
class TestContact(object):
3232
phone_number = '+11234567890'
3333
first_name = 'Leandro'
3434
last_name = 'Toledo'
3535
user_id = 23
36+
vcard = 'vCard'
3637

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

4445
def test_de_json_all(self, bot):
4546
json_dict = {'phone_number': self.phone_number, 'first_name': self.first_name,
46-
'last_name': self.last_name, 'user_id': self.user_id}
47+
'last_name': self.last_name, 'user_id': self.user_id,
48+
'vcard': self.vcard}
4749
contact = Contact.de_json(json_dict, bot)
4850

4951
assert contact.phone_number == self.phone_number
5052
assert contact.first_name == self.first_name
5153
assert contact.last_name == self.last_name
5254
assert contact.user_id == self.user_id
55+
assert contact.vcard == self.vcard
5356

5457
def test_send_with_contact(self, monkeypatch, bot, chat_id, contact):
5558
def test(_, url, data, **kwargs):
5659
phone = data['phone_number'] == contact.phone_number
5760
first = data['first_name'] == contact.first_name
5861
last = data['last_name'] == contact.last_name
59-
return phone and first and last
62+
vcard = data['vcard'] == contact.vcard
63+
return phone and first and last and vcard
6064

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

7883
def test_equality(self):
7984
a = Contact(self.phone_number, self.first_name)

tests/test_inlinequeryresultcontact.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def inline_query_result_contact():
3030
TestInlineQueryResultContact.phone_number,
3131
TestInlineQueryResultContact.first_name,
3232
last_name=TestInlineQueryResultContact.last_name,
33+
vcard=TestInlineQueryResultContact.vcard,
3334
thumb_url=TestInlineQueryResultContact.thumb_url,
3435
thumb_width=TestInlineQueryResultContact.thumb_width,
3536
thumb_height=TestInlineQueryResultContact.thumb_height,
@@ -43,6 +44,7 @@ class TestInlineQueryResultContact(object):
4344
phone_number = 'phone_number'
4445
first_name = 'first_name'
4546
last_name = 'last_name'
47+
vcard = 'vCard'
4648
thumb_url = 'thumb url'
4749
thumb_width = 10
4850
thumb_height = 15
@@ -55,6 +57,7 @@ def test_expected_values(self, inline_query_result_contact):
5557
assert inline_query_result_contact.phone_number == self.phone_number
5658
assert inline_query_result_contact.first_name == self.first_name
5759
assert inline_query_result_contact.last_name == self.last_name
60+
assert inline_query_result_contact.vcard == self.vcard
5861
assert inline_query_result_contact.thumb_url == self.thumb_url
5962
assert inline_query_result_contact.thumb_width == self.thumb_width
6063
assert inline_query_result_contact.thumb_height == self.thumb_height
@@ -74,6 +77,8 @@ def test_to_dict(self, inline_query_result_contact):
7477
inline_query_result_contact.first_name)
7578
assert (inline_query_result_contact_dict['last_name'] ==
7679
inline_query_result_contact.last_name)
80+
assert (inline_query_result_contact_dict['vcard'] ==
81+
inline_query_result_contact.vcard)
7782
assert (inline_query_result_contact_dict['thumb_url'] ==
7883
inline_query_result_contact.thumb_url)
7984
assert (inline_query_result_contact_dict['thumb_width'] ==

tests/test_inputcontactmessagecontent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@
2626
def input_contact_message_content():
2727
return InputContactMessageContent(TestInputContactMessageContent.phone_number,
2828
TestInputContactMessageContent.first_name,
29-
last_name=TestInputContactMessageContent.last_name)
29+
last_name=TestInputContactMessageContent.last_name,
30+
vcard=TestInputContactMessageContent.vcard)
3031

3132

3233
class TestInputContactMessageContent(object):
3334
phone_number = 'phone number'
3435
first_name = 'first name'
3536
last_name = 'last name'
37+
vcard = 'vCard'
3638

3739
def test_expected_values(self, input_contact_message_content):
3840
assert input_contact_message_content.first_name == self.first_name
3941
assert input_contact_message_content.phone_number == self.phone_number
4042
assert input_contact_message_content.last_name == self.last_name
43+
assert input_contact_message_content.vcard == self.vcard
4144

4245
def test_to_dict(self, input_contact_message_content):
4346
input_contact_message_content_dict = input_contact_message_content.to_dict()
@@ -49,3 +52,5 @@ def test_to_dict(self, input_contact_message_content):
4952
input_contact_message_content.first_name)
5053
assert (input_contact_message_content_dict['last_name'] ==
5154
input_contact_message_content.last_name)
55+
assert (input_contact_message_content_dict['vcard'] ==
56+
input_contact_message_content.vcard)

0 commit comments

Comments
 (0)