Skip to content

Commit

Permalink
Added Copy,Delete,Forward Messages to sync and async
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Dec 29, 2023
1 parent a261174 commit c406e6c
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
91 changes: 91 additions & 0 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,97 @@ def delete_message(self, chat_id: Union[int, str], message_id: int,
:rtype: :obj:`bool`
"""
return apihelper.delete_message(self.token, chat_id, message_id, timeout)

def delete_messages(self, chat_id: Union[int, str], message_ids: List[int]):
"""
Use this method to delete multiple messages in a chat.
The number of messages to be deleted must not exceed 100.
If the chat is a private chat, the user must be an administrator of the chat for this to work and must have the appropriate admin rights.
Returns True on success.
Telegram documentation: https://core.telegram.org/bots/api#deletemessages
: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 message_ids: Identifiers of the messages to be deleted
:type message_ids: :obj:`list` of :obj:`int`
:return: Returns True on success.
"""
return apihelper.delete_messages(self.token, chat_id, message_ids)

def forward_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) -> List[types.MessageID]:
"""
Use this method to forward messages of any kind.
: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 message_ids: Message identifiers in the chat specified in from_chat_id
:type message_ids: :obj:`list`
: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 protect_content: Protects the contents of the forwarded message from forwarding and saving
:type protect_content: :obj:`bool`
:return: On success, the sent Message is returned.
:rtype: :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 types.MessageID.de_json(
apihelper.forward_messages(self.token, chat_id, from_chat_id, message_ids, disable_notification, protect_content, message_thread_id))

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.
: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 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 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 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 [types.MessageID.de_json(message_id) for message_id in
apihelper.copy_messages(self.token, chat_id, from_chat_id, message_ids, disable_notification,
protect_content, message_thread_id, remove_caption)]


def send_dice(
self, chat_id: Union[int, str],
Expand Down
46 changes: 46 additions & 0 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,52 @@ def unhide_general_forum_topic(token, chat_id):
payload = {'chat_id': chat_id}
return _make_request(token, method_url, params=payload)

def delete_messages(token, chat_id, message_ids):
method_url = 'deleteMessages'
payload = {
'chat_id': chat_id,
'message_ids': message_ids
}
return _make_request(token, method_url, params=payload)

def forward_messages(token, chat_id, from_chat_id, message_ids, disable_notification=None,
message_thread_id=None, protect_content=None):
method_url = 'forwardMessages'
payload = {
'chat_id': chat_id,
'from_chat_id': from_chat_id,
'message_ids': message_ids,
}
if disable_notification is not None:
payload['disable_notification'] = disable_notification
if message_thread_id is not None:
payload['message_thread_id'] = message_thread_id
if protect_content is not None:
payload['protect_content'] = protect_content

result = _make_request(token, method_url, params=payload)
return result

def copy_messages(token, chat_id, from_chat_id, message_ids, disable_notification=None,
message_thread_id=None, protect_content=None, remove_caption=None):
method_url = 'copyMessages'
payload = {
'chat_id': chat_id,
'from_chat_id': from_chat_id,
'message_ids': message_ids,
}
if disable_notification is not None:
payload['disable_notification'] = disable_notification
if message_thread_id is not None:
payload['message_thread_id'] = message_thread_id
if protect_content is not None:
payload['protect_content'] = protect_content
if remove_caption is not None:
payload['remove_caption'] = remove_caption

result = _make_request(token, method_url, params=payload)
return result


def _convert_list_json_serializable(results):
ret = ''
Expand Down
90 changes: 90 additions & 0 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,96 @@ async def delete_message(self, chat_id: Union[int, str], message_id: int,
:rtype: :obj:`bool`
"""
return await asyncio_helper.delete_message(self.token, chat_id, message_id, timeout)

async def delete_messages(self, chat_id: Union[int, str], message_ids: List[int]):
"""
Use this method to delete multiple messages in a chat.
The number of messages to be deleted must not exceed 100.
If the chat is a private chat, the user must be an administrator of the chat for this to work and must have the appropriate admin rights.
Returns True on success.
Telegram documentation: https://core.telegram.org/bots/api#deletemessages
: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 message_ids: Identifiers of the messages to be deleted
:type message_ids: :obj:`list` of :obj:`int`
:return: Returns True on success.
"""
return await asyncio_helper.delete_messages(self.token, chat_id, message_ids)

async def forward_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) -> List[types.MessageID]:
"""
Use this method to forward messages of any kind.
: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 message_ids: Message identifiers in the chat specified in from_chat_id
:type message_ids: :obj:`list`
: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 protect_content: Protects the contents of the forwarded message from forwarding and saving
:type protect_content: :obj:`bool`
:return: On success, the sent Message is returned.
:rtype: :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 = await asyncio_helper.forward_messages(self.token, chat_id, from_chat_id, message_ids, disable_notification, protect_content, message_thread_id)
return types.MessageID.de_json(
result)

async 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.
: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 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 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 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
result = await asyncio_helper.copy_messages(self.token, chat_id, from_chat_id, message_ids, disable_notification,
protect_content, message_thread_id, remove_caption)
return [types.MessageID.de_json(message_id) for message_id in
result]

async def send_dice(
self, chat_id: Union[int, str],
Expand Down
49 changes: 49 additions & 0 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,53 @@ async def unhide_general_forum_topic(token, chat_id):
payload = {'chat_id': chat_id}
return await _process_request(token, method_url, params=payload)

async def delete_messages(token, chat_id, message_ids):
method_url = 'deleteMessages'
payload = {
'chat_id': chat_id,
'message_ids': message_ids
}
return await _process_request(token, method_url, params=payload)

async def forward_messages(token, chat_id, from_chat_id, message_ids, disable_notification=None,
message_thread_id=None, protect_content=None):
method_url = 'forwardMessages'
payload = {
'chat_id': chat_id,
'from_chat_id': from_chat_id,
'message_ids': message_ids,
}
if disable_notification is not None:
payload['disable_notification'] = disable_notification
if message_thread_id is not None:
payload['message_thread_id'] = message_thread_id
if protect_content is not None:
payload['protect_content'] = protect_content

result = await _process_request(token, method_url, params=payload)
return result

async def copy_messages(token, chat_id, from_chat_id, message_ids, disable_notification=None,
message_thread_id=None, protect_content=None, remove_caption=None):
method_url = 'copyMessages'
payload = {
'chat_id': chat_id,
'from_chat_id': from_chat_id,
'message_ids': message_ids,
}
if disable_notification is not None:
payload['disable_notification'] = disable_notification
if message_thread_id is not None:
payload['message_thread_id'] = message_thread_id
if protect_content is not None:
payload['protect_content'] = protect_content
if remove_caption is not None:
payload['remove_caption'] = remove_caption

result = await _process_request(token, method_url, params=payload)
return result


async def _convert_list_json_serializable(results):
ret = ''
for r in results:
Expand Down Expand Up @@ -1999,3 +2046,5 @@ class RequestTimeout(Exception):
This class represents a request timeout.
"""
pass


0 comments on commit c406e6c

Please sign in to comment.