Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d1d38c

Browse files
Eldinnietsnoam
authored andcommitted
send_* now accepts tg-objects (python-telegram-bot#742)
Fixes python-telegram-bot#731
1 parent 2d1028a commit 8d1d38c

14 files changed

+223
-60
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Changes
1313
- Remove deprecated ``Botan`` import from ``utils`` (``Botan`` is still available through ``contrib``).
1414
- Remove deprecated ``ReplyKeyboardHide``.
1515
- Remove deprecated ``edit_message`` argument of `bot.set_game_score``.
16+
- Add the possibility to add objects as arguments to send_* methods.
1617

1718
**2017-06-18**
1819

telegram/bot.py

+120-45
Large diffs are not rendered by default.

telegram/files/sticker.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class MaskPosition(TelegramObject):
149149
size, from left to right.
150150
y_shift (:obj:`float`): Shift by Y-axis measured in heights of the mask scaled to the face
151151
size, from top to bottom.
152-
zoom (:obj:`float`): Mask scaling coefficient. For example, 2.0 means double size.
152+
scale (:obj:`float`): Mask scaling coefficient. For example, 2.0 means double size.
153153
154154
Notes:
155155
:attr:`type` should be one of the following: `forehead`, `eyes`, `mouth` or `chin`. You can
@@ -163,7 +163,7 @@ class MaskPosition(TelegramObject):
163163
y_shift (:obj:`float`): Shift by Y-axis measured in heights of the mask scaled to the face
164164
size, from top to bottom. For example, 1.0 will place the mask just below the default
165165
mask position.
166-
zoom (:obj:`float`): Mask scaling coefficient. For example, 2.0 means double size.
166+
scale (:obj:`float`): Mask scaling coefficient. For example, 2.0 means double size.
167167
"""
168168

169169
FOREHEAD = 'forehead'
@@ -175,11 +175,11 @@ class MaskPosition(TelegramObject):
175175
CHIN = 'chin'
176176
""":obj:`str`: 'chin'"""
177177

178-
def __init__(self, point, x_shift, y_shift, zoom, **kwargs):
178+
def __init__(self, point, x_shift, y_shift, scale, **kwargs):
179179
self.point = point
180180
self.x_shift = x_shift
181181
self.y_shift = y_shift
182-
self.zoom = zoom
182+
self.scale = scale
183183

184184
@classmethod
185185
def de_json(cls, data, bot):

tests/test_audio.py

+8
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ def test_send_audio_resend(self):
155155

156156
self.assertEqual(audio, self.audio)
157157

158+
@flaky(3, 1)
159+
@timeout(10)
160+
def test_send_audio_with_audio(self):
161+
message = self._bot.send_audio(audio=self.audio, chat_id=self._chat_id)
162+
audio = message.audio
163+
164+
self.assertEqual(audio, self.audio)
165+
158166
def test_audio_de_json(self):
159167
audio = telegram.Audio.de_json(self.json_dict, self._bot)
160168

tests/test_contact.py

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def test_contact_de_json(self):
5353
self.assertEqual(contact.last_name, self.last_name)
5454
self.assertEqual(contact.user_id, self.user_id)
5555

56+
def test_send_contact_with_contact(self):
57+
con = telegram.Contact.de_json(self.json_dict, self._bot)
58+
message = self._bot.send_contact(contact=con, chat_id=self._chat_id)
59+
contact = message.contact
60+
61+
self.assertEqual(contact, con)
62+
5663
def test_contact_to_json(self):
5764
contact = telegram.Contact.de_json(self.json_dict, self._bot)
5865

tests/test_document.py

+9
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ def test_send_document_resend(self):
132132

133133
self.assertEqual(document, self.document)
134134

135+
@flaky(3, 1)
136+
@timeout(10)
137+
def test_send_document_with_document(self):
138+
message = self._bot.send_document(document=self.document, chat_id=self._chat_id)
139+
document = message.document
140+
141+
self.assertEqual(document, self.document)
142+
143+
135144
def test_document_de_json(self):
136145
document = telegram.Document.de_json(self.json_dict, self._bot)
137146

tests/test_location.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def test_send_location_explicit_args(self):
5555
self.assertEqual(location.latitude, self.latitude)
5656
self.assertEqual(location.longitude, self.longitude)
5757

58+
def test_send_location_with_location(self):
59+
loc = telegram.Location(longitude=self.longitude, latitude=self.latitude)
60+
message = self._bot.send_location(location=loc, chat_id=self._chat_id)
61+
location = message.location
62+
63+
self.assertEqual(location, loc)
64+
5865
def test_location_de_json(self):
5966
location = telegram.Location.de_json(self.json_dict, self._bot)
6067

@@ -78,19 +85,17 @@ def test_error_send_location_empty_args(self):
7885
json_dict['latitude'] = ''
7986
json_dict['longitude'] = ''
8087

81-
self.assertRaises(telegram.TelegramError,
82-
lambda: self._bot.sendLocation(chat_id=self._chat_id,
83-
**json_dict))
88+
with self.assertRaises(TypeError):
89+
self._bot.sendLocation(chat_id=self._chat_id, **json_dict)
8490

8591
def test_error_location_without_required_args(self):
8692
json_dict = self.json_dict
8793

8894
del (json_dict['latitude'])
8995
del (json_dict['longitude'])
9096

91-
self.assertRaises(TypeError,
92-
lambda: self._bot.sendLocation(chat_id=self._chat_id,
93-
**json_dict))
97+
with self.assertRaises(ValueError):
98+
self._bot.sendLocation(chat_id=self._chat_id, **json_dict)
9499

95100
@flaky(3, 1)
96101
def test_reply_location(self):

tests/test_official.py

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def check_method(h4):
6363
ignored |= {'filename'} # Undocumented
6464
elif name == 'setGameScore':
6565
ignored |= {'edit_message'} # TODO: Now deprecated, so no longer in telegrams docs
66+
elif name == 'sendContact':
67+
ignored |= {'contact'} # Added for ease of use
68+
elif name == 'sendLocation':
69+
ignored |= {'location'} # Added for ease of use
70+
elif name == 'sendVenue':
71+
ignored |= {'venue'} # Added for ease of use
6672

6773
logger.debug((sig.parameters.keys(), checked, ignored,
6874
sig.parameters.keys() - checked - ignored))

tests/test_photo.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def test_send_photo_bytesio_jpg_file(self):
181181
@flaky(3, 1)
182182
@timeout(10)
183183
def test_silent_send_photo(self):
184-
message = self._bot.sendPhoto(photo=self.photo_file, chat_id=self._chat_id, disable_notification=True)
184+
message = self._bot.sendPhoto(photo=self.photo_file, chat_id=self._chat_id,
185+
disable_notification=True)
185186
thumb, photo = message.photo
186187

187188
self.assertIsInstance(thumb, telegram.PhotoSize)
@@ -192,6 +193,15 @@ def test_silent_send_photo(self):
192193
self.assertIsInstance(photo.file_id, str)
193194
self.assertNotEqual(photo.file_id, '')
194195

196+
@flaky(3, 1)
197+
@timeout(10)
198+
def test_send_photo_with_photosize(self):
199+
message = self._bot.send_photo(photo=self.photo, chat_id=self._chat_id)
200+
thumb, photo = message.photo
201+
202+
self.assertEqual(photo, self.photo)
203+
self.assertEqual(thumb, self.thumb)
204+
195205
@flaky(3, 1)
196206
@timeout(10)
197207
def test_send_photo_resend(self):

tests/test_sticker.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ def test_sticker_de_json(self):
152152
self.assertEqual(sticker.emoji, self.emoji)
153153
self.assertEqual(sticker.file_size, self.sticker.file_size)
154154

155+
@flaky(3, 1)
156+
@timeout(10)
157+
def test_send_sticker_with_sticker(self):
158+
message = self._bot.send_sticker(sticker=self.sticker, chat_id=self._chat_id)
159+
sticker = message.sticker
160+
161+
self.assertEqual(sticker, self.sticker)
162+
163+
155164
def test_sticker_to_json(self):
156165
self.assertTrue(self.is_json(self.sticker.to_json()))
157166

@@ -288,13 +297,13 @@ def setUp(self):
288297
self.point = telegram.MaskPosition.EYES
289298
self.x_shift = -1
290299
self.y_shift = 1
291-
self.zoom = 2
300+
self.scale = 2
292301

293302
self.json_dict = {
294303
'point': self.point,
295304
'x_shift': self.x_shift,
296305
'y_shift': self.y_shift,
297-
'zoom': self.zoom
306+
'scale': self.scale
298307
}
299308

300309
def test_mask_position_de_json(self):
@@ -303,7 +312,7 @@ def test_mask_position_de_json(self):
303312
self.assertEqual(mask_position.point, self.point)
304313
self.assertEqual(mask_position.x_shift, self.x_shift)
305314
self.assertEqual(mask_position.y_shift, self.y_shift)
306-
self.assertEqual(mask_position.zoom, self.zoom)
315+
self.assertEqual(mask_position.scale, self.scale)
307316

308317
def test_mask_positiont_to_json(self):
309318
mask_position = telegram.MaskPosition.de_json(self.json_dict, self._bot)

tests/test_venue.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class VenueTest(BaseTest, unittest.TestCase):
3333
"""This object represents Tests for Telegram Venue."""
3434

3535
def setUp(self):
36-
self.location = telegram.Location(longitude=1., latitude=0.)
36+
self.location = telegram.Location(longitude=-46.788279, latitude=-23.691288)
3737
self.title = 'title'
3838
self._address = '_address'
3939
self.foursquare_id = 'foursquare id'
@@ -53,6 +53,13 @@ def test_venue_de_json(self):
5353
self.assertEqual(sticker.address, self._address)
5454
self.assertEqual(sticker.foursquare_id, self.foursquare_id)
5555

56+
def test_send_venue_with_venue(self):
57+
ven = telegram.Venue.de_json(self.json_dict, self._bot)
58+
message = self._bot.send_venue(chat_id=self._chat_id, venue=ven)
59+
venue = message.venue
60+
61+
self.assertEqual(venue, ven)
62+
5663
def test_venue_to_json(self):
5764
sticker = telegram.Venue.de_json(self.json_dict, self._bot)
5865

tests/test_video.py

+9
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ def test_send_video_resend(self):
144144
self.assertEqual(video.thumb, self.video.thumb)
145145
self.assertEqual(video.mime_type, self.video.mime_type)
146146

147+
@flaky(3, 1)
148+
@timeout(10)
149+
def test_send_video_with_video(self):
150+
message = self._bot.send_video(video=self.video, chat_id=self._chat_id)
151+
video = message.video
152+
153+
self.assertEqual(video, self.video)
154+
155+
147156
def test_video_de_json(self):
148157
video = telegram.Video.de_json(self.json_dict, self._bot)
149158

tests/test_videonote.py

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ def test_send_videonote_resend(self):
114114
self.assertEqual(videonote.thumb, self.videonote.thumb)
115115
self.assertEqual(videonote.file_size, self.videonote.file_size)
116116

117+
@flaky(3, 1)
118+
@timeout(10)
119+
def test_send_video_note_with_video_note(self):
120+
message = self._bot.send_video_note(video_note=self.videonote, chat_id=self._chat_id)
121+
video_note = message.video_note
122+
123+
self.assertEqual(video_note, self.videonote)
124+
117125
def test_videonote_de_json(self):
118126
videonote = telegram.VideoNote.de_json(self.json_dict, self._bot)
119127

tests/test_voice.py

+9
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ def test_send_voice_resend(self):
149149
self.assertEqual(voice.duration, self.voice.duration)
150150
self.assertEqual(voice.mime_type, self.voice.mime_type)
151151

152+
@flaky(3, 1)
153+
@timeout(10)
154+
def test_send_voice_with_voice(self):
155+
message = self._bot.send_voice(voice=self.voice, chat_id=self._chat_id)
156+
voice = message.voice
157+
158+
self.assertEqual(voice, self.voice)
159+
160+
152161
def test_voice_de_json(self):
153162
voice = telegram.Voice.de_json(self.json_dict, self._bot)
154163

0 commit comments

Comments
 (0)