Skip to content

Commit 3b60f7c

Browse files
authored
Merge pull request eternnoir#462 from heyyyoyy/Bot_Api_3.6
Bot Api 3.6
2 parents a1930e0 + 518c49f commit 3b60f7c

4 files changed

Lines changed: 113 additions & 22 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -486,67 +486,72 @@ def delete_message(self, chat_id, message_id):
486486
return apihelper.delete_message(self.token, chat_id, message_id)
487487

488488
def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
489-
disable_notification=None):
489+
parse_mode=None, disable_notification=None):
490490
"""
491491
Use this method to send photos.
492492
:param disable_notification:
493493
:param chat_id:
494494
:param photo:
495495
:param caption:
496+
:param parse_mode
496497
:param reply_to_message_id:
497498
:param reply_markup:
498499
:return: API reply.
499500
"""
500501
return types.Message.de_json(
501502
apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup,
502-
disable_notification))
503+
parse_mode, disable_notification))
503504

504505
def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None,
505-
reply_to_message_id=None,
506-
reply_markup=None, disable_notification=None, timeout=None):
506+
reply_to_message_id=None, reply_markup=None, parse_mode=None, disable_notification=None,
507+
timeout=None):
507508
"""
508509
Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format.
509510
:param chat_id:Unique identifier for the message recipient
510511
:param audio:Audio file to send.
511512
:param duration:Duration of the audio in seconds
512513
:param performer:Performer
513514
:param title:Track name
515+
:param parse_mode
514516
:param reply_to_message_id:If the message is a reply, ID of the original message
515517
:param reply_markup:
516518
:return: Message
517519
"""
518520
return types.Message.de_json(
519521
apihelper.send_audio(self.token, chat_id, audio, caption, duration, performer, title, reply_to_message_id,
520-
reply_markup, disable_notification, timeout))
522+
reply_markup, parse_mode, disable_notification, timeout))
521523

522524
def send_voice(self, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None,
523-
disable_notification=None, timeout=None):
525+
parse_mode=None, disable_notification=None, timeout=None):
524526
"""
525527
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.
526528
:param chat_id:Unique identifier for the message recipient.
527529
:param voice:
528530
:param duration:Duration of sent audio in seconds
529531
:param reply_to_message_id:
530532
:param reply_markup:
533+
:param parse_mode
531534
:return: Message
532535
"""
533536
return types.Message.de_json(
534537
apihelper.send_voice(self.token, chat_id, voice, caption, duration, reply_to_message_id, reply_markup,
535-
disable_notification, timeout))
538+
parse_mode, disable_notification, timeout))
536539

537540
def send_document(self, chat_id, data, reply_to_message_id=None, caption=None, reply_markup=None,
538-
disable_notification=None, timeout=None):
541+
parse_mode=None, disable_notification=None, timeout=None):
539542
"""
540543
Use this method to send general files.
541544
:param chat_id:
542545
:param data:
543546
:param reply_to_message_id:
544547
:param reply_markup:
548+
:param parse_mode:
549+
:param disable_notification:
545550
:return: API reply.
546551
"""
547552
return types.Message.de_json(
548553
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
549-
disable_notification, timeout, caption=caption))
554+
parse_mode, disable_notification, timeout, caption=caption))
550555

551556
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None,
552557
timeout=None):
@@ -563,20 +568,22 @@ def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=Non
563568
disable_notification, timeout))
564569

565570
def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
566-
disable_notification=None, timeout=None):
571+
parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None):
567572
"""
568573
Use this method to send video files, Telegram clients support mp4 videos.
569574
:param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id
570575
:param data: InputFile or String : Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram server
571576
:param duration: Integer : Duration of sent video in seconds
572577
:param caption: String : Video caption (may also be used when resending videos by file_id).
578+
:param parse_mode:
579+
:param supports_streaming:
573580
:param reply_to_message_id:
574581
:param reply_markup:
575582
:return:
576583
"""
577584
return types.Message.de_json(
578585
apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup,
579-
disable_notification, timeout))
586+
parse_mode, supports_streaming, disable_notification, timeout))
580587

581588
def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
582589
disable_notification=None, timeout=None):
@@ -903,9 +910,10 @@ def answer_shipping_query(self, shipping_query_id, ok, shipping_options=None, er
903910
def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=None):
904911
return apihelper.answer_pre_checkout_query(self.token, pre_checkout_query_id, ok, error_message)
905912

906-
def edit_message_caption(self, caption, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
913+
def edit_message_caption(self, caption, chat_id=None, message_id=None, inline_message_id=None,
914+
parse_mode=None, reply_markup=None):
907915
result = apihelper.edit_message_caption(self.token, caption, chat_id, message_id, inline_message_id,
908-
reply_markup)
916+
parse_mode, reply_markup)
909917
if type(result) == bool:
910918
return result
911919
return types.Message.de_json(result)

‎telebot/apihelper.py‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def forward_message(token, chat_id, from_chat_id, message_id, disable_notificati
236236

237237

238238
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
239-
disable_notification=None):
239+
parse_mode=None, disable_notification=None):
240240
method_url = r'sendPhoto'
241241
payload = {'chat_id': chat_id}
242242
files = None
@@ -250,6 +250,8 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
250250
payload['reply_to_message_id'] = reply_to_message_id
251251
if reply_markup:
252252
payload['reply_markup'] = _convert_markup(reply_markup)
253+
if parse_mode:
254+
payload['parse_mode'] = parse_mode
253255
if disable_notification:
254256
payload['disable_notification'] = disable_notification
255257
return _make_request(token, method_url, params=payload, files=files, method='post')
@@ -349,7 +351,7 @@ def send_chat_action(token, chat_id, action):
349351

350352

351353
def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
352-
disable_notification=None, timeout=None):
354+
parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None):
353355
method_url = r'sendVideo'
354356
payload = {'chat_id': chat_id}
355357
files = None
@@ -365,6 +367,10 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
365367
payload['reply_to_message_id'] = reply_to_message_id
366368
if reply_markup:
367369
payload['reply_markup'] = _convert_markup(reply_markup)
370+
if parse_mode:
371+
payload['parse_mode'] = parse_mode
372+
if supports_streaming:
373+
payload['supports_streaming'] = supports_streaming
368374
if disable_notification:
369375
payload['disable_notification'] = disable_notification
370376
if timeout:
@@ -373,7 +379,7 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
373379

374380

375381
def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None,
376-
disable_notification=None, timeout=None):
382+
parse_mode=None, disable_notification=None, timeout=None):
377383
method_url = r'sendVoice'
378384
payload = {'chat_id': chat_id}
379385
files = None
@@ -389,6 +395,8 @@ def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_mess
389395
payload['reply_to_message_id'] = reply_to_message_id
390396
if reply_markup:
391397
payload['reply_markup'] = _convert_markup(reply_markup)
398+
if parse_mode:
399+
payload['parse_mode'] = parse_mode
392400
if disable_notification:
393401
payload['disable_notification'] = disable_notification
394402
if timeout:
@@ -423,7 +431,7 @@ def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_m
423431

424432

425433
def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None,
426-
reply_markup=None, disable_notification=None, timeout=None):
434+
reply_markup=None, parse_mode=None, disable_notification=None, timeout=None):
427435
method_url = r'sendAudio'
428436
payload = {'chat_id': chat_id}
429437
files = None
@@ -443,15 +451,17 @@ def send_audio(token, chat_id, audio, caption=None, duration=None, performer=Non
443451
payload['reply_to_message_id'] = reply_to_message_id
444452
if reply_markup:
445453
payload['reply_markup'] = _convert_markup(reply_markup)
454+
if parse_mode:
455+
payload['parse_mode'] = parse_mode
446456
if disable_notification:
447457
payload['disable_notification'] = disable_notification
448458
if timeout:
449459
payload['connect-timeout'] = timeout
450460
return _make_request(token, method_url, params=payload, files=files, method='post')
451461

452462

453-
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None,
454-
timeout=None, caption=None):
463+
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, parse_mode=None,
464+
disable_notification=None, timeout=None, caption=None):
455465
method_url = get_method_by_type(data_type)
456466
payload = {'chat_id': chat_id}
457467
files = None
@@ -463,6 +473,8 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
463473
payload['reply_to_message_id'] = reply_to_message_id
464474
if reply_markup:
465475
payload['reply_markup'] = _convert_markup(reply_markup)
476+
if parse_mode and data_type == 'document':
477+
payload['parse_mode'] = parse_mode
466478
if disable_notification:
467479
payload['disable_notification'] = disable_notification
468480
if timeout:
@@ -604,7 +616,8 @@ def edit_message_text(token, text, chat_id=None, message_id=None, inline_message
604616
return _make_request(token, method_url, params=payload)
605617

606618

607-
def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
619+
def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_message_id=None,
620+
parse_mode=None, reply_markup=None):
608621
method_url = r'editMessageCaption'
609622
payload = {'caption': caption}
610623
if chat_id:
@@ -613,6 +626,8 @@ def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_m
613626
payload['message_id'] = message_id
614627
if inline_message_id:
615628
payload['inline_message_id'] = inline_message_id
629+
if parse_mode:
630+
payload['parse_mode'] = parse_mode
616631
if reply_markup:
617632
payload['reply_markup'] = _convert_markup(reply_markup)
618633
return _make_request(token, method_url, params=payload)

‎telebot/types.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ def de_json(cls, json_string):
366366
if 'successful_payment' in obj:
367367
opts['successful_payment'] = SuccessfulPayment.de_json(obj['successful_payment'])
368368
content_type = 'successful_payment'
369+
if 'connected_website' in obj:
370+
opts['connected_website'] = obj['connected_website']
371+
content_type = 'connected_website'
369372
return cls(message_id, from_user, date, chat, content_type, opts)
370373

371374
@classmethod
@@ -430,6 +433,7 @@ def __init__(self, message_id, from_user, date, chat, content_type, options):
430433
self.pinned_message = None
431434
self.invoice = None
432435
self.successful_payment = None
436+
self.connected_website = None
433437
for key in options:
434438
setattr(self, key, options[key])
435439

@@ -1975,10 +1979,11 @@ def to_dic(self):
19751979
# InputMedia
19761980

19771981
class InputMediaPhoto(JsonSerializable):
1978-
def __init__(self, media, caption=None):
1982+
def __init__(self, media, caption=None, parse_mode=None):
19791983
self.type = "photo"
19801984
self.media = media
19811985
self.caption = caption
1986+
self.parse_mode = parse_mode
19821987

19831988
def to_json(self):
19841989
return json.dumps(self.to_dic())
@@ -1988,17 +1993,22 @@ def to_dic(self):
19881993
if not util.is_string(self.media) else self.media}
19891994
if self.caption:
19901995
ret['caption'] = self.caption
1996+
if self.parse_mode:
1997+
ret['parse_mode'] = self.parse_mode
19911998
return ret
19921999

19932000

19942001
class InputMediaVideo(JsonSerializable):
1995-
def __init__(self, media, caption=None, width=None, height=None, duration=None):
2002+
def __init__(self, media, caption=None, parse_mode=None, width=None, height=None, duration=None,
2003+
supports_streaming=None):
19962004
self.type = "video"
19972005
self.media = media
19982006
self.caption = caption
2007+
self.parse_mode = parse_mode
19992008
self.width = width
20002009
self.height = height
20012010
self.duration = duration
2011+
self.supports_streaming = supports_streaming
20022012

20032013
def to_json(self):
20042014
return json.dumps(self.to_dic())
@@ -2008,10 +2018,14 @@ def to_dic(self):
20082018
if not util.is_string(self.media) else self.media}
20092019
if self.caption:
20102020
ret['caption'] = self.caption
2021+
if self.parse_mode:
2022+
ret['parse_mode'] = self.parse_mode
20112023
if self.width:
20122024
ret['width'] = self.width
20132025
if self.height:
20142026
ret['height'] = self.height
20152027
if self.duration:
20162028
ret['duration'] = self.duration
2029+
if self.supports_streaming:
2030+
ret['supports_streaming'] = self.supports_streaming
20172031
return ret

‎tests/test_telebot.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,57 @@ def test_send_media_group(self):
420420
assert len(result) == 2
421421
assert result[0].media_group_id is not None
422422
assert result[0].media_group_id == result[1].media_group_id
423+
424+
def test_send_media_group_local_files(self):
425+
photo = open('../examples/detailed_example/kitten.jpg', 'rb')
426+
video = open('./test_data/test_video.mp4', 'rb')
427+
tb = telebot.TeleBot(TOKEN)
428+
medias = [types.InputMediaPhoto(photo, "View"),
429+
types.InputMediaVideo(video)]
430+
result = tb.send_media_group(CHAT_ID, medias)
431+
assert len(result) == 2
432+
assert result[0].media_group_id is not None
433+
assert result[1].media_group_id is not None
434+
435+
def test_send_photo_formating_caption(self):
436+
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
437+
tb = telebot.TeleBot(TOKEN)
438+
ret_msg = tb.send_photo(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
439+
assert ret_msg.caption_entities[0].type == 'italic'
440+
441+
def test_send_video_formatting_caption(self):
442+
file_data = open('./test_data/test_video.mp4', 'rb')
443+
tb = telebot.TeleBot(TOKEN)
444+
ret_msg = tb.send_video(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
445+
assert ret_msg.caption_entities[0].type == 'italic'
446+
447+
def test_send_audio_formatting_caption(self):
448+
file_data = open('./test_data/record.mp3', 'rb')
449+
tb = telebot.TeleBot(TOKEN)
450+
ret_msg = tb.send_audio(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
451+
assert ret_msg.caption_entities[0].type == 'bold'
452+
453+
def test_send_voice_formatting_caprion(self):
454+
file_data = open('./test_data/record.ogg', 'rb')
455+
tb = telebot.TeleBot(TOKEN)
456+
ret_msg = tb.send_voice(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
457+
assert ret_msg.caption_entities[0].type == 'bold'
458+
assert ret_msg.voice.mime_type == 'audio/ogg'
459+
460+
def test_send_media_group_formatting_caption(self):
461+
tb = telebot.TeleBot(TOKEN)
462+
img1 = 'https://i.imgur.com/CjXjcnU.png'
463+
img2 = 'https://i.imgur.com/CjXjcnU.png'
464+
medias = [types.InputMediaPhoto(img1, "*View*", parse_mode='Markdown'),
465+
types.InputMediaPhoto(img2, "_Dog_", parse_mode='Markdown')]
466+
result = tb.send_media_group(CHAT_ID, medias)
467+
assert len(result) == 2
468+
assert result[0].media_group_id is not None
469+
assert result[0].caption_entities[0].type == 'bold'
470+
assert result[1].caption_entities[0].type == 'italic'
471+
472+
def test_send_document_formating_caption(self):
473+
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
474+
tb = telebot.TeleBot(TOKEN)
475+
ret_msg = tb.send_document(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
476+
assert ret_msg.caption_entities[0].type == 'italic'

0 commit comments

Comments
 (0)