From 0aa7a8a8f694459bee66dfb7652430bdfab26ec1 Mon Sep 17 00:00:00 2001 From: SwissCorePy <51398037+SwissCorePy@users.noreply.github.com> Date: Mon, 28 Jun 2021 09:31:06 +0200 Subject: [PATCH] new 5.3 function names added the new function names (the previous names are still working) from 5.3 and some other small changes --- telebot/__init__.py | 107 +++++++++++++++++++++++++++++-------------- telebot/apihelper.py | 39 ++++++++++++---- telebot/types.py | 27 +++++------ 3 files changed, 116 insertions(+), 57 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 9864b59..7adeeb1 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -104,6 +104,7 @@ class TeleBot: editMessageLiveLocation stopMessageLiveLocation kickChatMember + banChatMember unbanChatMember restrictChatMember promoteChatMember @@ -132,10 +133,12 @@ class TeleBot: getChat getChatAdministrators getChatMembersCount + getChatMemberCount getChatMember answerCallbackQuery getMyCommands setMyCommands + deleteMyCommands answerInlineQuery answerShippingQuery answerPreCheckoutQuery @@ -844,6 +847,15 @@ class TeleBot: """ result = apihelper.get_chat_members_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_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: """ @@ -1475,6 +1487,26 @@ class TeleBot: """ return apihelper.kick_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 + less than 30 seconds from the current time they are considered to be banned forever + :param revoke_messages: Bool: Pass True to delete all messages from the chat for the user that is being removed. + If False, the user will be able to see messages in the group that were sent before the user was removed. + Always True for supergroups and channels. + :return: boolean + """ + 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, only_if_banned: Optional[bool]=False) -> bool: @@ -1699,54 +1731,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 +2934,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): @@ -2941,6 +2972,10 @@ class AsyncTeleBot(TeleBot): @util.async_dec() def get_chat_members_count(self, *args): return TeleBot.get_chat_members_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): @@ -3037,6 +3072,10 @@ class AsyncTeleBot(TeleBot): @util.async_dec() def kick_chat_member(self, *args, **kwargs): return TeleBot.kick_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): diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 384c0bc..7b1779a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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: @@ -335,6 +336,12 @@ def get_chat_members_count(token, chat_id): return _make_request(token, method_url, params=payload) +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) + + def set_sticker_set_thumb(token, name, user_id, thumb): method_url = r'setStickerSetThumb' payload = {'name': name, 'user_id': user_id} @@ -861,6 +868,18 @@ def kick_chat_member(token, chat_id, user_id, until_date=None, revoke_messages=N return _make_request(token, method_url, params=payload, method='post') +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() + else: + payload['until_date'] = until_date + if revoke_messages is not None: + payload['revoke_messages'] = revoke_messages + return _make_request(token, method_url, params=payload, method='post') + + def unban_chat_member(token, chat_id, user_id, only_if_banned): method_url = 'unbanChatMember' payload = {'chat_id': chat_id, 'user_id': user_id} @@ -1027,32 +1046,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') diff --git a/telebot/types.py b/telebot/types.py index 8688132..aed2e92 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -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