1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1208 from SwissCorePy/master

get_chat_member_count and ban_chat_member added.
get_chat_members_count and kick_chat_member are marked as deprecated.
This commit is contained in:
Badiboy 2021-06-28 13:09:19 +03:00 committed by GitHub
commit 6bc60f4aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 68 deletions

View File

@ -103,7 +103,7 @@ class TeleBot:
editMessageReplyMarkup
editMessageLiveLocation
stopMessageLiveLocation
kickChatMember
banChatMember
unbanChatMember
restrictChatMember
promoteChatMember
@ -131,11 +131,12 @@ class TeleBot:
leaveChat
getChat
getChatAdministrators
getChatMembersCount
getChatMemberCount
getChatMember
answerCallbackQuery
getMyCommands
setMyCommands
deleteMyCommands
answerInlineQuery
answerShippingQuery
answerPreCheckoutQuery
@ -837,12 +838,20 @@ class TeleBot:
return ret
def get_chat_members_count(self, chat_id: Union[int, str]) -> int:
"""
This function is deprecated. Use `get_chat_member_count` instead
"""
logger.info('get_chat_members_count is deprecated. Use get_chat_member_count instead.')
result = apihelper.get_chat_member_count(self.token, chat_id)
return result
def get_chat_member_count(self, chat_id: Union[int, str]) -> int:
"""
Use this method to get the number of members in a chat. Returns Int on success.
:param chat_id:
:return:
"""
result = apihelper.get_chat_members_count(self.token, chat_id)
result = apihelper.get_chat_member_count(self.token, chat_id)
return result
def set_chat_sticker_set(self, chat_id: Union[int, str], sticker_set_name: str) -> types.StickerSet:
@ -1463,7 +1472,20 @@ class TeleBot:
until_date:Optional[Union[int, datetime]]=None,
revoke_messages: Optional[bool]=None) -> bool:
"""
Use this method to kick a user from a group or a supergroup.
This function is deprecated. Use `ban_chat_member` instead
"""
logger.info('kick_chat_member is deprecated. Use ban_chat_member instead.')
return apihelper.ban_chat_member(self.token, chat_id, user_id, until_date, revoke_messages)
def ban_chat_member(
self, chat_id: Union[int, str], user_id: int,
until_date:Optional[Union[int, datetime]]=None,
revoke_messages: Optional[bool]=None) -> bool:
"""
Use this method to ban a user in a group, a supergroup or a channel.
In the case of supergroups and channels, the user will not be able to return to the chat on their
own using invite links, etc., unless unbanned first.
Returns True on success.
:param chat_id: Int or string : Unique identifier for the target group or username of the target supergroup
:param user_id: Int : Unique identifier of the target user
:param until_date: Date when the user will be unbanned, unix time. If user is banned for more than 366 days or
@ -1473,7 +1495,7 @@ class TeleBot:
Always True for supergroups and channels.
:return: boolean
"""
return apihelper.kick_chat_member(self.token, chat_id, user_id, until_date, revoke_messages)
return apihelper.ban_chat_member(self.token, chat_id, user_id, until_date, revoke_messages)
def unban_chat_member(
self, chat_id: Union[int, str], user_id: int,
@ -1699,54 +1721,49 @@ class TeleBot:
setting is off in the target group.
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:return:
"""
return apihelper.delete_chat_photo(self.token, chat_id)
def get_my_commands(self,
scope: Optional[Union[
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats,
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]]=None,
language_code: Optional[str]=None) -> List[types.BotCommand]:
def get_my_commands(self, scope: Optional[types.BotCommandScope],
language_code: Optional[str]) -> List[types.BotCommand]:
"""
Use this method to get the current list of the bot's commands for the given scope and user language
:param scope: scope of users for which the commands are relevant
:param language_code: A two-letter ISO 639-1 language code
Use this method to get the current list of the bot's commands.
Returns List of BotCommand on success.
:param scope: The scope of users for which the commands are relevant.
Defaults to BotCommandScopeDefault.
:param language_code: A two-letter ISO 639-1 language code. If empty,
commands will be applied to all users from the given scope,
for whose language there are no dedicated commands
"""
result = apihelper.get_my_commands(self.token, scope, language_code)
return [types.BotCommand.de_json(cmd) for cmd in result]
def set_my_commands(self, commands: List[types.BotCommand],
scope: Optional[Union[
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats,
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]] = None,
language_code: Optional[str]=None) -> bool:
def set_my_commands(self, commands: List[types.BotCommand],
scope: Optional[types.BotCommandScope]=None,
language_code: Optional[str]=None) -> bool:
"""
Use this method to change the list of the bot's commands.
:param commands: List of BotCommand. At most 100 commands can be specified.
:param scope: scope of users for which the commands are relevant
:param language_code: A two-letter ISO 639-1 language code
:param scope: The scope of users for which the commands are relevant.
Defaults to BotCommandScopeDefault.
:param language_code: A two-letter ISO 639-1 language code. If empty,
commands will be applied to all users from the given scope,
for whose language there are no dedicated commands
:return:
"""
return apihelper.set_my_commands(self.token, commands, scope, language_code)
def delete_my_commands(self,
scope: Optional[Union[
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats,
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]]=None,
language_code: Optional[str]=None) -> bool:
def delete_my_commands(self, scope: Optional[types.BotCommandScope]=None,
language_code: Optional[int]=None) -> bool:
"""
Use this method to delete the list of the bot's commands for the given scope and user language.
:param scope: scope of users for which the commands are relevant
:param language_code: A two-letter ISO 639-1 language code
:return:
Use this method to delete the list of the bot's commands for the given scope and user language.
After deletion, higher level commands will be shown to affected users.
Returns True on success.
:param scope: The scope of users for which the commands are relevant.
Defaults to BotCommandScopeDefault.
:param language_code: A two-letter ISO 639-1 language code. If empty,
commands will be applied to all users from the given scope,
for whose language there are no dedicated commands
"""
return apihelper.delete_my_commands(self.token, scope, language_code)
@ -2907,12 +2924,16 @@ class AsyncTeleBot(TeleBot):
return TeleBot.close(self)
@util.async_dec()
def get_my_commands(self):
return TeleBot.get_my_commands(self)
def get_my_commands(self, *args, **kwargs): # needed args because new scope and language_code
return TeleBot.get_my_commands(self, *args, **kwargs)
@util.async_dec()
def set_my_commands(self, *args, **kwargs):
return TeleBot.set_my_commands(self, *args, **kwargs)
@util.async_dec()
def delete_my_commands(self, *args, **kwargs):
return TeleBot.delete_my_commands(self, *args, **kwargs)
@util.async_dec()
def get_file(self, *args):
@ -2940,7 +2961,12 @@ class AsyncTeleBot(TeleBot):
@util.async_dec()
def get_chat_members_count(self, *args):
return TeleBot.get_chat_members_count(self, *args)
logger.info('get_chat_members_count is deprecated. Use get_chat_member_count instead')
return TeleBot.get_chat_member_count(self, *args)
@util.async_dec()
def get_chat_member_count(self, *args):
return TeleBot.get_chat_member_count(self, *args)
@util.async_dec()
def set_chat_sticker_set(self, *args):
@ -3036,7 +3062,12 @@ class AsyncTeleBot(TeleBot):
@util.async_dec()
def kick_chat_member(self, *args, **kwargs):
return TeleBot.kick_chat_member(self, *args, **kwargs)
logger.info('kick_chat_member is deprecated. Use ban_chat_member instead.')
return TeleBot.ban_chat_member(self, *args, **kwargs)
@util.async_dec()
def ban_chat_member(self, *args, **kwargs):
return TeleBot.ban_chat_member(self, *args, **kwargs)
@util.async_dec()
def unban_chat_member(self, *args, **kwargs):

View File

@ -93,6 +93,7 @@ def _make_request(token, method_name, method='get', params=None, files=None):
# Long polling hangs for given time. Read timeout should be greater that long_polling_timeout
read_timeout = max(params['timeout'] + 10, read_timeout)
params = params or None #set params to None if empty
result = None
if RETRY_ON_ERROR:
@ -329,8 +330,8 @@ def get_chat_administrators(token, chat_id):
return _make_request(token, method_url, params=payload)
def get_chat_members_count(token, chat_id):
method_url = r'getChatMembersCount'
def get_chat_member_count(token, chat_id):
method_url = r'getChatMemberCount'
payload = {'chat_id': chat_id}
return _make_request(token, method_url, params=payload)
@ -849,8 +850,8 @@ def get_method_by_type(data_type):
return r'sendSticker'
def kick_chat_member(token, chat_id, user_id, until_date=None, revoke_messages=None):
method_url = 'kickChatMember'
def ban_chat_member(token, chat_id, user_id, until_date=None, revoke_messages=None):
method_url = 'banChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id}
if isinstance(until_date, datetime):
payload['until_date'] = until_date.timestamp()
@ -1027,32 +1028,32 @@ def set_chat_title(token, chat_id, title):
return _make_request(token, method_url, params=payload, method='post')
def get_my_commands(token, scope, language_code):
def get_my_commands(token, scope=None, language_code=None):
method_url = r'getMyCommands'
payload = {}
if scope is not None:
if scope:
payload['scope'] = scope.to_json()
if language_code is not None:
if language_code:
payload['language_code'] = language_code
return _make_request(token, method_url, params=payload, method='post')
return _make_request(token, method_url, params=payload)
def set_my_commands(token, commands, scope, language_code):
def set_my_commands(token, commands, scope=None, language_code=None):
method_url = r'setMyCommands'
payload = {'commands': _convert_list_json_serializable(commands)}
if scope is not None:
if scope:
payload['scope'] = scope.to_json()
if language_code is not None:
if language_code:
payload['language_code'] = language_code
return _make_request(token, method_url, params=payload, method='post')
def delete_my_commands(token, scope, language_code):
def delete_my_commands(token, scope=None, language_code=None):
method_url = r'deleteMyCommands'
payload = {}
if scope is not None:
if scope:
payload['scope'] = scope.to_json()
if language_code is not None:
if language_code:
payload['language_code'] = language_code
return _make_request(token, method_url, params=payload, method='post')

View File

@ -849,13 +849,13 @@ class File(JsonDeserializable):
class ForceReply(JsonSerializable):
def __init__(self, selective=None, input_field_placeholder=None):
def __init__(self, selective: Optional[bool]=None, input_field_placeholder: Optional[str]=None):
self.selective: bool = selective
self.input_field_placeholder = input_field_placeholder
self.input_field_placeholder: str = input_field_placeholder
def to_json(self):
json_dict = {'force_reply': True}
if self.selective:
if self.selective is not None:
json_dict['selective'] = True
if self.input_field_placeholder:
json_dict['input_field_placeholder'] = self.input_field_placeholder
@ -876,8 +876,8 @@ class ReplyKeyboardRemove(JsonSerializable):
class ReplyKeyboardMarkup(JsonSerializable):
max_row_keys = 12
def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3,
input_field_placeholder=None):
def __init__(self, resize_keyboard: Optional[bool]=None, one_time_keyboard: Optional[bool]=None,
selective: Optional[bool]=None, row_width: int=3, input_field_placeholder: Optional[str]=None):
if row_width > self.max_row_keys:
# Todo: Will be replaced with Exception in future releases
if not DISABLE_KEYLEN_ERROR:
@ -888,7 +888,7 @@ class ReplyKeyboardMarkup(JsonSerializable):
self.one_time_keyboard: bool = one_time_keyboard
self.selective: bool = selective
self.row_width: int = row_width
self.input_field_placeholder = input_field_placeholder
self.input_field_placeholder: str = input_field_placeholder
self.keyboard: List[List[KeyboardButton]] = []
def add(self, *args, row_width=None):
@ -942,11 +942,11 @@ class ReplyKeyboardMarkup(JsonSerializable):
:return:
"""
json_dict = {'keyboard': self.keyboard}
if self.one_time_keyboard:
if self.one_time_keyboard is not None:
json_dict['one_time_keyboard'] = True
if self.resize_keyboard:
if self.resize_keyboard is not None:
json_dict['resize_keyboard'] = True
if self.selective:
if self.selective is not None:
json_dict['selective'] = True
if self.input_field_placeholder:
json_dict['input_field_placeholder'] = self.input_field_placeholder
@ -954,7 +954,8 @@ class ReplyKeyboardMarkup(JsonSerializable):
class KeyboardButton(Dictionaryable, JsonSerializable):
def __init__(self, text, request_contact=None, request_location=None, request_poll=None):
def __init__(self, text: str, request_contact: Optional[bool]=None,
request_location: Optional[bool]=None, request_poll: Optional[bool]=None):
self.text: str = text
self.request_contact: bool = request_contact
self.request_location: bool = request_location
@ -965,11 +966,11 @@ class KeyboardButton(Dictionaryable, JsonSerializable):
def to_dict(self):
json_dict = {'text': self.text}
if self.request_contact:
if self.request_contact is not None:
json_dict['request_contact'] = self.request_contact
if self.request_location:
if self.request_location is not None:
json_dict['request_location'] = self.request_location
if self.request_poll:
if self.request_poll is not None:
json_dict['request_poll'] = self.request_poll.to_dict()
return json_dict