Skip to content

Commit

Permalink
Merge pull request #2329 from coder2020official/botapi-76
Browse files Browse the repository at this point in the history
Bot API 7.6
  • Loading branch information
Badiboy authored Jul 4, 2024
2 parents f653a72 + 01f9a28 commit 134fda7
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<p align="center">A simple, but extensible Python implementation for the <a href="https://core.telegram.org/bots/api">Telegram Bot API</a>.</p>
<p align="center">Both synchronous and asynchronous.</p>

## <p align="center">Supported Bot API version: <a href="https://core.telegram.org/bots/api#june-18-2024">7.5</a>!
## <p align="center">Supported Bot API version: <a href="https://core.telegram.org/bots/api#july-1-2024">7.6</a>!

<h2><a href='https://pytba.readthedocs.io/en/latest/index.html'>Official documentation</a></h2>
<h2><a href='https://pytba.readthedocs.io/ru/latest/index.html'>Official ru documentation</a></h2>
Expand Down
118 changes: 88 additions & 30 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,9 @@ def copy_message(
show_caption_above_media: Optional[bool]=None) -> types.MessageID:
"""
Use this method to copy messages of any kind.
Service messages, paid media messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied.
A quiz poll can be copied only if the value of the field correct_option_id is known to the bot. The method is analogous to the method
forwardMessage, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success.
Telegram documentation: https://core.telegram.org/bots/api#copymessage
Expand Down Expand Up @@ -1999,47 +2002,47 @@ def forward_messages(self, chat_id: Union[str, int], from_chat_id: Union[str, in
def copy_messages(self, chat_id: Union[str, int], from_chat_id: Union[str, int], message_ids: List[int],
disable_notification: Optional[bool] = None, message_thread_id: Optional[int] = None,
protect_content: Optional[bool] = None, remove_caption: Optional[bool] = None) -> List[types.MessageID]:
"""
Use this method to copy messages of any kind. If some of the specified messages can't be found or copied, they are skipped.
Service messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied.
A quiz poll can be copied only if the value of the field correct_option_id is known to the bot.
The method is analogous to the method forwardMessages, but the copied messages don't have a link to the original message.
Album grouping is kept for copied messages. On success, an array of MessageId of the sent messages is returned.
"""
Use this method to copy messages of any kind.
If some of the specified messages can't be found or copied, they are skipped. Service messages, paid media messages, giveaway messages, giveaway winners messages,
and invoice messages can't be copied. A quiz poll can be copied only if the value of the field correct_option_id is known to the bot. The method is analogous
to the method forwardMessages, but the copied messages don't have a link to the original message. Album grouping is kept for copied messages. On success, an array
of MessageId of the sent messages is returned.
Telegram documentation: https://core.telegram.org/bots/api#copymessages
Telegram documentation: https://core.telegram.org/bots/api#copymessages
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:type chat_id: :obj:`int` or :obj:`str`
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:type chat_id: :obj:`int` or :obj:`str`
:param from_chat_id: Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
:type from_chat_id: :obj:`int` or :obj:`str`
:param from_chat_id: Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
:type from_chat_id: :obj:`int` or :obj:`str`
:param message_ids: Message identifiers in the chat specified in from_chat_id
:type message_ids: :obj:`list` of :obj:`int`
:param message_ids: Message identifiers in the chat specified in from_chat_id
:type message_ids: :obj:`list` of :obj:`int`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`bool`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`bool`
:param message_thread_id: Identifier of a message thread, in which the messages will be sent
:type message_thread_id: :obj:`int`
:param message_thread_id: Identifier of a message thread, in which the messages will be sent
:type message_thread_id: :obj:`int`
:param protect_content: Protects the contents of the forwarded message from forwarding and saving
:type protect_content: :obj:`bool`
:param protect_content: Protects the contents of the forwarded message from forwarding and saving
:type protect_content: :obj:`bool`
:param remove_caption: Pass True to copy the messages without their captions
:type remove_caption: :obj:`bool`
:param remove_caption: Pass True to copy the messages without their captions
:type remove_caption: :obj:`bool`
:return: On success, an array of MessageId of the sent messages is returned.
:rtype: :obj:`list` of :class:`telebot.types.MessageID`
"""
disable_notification = self.disable_notification if disable_notification is None else disable_notification
protect_content = self.protect_content if protect_content is None else protect_content
:return: On success, an array of MessageId of the sent messages is returned.
:rtype: :obj:`list` of :class:`telebot.types.MessageID`
"""
disable_notification = self.disable_notification if disable_notification is None else disable_notification
protect_content = self.protect_content if protect_content is None else protect_content

result = apihelper.copy_messages(
self.token, chat_id, from_chat_id, message_ids, disable_notification=disable_notification,
message_thread_id=message_thread_id, protect_content=protect_content, remove_caption=remove_caption)
return [types.MessageID.de_json(message_id) for message_id in result]
result = apihelper.copy_messages(
self.token, chat_id, from_chat_id, message_ids, disable_notification=disable_notification,
message_thread_id=message_thread_id, protect_content=protect_content, remove_caption=remove_caption)
return [types.MessageID.de_json(message_id) for message_id in result]


def send_dice(
Expand Down Expand Up @@ -3112,6 +3115,61 @@ def send_video_note(
protect_content=protect_content, message_thread_id=message_thread_id, reply_parameters=reply_parameters,
business_connection_id=business_connection_id, message_effect_id=message_effect_id)
)

def send_paid_media(
self, chat_id: Union[int, str], star_count: int, media: List[types.InputPaidMedia],
caption: Optional[str]=None, parse_mode: Optional[str]=None, caption_entities: Optional[List[types.MessageEntity]]=None,
show_caption_above_media: Optional[bool]=None, disable_notification: Optional[bool]=None,
protect_content: Optional[bool]=None, reply_parameters: Optional[types.ReplyParameters]=None,
reply_markup: Optional[REPLY_MARKUP_TYPES]=None) -> types.Message:
"""
Use this method to send paid media to channel chats. On success, the sent Message is returned.
Telegram documentation: https://core.telegram.org/bots/api#sendpaidmedia
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:type chat_id: :obj:`int` or :obj:`str`
:param star_count: The number of Telegram Stars that must be paid to buy access to the media
:type star_count: :obj:`int`
:param media: A JSON-serialized array describing the media to be sent; up to 10 items
:type media: :obj:`list` of :class:`telebot.types.InputPaidMedia`
:param caption: Media caption, 0-1024 characters after entities parsing
:type caption: :obj:`str`
:param parse_mode: Mode for parsing entities in the media caption
:type parse_mode: :obj:`str`
:param caption_entities: List of special entities that appear in the caption, which can be specified instead of parse_mode
:type caption_entities: :obj:`list` of :class:`telebot.types.MessageEntity`
:param show_caption_above_media: Pass True, if the caption must be shown above the message media
:type show_caption_above_media: :obj:`bool`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound.
:type disable_notification: :obj:`bool`
:param protect_content: Protects the contents of the sent message from forwarding and saving
:type protect_content: :obj:`bool`
:param reply_parameters: Description of the message to reply to
:type reply_parameters: :class:`telebot.types.ReplyParameters`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user
:type reply_markup: :class:`telebot.types.InlineKeyboardMarkup` or :class:`telebot.types.ReplyKeyboardMarkup` or :class:`telebot.types.ReplyKeyboardRemove` or :class:`telebot.types.ForceReply`
:return: On success, the sent Message is returned.
:rtype: :class:`telebot.types.Message`
"""
return types.Message.de_json(
apihelper.send_paid_media(
self.token, chat_id, star_count, media, caption=caption, parse_mode=parse_mode,
caption_entities=caption_entities, show_caption_above_media=show_caption_above_media,
disable_notification=disable_notification, protect_content=protect_content,
reply_parameters=reply_parameters, reply_markup=reply_markup)
)


def send_media_group(
Expand Down
30 changes: 29 additions & 1 deletion telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,34 @@ def send_photo(
if show_caption_above_media is not None:
payload['show_caption_above_media'] = show_caption_above_media
return _make_request(token, method_url, params=payload, files=files, method='post')

def send_paid_media(
token, chat_id, star_count, media,
caption=None, parse_mode=None, caption_entities=None, show_caption_above_media=None,
disable_notification=None, protect_content=None, reply_parameters=None, reply_markup=None):
method_url = r'sendPaidMedia'
media_json, files = convert_input_media_array(media)
payload = {'chat_id': chat_id, 'star_count': star_count, 'media': media_json}
if caption:
payload['caption'] = caption
if parse_mode:
payload['parse_mode'] = parse_mode
if caption_entities:
payload['caption_entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(caption_entities))
if show_caption_above_media is not None:
payload['show_caption_above_media'] = show_caption_above_media
if disable_notification is not None:
payload['disable_notification'] = disable_notification
if protect_content is not None:
payload['protect_content'] = protect_content
if reply_parameters is not None:
payload['reply_parameters'] = reply_parameters.to_json()
if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup)
return _make_request(
token, method_url, params=payload,
method='post' if files else 'get',
files=files if files else None)


def send_media_group(
Expand Down Expand Up @@ -2117,7 +2145,7 @@ def convert_input_media_array(array):
media = []
files = {}
for input_media in array:
if isinstance(input_media, types.InputMedia):
if isinstance(input_media, types.InputMedia) or isinstance(input_media, types.InputPaidMedia):
media_dict = input_media.to_dict()
if media_dict['media'].startswith('attach://'):
key = media_dict['media'].replace('attach://', '')
Expand Down
Loading

0 comments on commit 134fda7

Please sign in to comment.