Skip to content

Commit

Permalink
Merge pull request #1902 from coder2020official/botapi6.5
Browse files Browse the repository at this point in the history
Bot API 6.5 update 🔥
  • Loading branch information
coder2020official authored Feb 5, 2023
2 parents fdd82a5 + 4179e50 commit 4056757
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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#december-30-2022">6.4</a>!
## <p align="center">Supported Bot API version: <a href="https://core.telegram.org/bots/api#february-3-2023">6.5</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
49 changes: 41 additions & 8 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2917,14 +2917,19 @@ def restrict_chat_member(
can_add_web_page_previews: Optional[bool]=None,
can_change_info: Optional[bool]=None,
can_invite_users: Optional[bool]=None,
can_pin_messages: Optional[bool]=None) -> bool:
can_pin_messages: Optional[bool]=None,
permissions: Optional[types.ChatPermissions]=None,
use_independent_chat_permissions: Optional[bool]=None) -> bool:
"""
Use this method to restrict a user in a supergroup.
The bot must be an administrator in the supergroup for this to work and must have
the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user.
Telegram documentation: https://core.telegram.org/bots/api#restrictchatmember
.. warning::
Individual parameters are deprecated and will be removed, use 'permissions' instead.
:param chat_id: Unique identifier for the target group or username of the target supergroup
or channel (in the format @channelusername)
:type chat_id: :obj:`int` or :obj:`str`
Expand Down Expand Up @@ -2965,15 +2970,36 @@ def restrict_chat_member(
:param can_pin_messages: Pass True, if the user is allowed to pin messages. Ignored in public supergroups
:type can_pin_messages: :obj:`bool`
:param use_independent_chat_permissions: Pass True if chat permissions are set independently. Otherwise,
the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages,
can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and
can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission.
:type use_independent_chat_permissions: :obj:`bool`
:param permissions: Pass ChatPermissions object to set all permissions at once. Use this param instead of
passing all boolean parameters.
:type permissions: :class:`telebot.types.ChatPermissions`
:return: True on success
:rtype: :obj:`bool`
"""

if permissions is None:
permissions = types.ChatPermissions(
can_send_messages=can_send_messages,
can_send_media_messages=can_send_media_messages,
can_send_polls=can_send_polls,
can_send_other_messages=can_send_other_messages,
can_add_web_page_previews=can_add_web_page_previews,
can_change_info=can_change_info,
can_invite_users=can_invite_users,
can_pin_messages=can_pin_messages
)
logger.warning(
"Individual parameters are deprecated and will be removed, use 'permissions' instead."
)
return apihelper.restrict_chat_member(
self.token, chat_id, user_id, until_date,
can_send_messages, can_send_media_messages,
can_send_polls, can_send_other_messages,
can_add_web_page_previews, can_change_info,
can_invite_users, can_pin_messages)
self.token, chat_id, user_id, permissions, until_date, use_independent_chat_permissions)

def promote_chat_member(
self, chat_id: Union[int, str], user_id: int,
Expand Down Expand Up @@ -3131,7 +3157,8 @@ def unban_chat_sender_chat(self, chat_id: Union[int, str], sender_chat_id: Union
return apihelper.unban_chat_sender_chat(self.token, chat_id, sender_chat_id)

def set_chat_permissions(
self, chat_id: Union[int, str], permissions: types.ChatPermissions) -> bool:
self, chat_id: Union[int, str], permissions: types.ChatPermissions,
use_independent_chat_permissions: Optional[bool]=None) -> bool:
"""
Use this method to set default chat permissions for all members.
The bot must be an administrator in the group or a supergroup for this to work
Expand All @@ -3146,10 +3173,16 @@ def set_chat_permissions(
:param permissions: New default chat permissions
:type permissions: :class:`telebot.types..ChatPermissions`
:param use_independent_chat_permissions: Pass True if chat permissions are set independently. Otherwise,
the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages,
can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and
can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission.
:type use_independent_chat_permissions: :obj:`bool`
:return: True on success
:rtype: :obj:`bool`
"""
return apihelper.set_chat_permissions(self.token, chat_id, permissions)
return apihelper.set_chat_permissions(self.token, chat_id, permissions, use_independent_chat_permissions)

def create_chat_invite_link(
self, chat_id: Union[int, str],
Expand Down
35 changes: 10 additions & 25 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,36 +968,19 @@ def unban_chat_member(token, chat_id, user_id, only_if_banned):


def restrict_chat_member(
token, chat_id, user_id, until_date=None,
can_send_messages=None, can_send_media_messages=None,
can_send_polls=None, can_send_other_messages=None,
can_add_web_page_previews=None, can_change_info=None,
can_invite_users=None, can_pin_messages=None):
token, chat_id, user_id, permissions, until_date=None,
use_independent_chat_permissions=None):
method_url = 'restrictChatMember'
permissions = {}
if can_send_messages is not None:
permissions['can_send_messages'] = can_send_messages
if can_send_media_messages is not None:
permissions['can_send_media_messages'] = can_send_media_messages
if can_send_polls is not None:
permissions['can_send_polls'] = can_send_polls
if can_send_other_messages is not None:
permissions['can_send_other_messages'] = can_send_other_messages
if can_add_web_page_previews is not None:
permissions['can_add_web_page_previews'] = can_add_web_page_previews
if can_change_info is not None:
permissions['can_change_info'] = can_change_info
if can_invite_users is not None:
permissions['can_invite_users'] = can_invite_users
if can_pin_messages is not None:
permissions['can_pin_messages'] = can_pin_messages
permissions_json = json.dumps(permissions)
payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions_json}
payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions.to_json()}

if use_independent_chat_permissions is not None:
permissions['use_independent_chat_permissions'] = use_independent_chat_permissions
if until_date is not None:
if isinstance(until_date, datetime):
payload['until_date'] = until_date.timestamp()
else:
payload['until_date'] = until_date

return _make_request(token, method_url, params=payload, method='post')


Expand Down Expand Up @@ -1056,12 +1039,14 @@ def unban_chat_sender_chat(token, chat_id, sender_chat_id):
return _make_request(token, method_url, params=payload, method='post')


def set_chat_permissions(token, chat_id, permissions):
def set_chat_permissions(token, chat_id, permissions, use_independent_chat_permissions=None):
method_url = 'setChatPermissions'
payload = {
'chat_id': chat_id,
'permissions': permissions.to_json()
}
if use_independent_chat_permissions is not None:
payload['use_independent_chat_permissions'] = use_independent_chat_permissions
return _make_request(token, method_url, params=payload, method='post')


Expand Down
48 changes: 40 additions & 8 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3775,14 +3775,19 @@ async def restrict_chat_member(
can_add_web_page_previews: Optional[bool]=None,
can_change_info: Optional[bool]=None,
can_invite_users: Optional[bool]=None,
can_pin_messages: Optional[bool]=None) -> bool:
can_pin_messages: Optional[bool]=None,
permissions: Optional[types.ChatPermissions]=None,
use_independent_chat_permissions: Optional[bool]=None) -> bool:
"""
Use this method to restrict a user in a supergroup.
The bot must be an administrator in the supergroup for this to work and must have
the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user.
Telegram documentation: https://core.telegram.org/bots/api#restrictchatmember
.. warning::
Individual parameters are deprecated and will be removed, use 'permissions' instead
:param chat_id: Unique identifier for the target group or username of the target supergroup
or channel (in the format @channelusername)
:type chat_id: :obj:`int` or :obj:`str`
Expand Down Expand Up @@ -3823,15 +3828,35 @@ async def restrict_chat_member(
:param can_pin_messages: Pass True, if the user is allowed to pin messages. Ignored in public supergroups
:type can_pin_messages: :obj:`bool`
:param use_independent_chat_permissions: Pass True if chat permissions are set independently. Otherwise,
the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages,
can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and
can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission.
:type use_independent_chat_permissions: :obj:`bool`
:param permissions: Pass ChatPermissions object to set all permissions at once. Use this parameter instead of
passing all boolean parameters to avoid backward compatibility problems in future.
:type permissions: :obj:`types.ChatPermissions`
:return: True on success
:rtype: :obj:`bool`
"""
if permissions is None:
permissions = types.ChatPermissions(
can_send_messages=can_send_messages,
can_send_media_messages=can_send_media_messages,
can_send_polls=can_send_polls,
can_send_other_messages=can_send_other_messages,
can_add_web_page_previews=can_add_web_page_previews,
can_change_info=can_change_info,
can_invite_users=can_invite_users,
can_pin_messages=can_pin_messages
)
logger.warning(
"Individual parameters are deprecated and will be removed, use 'permissions' instead."
)
return await asyncio_helper.restrict_chat_member(
self.token, chat_id, user_id, until_date,
can_send_messages, can_send_media_messages,
can_send_polls, can_send_other_messages,
can_add_web_page_previews, can_change_info,
can_invite_users, can_pin_messages)
self.token, chat_id, user_id, permissions, until_date, use_independent_chat_permissions)

async def promote_chat_member(
self, chat_id: Union[int, str], user_id: int,
Expand Down Expand Up @@ -3991,7 +4016,8 @@ async def unban_chat_sender_chat(self, chat_id: Union[int, str], sender_chat_id:
return await asyncio_helper.unban_chat_sender_chat(self.token, chat_id, sender_chat_id)

async def set_chat_permissions(
self, chat_id: Union[int, str], permissions: types.ChatPermissions) -> bool:
self, chat_id: Union[int, str], permissions: types.ChatPermissions,
use_independent_chat_permissions: Optional[bool]=None) -> bool:
"""
Use this method to set default chat permissions for all members.
The bot must be an administrator in the group or a supergroup for this to work
Expand All @@ -4006,10 +4032,16 @@ async def set_chat_permissions(
:param permissions: New default chat permissions
:type permissions: :class:`telebot.types..ChatPermissions`
:param use_independent_chat_permissions: Pass True if chat permissions are set independently. Otherwise,
the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages,
can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and
can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission.
:type use_independent_chat_permissions: :obj:`bool`
:return: True on success
:rtype: :obj:`bool`
"""
return await asyncio_helper.set_chat_permissions(self.token, chat_id, permissions)
return await asyncio_helper.set_chat_permissions(self.token, chat_id, permissions, use_independent_chat_permissions)

async def create_chat_invite_link(
self, chat_id: Union[int, str],
Expand Down
34 changes: 9 additions & 25 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,31 +960,13 @@ async def unban_chat_member(token, chat_id, user_id, only_if_banned):


async def restrict_chat_member(
token, chat_id, user_id, until_date=None,
can_send_messages=None, can_send_media_messages=None,
can_send_polls=None, can_send_other_messages=None,
can_add_web_page_previews=None, can_change_info=None,
can_invite_users=None, can_pin_messages=None):
token, chat_id, user_id, permissions, until_date=None,
use_independent_chat_permissions=None):
method_url = 'restrictChatMember'
permissions = {}
if can_send_messages is not None:
permissions['can_send_messages'] = can_send_messages
if can_send_media_messages is not None:
permissions['can_send_media_messages'] = can_send_media_messages
if can_send_polls is not None:
permissions['can_send_polls'] = can_send_polls
if can_send_other_messages is not None:
permissions['can_send_other_messages'] = can_send_other_messages
if can_add_web_page_previews is not None:
permissions['can_add_web_page_previews'] = can_add_web_page_previews
if can_change_info is not None:
permissions['can_change_info'] = can_change_info
if can_invite_users is not None:
permissions['can_invite_users'] = can_invite_users
if can_pin_messages is not None:
permissions['can_pin_messages'] = can_pin_messages
permissions_json = json.dumps(permissions)
payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions_json}
payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions.to_json()}

if use_independent_chat_permissions is not None:
permissions['use_independent_chat_permissions'] = use_independent_chat_permissions
if until_date is not None:
if isinstance(until_date, datetime):
payload['until_date'] = until_date.timestamp()
Expand Down Expand Up @@ -1046,12 +1028,14 @@ async def unban_chat_sender_chat(token, chat_id, sender_chat_id):
payload = {'chat_id': chat_id, 'sender_chat_id': sender_chat_id}
return await _process_request(token, method_url, params=payload, method='post')

async def set_chat_permissions(token, chat_id, permissions):
async def set_chat_permissions(token, chat_id, permissions, use_independent_chat_permissions=None):
method_url = 'setChatPermissions'
payload = {
'chat_id': chat_id,
'permissions': permissions.to_json()
}
if use_independent_chat_permissions is not None:
payload['use_independent_chat_permissions'] = use_independent_chat_permissions
return await _process_request(token, method_url, params=payload, method='post')


Expand Down
Loading

0 comments on commit 4056757

Please sign in to comment.