Skip to content

Commit 5ffd9c5

Browse files
committed
Merge remote-tracking branch 'refs/remotes/eternnoir/master'
2 parents 5969a66 + 2d10793 commit 5ffd9c5

5 files changed

Lines changed: 371 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ To start the bot, simply open up a terminal and enter `python echo_bot.py` to ru
123123
All types are defined in types.py. They are all completely in line with the [Telegram API's definition of the types](https://core.telegram.org/bots/api#available-types), except for the Message's `from` field, which is renamed to `from_user` (because `from` is a Python reserved token). Thus, attributes such as `message_id` can be accessed directly with `message.message_id`. Note that `message.chat` can be either an instance of `User` or `GroupChat` (see [How can I distinguish a User and a GroupChat in message.chat?](#how-can-i-distinguish-a-user-and-a-groupchat-in-messagechat)).
124124

125125
The Message object also has a `content_type`attribute, which defines the type of the Message. `content_type` can be one of the following strings:
126-
`text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `location`, `contact`, `new_chat_member`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`, `pinned_message`.
126+
`text`, `audio`, `document`, `photo`, `sticker`, `video`, `video_note`, `voice`, `location`, `contact`, `new_chat_member`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`, `pinned_message`.
127127

128128
### Methods
129129

@@ -284,6 +284,11 @@ video = open('/tmp/video.mp4', 'rb')
284284
tb.send_video(chat_id, video)
285285
tb.send_video(chat_id, "FILEID")
286286

287+
# sendVideoNote
288+
videonote = open('/tmp/videonote.mp4', 'rb')
289+
tb.send_video(chat_id, videonote)
290+
tb.send_video(chat_id, "FILEID")
291+
287292
# sendLocation
288293
tb.send_location(chat_id, lat, lon)
289294

‎telebot/__init__.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TeleBot:
3939
sendDocument
4040
sendSticker
4141
sendVideo
42+
sendVideoNote
4243
sendLocation
4344
sendChatAction
4445
getUserProfilePhotos
@@ -510,6 +511,22 @@ def send_video(self, chat_id, data, duration=None, caption=None, reply_to_messag
510511
apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup,
511512
disable_notification, timeout))
512513

514+
def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
515+
disable_notification=None, timeout=None):
516+
"""
517+
Use this method to send video files, Telegram clients support mp4 videos.
518+
:param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id
519+
:param data: InputFile or String : Video note to send. You can either pass a file_id as String to resend a video that is already on the Telegram server
520+
:param duration: Integer : Duration of sent video in seconds
521+
:param length: Integer : Video width and height, Can't be None and should be in range of (0, 640)
522+
:param reply_to_message_id:
523+
:param reply_markup:
524+
:return:
525+
"""
526+
return types.Message.de_json(
527+
apihelper.send_video_note(self.token, chat_id, data, duration, length, reply_to_message_id, reply_markup,
528+
disable_notification, timeout))
529+
513530
def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None,
514531
disable_notification=None):
515532
"""
@@ -559,7 +576,7 @@ def send_chat_action(self, chat_id, action):
559576
its typing status).
560577
:param chat_id:
561578
:param action: One of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
562-
'record_audio', 'upload_audio', 'upload_document', 'find_location'.
579+
'record_audio', 'upload_audio', 'upload_document', 'find_location', 'record_video_note', 'upload_video_note'.
563580
:return: API reply. :type: boolean
564581
"""
565582
return apihelper.send_chat_action(self.token, chat_id, action)
@@ -953,6 +970,10 @@ def send_sticker(self, *args, **kwargs):
953970
def send_video(self, *args, **kwargs):
954971
return TeleBot.send_video(self, *args, **kwargs)
955972

973+
@util.async()
974+
def send_video_note(self, *args, **kwargs):
975+
return TeleBot.send_video_note(self, *args, **kwargs)
976+
956977
@util.async()
957978
def send_location(self, *args, **kwargs):
958979
return TeleBot.send_location(self, *args, **kwargs)

‎telebot/apihelper.py‎

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,32 @@ def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_mess
328328
return _make_request(token, method_url, params=payload, files=files, method='post')
329329

330330

331+
def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
332+
disable_notification=None, timeout=None):
333+
method_url = r'sendVideoNote'
334+
payload = {'chat_id': chat_id}
335+
files = None
336+
if not util.is_string(data):
337+
files = {'video_note': data}
338+
else:
339+
payload['video_note'] = data
340+
if duration:
341+
payload['duration'] = duration
342+
if length:
343+
payload['length'] = length
344+
else:
345+
payload['length'] = 639 # seems like it is MAX length size
346+
if reply_to_message_id:
347+
payload['reply_to_message_id'] = reply_to_message_id
348+
if reply_markup:
349+
payload['reply_markup'] = _convert_markup(reply_markup)
350+
if disable_notification:
351+
payload['disable_notification'] = disable_notification
352+
if timeout:
353+
payload['connect-timeout'] = timeout
354+
return _make_request(token, method_url, params=payload, files=files, method='post')
355+
356+
331357
def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None,
332358
reply_markup=None, disable_notification=None, timeout=None):
333359
method_url = r'sendAudio'
@@ -446,7 +472,7 @@ def edit_message_reply_markup(token, chat_id=None, message_id=None, inline_messa
446472
return _make_request(token, method_url, params=payload)
447473

448474

449-
def delete_message(token, chat_id=None, message_id=None):
475+
def delete_message(token, chat_id, message_id):
450476
method_url = r'deleteMessage'
451477
payload = {'chat_id': chat_id, 'message_id': message_id}
452478
return _make_request(token, method_url, params=payload)
@@ -518,14 +544,109 @@ def get_game_high_scores(token, user_id, chat_id=None, message_id=None, inline_m
518544
payload['inline_message_id'] = inline_message_id
519545
return _make_request(token, method_url, params=payload)
520546

547+
# Payments (https://core.telegram.org/bots/api#payments)
548+
549+
def send_invoice(token, chat_id, title, description, invoice_payload, provider_token, currency, prices, start_parameter=None,
550+
photo_url=None, photo_size=None, photo_width=None, photo_height=None, need_name=None,
551+
need_phone_number=None, need_email=None, need_shipping_address=None, is_flexible=None,
552+
disable_notification=None, reply_to_message_id=None, reply_markup=None):
553+
"""
554+
Use this method to send invoices. On success, the sent Message is returned.
555+
:param token: Bot's token (you don't need to fill this)
556+
:param chat_id: Unique identifier for the target private chat
557+
:param title: Product name
558+
:param description: Product description
559+
:param payload: Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, use for your internal processes.
560+
:param provider_token: Payments provider token, obtained via @Botfather
561+
:param currency: Three-letter ISO 4217 currency code, see https://core.telegram.org/bots/payments#supported-currencies
562+
:param prices: Price breakdown, a list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.)
563+
:param start_parameter: Unique deep-linking parameter that can be used to generate this invoice when used as a start parameter
564+
:param photo_url: URL of the product photo for the invoice. Can be a photo of the goods or a marketing image for a service. People like it better when they see what they are paying for.
565+
:param photo_size: Photo size
566+
:param photo_width: Photo width
567+
:param photo_height: Photo height
568+
:param need_name: Pass True, if you require the user's full name to complete the order
569+
:param need_phone_number: Pass True, if you require the user's phone number to complete the order
570+
:param need_email: Pass True, if you require the user's email to complete the order
571+
:param need_shipping_address: Pass True, if you require the user's shipping address to complete the order
572+
:param is_flexible: Pass True, if the final price depends on the shipping method
573+
:param disable_notification: Sends the message silently. Users will receive a notification with no sound.
574+
:param reply_to_message_id: If the message is a reply, ID of the original message
575+
:param reply_markup: A JSON-serialized object for an inline keyboard. If empty, one 'Pay total price' button will be shown. If not empty, the first button must be a Pay button
576+
:return:
577+
"""
578+
method_url = r'sendInvoice'
579+
payload = {'chat_id': chat_id, 'title': title, 'description': description, 'payload': invoice_payload, 'provider_token': provider_token, 'currency': currency, 'prices': prices}
580+
if start_parameter:
581+
payload['start_parameter'] = start_parameter
582+
if photo_url:
583+
payload['photo_url'] = photo_url
584+
if photo_size:
585+
payload['photo_size'] = photo_size
586+
if photo_width:
587+
payload['photo_width'] = photo_width
588+
if photo_height:
589+
payload['photo_height'] = photo_height
590+
if need_name:
591+
payload['need_name'] = need_name
592+
if need_phone_number:
593+
payload['need_phone_number'] = need_phone_number
594+
if need_email:
595+
payload['need_email'] = need_email
596+
if need_shipping_address:
597+
payload['need_shipping_address'] = need_shipping_address
598+
if is_flexible:
599+
payload['is_flexible'] = is_flexible
600+
if disable_notification:
601+
payload['disable_notification'] = disable_notification
602+
if reply_to_message_id:
603+
payload['reply_to_message_id'] = reply_to_message_id
604+
if reply_markup:
605+
payload['reply_markup'] = reply_markup
606+
return _make_request(token, method_url, params=payload)
607+
608+
609+
def answer_shippingQuery(token, shipping_query_id, ok, shipping_options=None, error_message=None):
610+
"""
611+
If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries. On success, True is returned.
612+
:param token: Bot's token (you don't need to fill this)
613+
:param shipping_query_id: Unique identifier for the query to be answered
614+
:param ok: Specify True if delivery to the specified address is possible and False if there are any problems (for example, if delivery to the specified address is not possible)
615+
:param shipping_options: Required if ok is True. A JSON-serialized array of available shipping options.
616+
:param error_message: Required if ok is False. Error message in human readable form that explains why it is impossible to complete the order (e.g. "Sorry, delivery to your desired address is unavailable'). Telegram will display this message to the user.
617+
:return:
618+
"""
619+
method_url = 'answerShippingQuery'
620+
payload = {'shipping_query_id': shipping_query_id, 'ok': ok}
621+
if shipping_options:
622+
payload['reply_markup'] = shipping_options
623+
if error_message:
624+
payload['reply_markup'] = error_message
625+
return _make_request(token, method_url, params=payload)
626+
627+
628+
def answer_pre_checkout_query(token, pre_checkout_query_id, ok, error_message=None):
629+
"""
630+
Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, True is returned. Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
631+
:param token: Bot's token (you don't need to fill this)
632+
:param pre_checkout_query_id: Unique identifier for the query to be answered
633+
:param ok: Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems.
634+
:param error_message: Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user.
635+
:return:
636+
"""
637+
method_url = 'answerPreCheckoutQuery'
638+
payload = {'pre_checkout_query_id': pre_checkout_query_id, 'ok': ok}
639+
if error_message:
640+
payload['error_message'] = error_message
641+
return _make_request(token, method_url, params=payload)
642+
521643

522644
# InlineQuery
523645

524646
def answer_callback_query(token, callback_query_id, text=None, show_alert=None, url=None, cache_time=None):
525647
"""
526648
Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
527649
Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via BotFather and accept the terms. Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
528-
529650
:param token: Bot's token (you don't need to fill this)
530651
:param callback_query_id: Unique identifier for the query to be answered
531652
:param text: (Optional) Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters

0 commit comments

Comments
 (0)