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

new 5.3 function names

added the new function names (the previous names are still working) from 5.3 and some other small changes
This commit is contained in:
SwissCorePy 2021-06-28 09:31:06 +02:00
parent 72ed7c1dde
commit 0aa7a8a8f6
3 changed files with 116 additions and 57 deletions

View File

@ -104,6 +104,7 @@ class TeleBot:
editMessageLiveLocation editMessageLiveLocation
stopMessageLiveLocation stopMessageLiveLocation
kickChatMember kickChatMember
banChatMember
unbanChatMember unbanChatMember
restrictChatMember restrictChatMember
promoteChatMember promoteChatMember
@ -132,10 +133,12 @@ class TeleBot:
getChat getChat
getChatAdministrators getChatAdministrators
getChatMembersCount getChatMembersCount
getChatMemberCount
getChatMember getChatMember
answerCallbackQuery answerCallbackQuery
getMyCommands getMyCommands
setMyCommands setMyCommands
deleteMyCommands
answerInlineQuery answerInlineQuery
answerShippingQuery answerShippingQuery
answerPreCheckoutQuery answerPreCheckoutQuery
@ -844,6 +847,15 @@ class TeleBot:
""" """
result = apihelper.get_chat_members_count(self.token, chat_id) result = apihelper.get_chat_members_count(self.token, chat_id)
return result 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: 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) 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( def unban_chat_member(
self, chat_id: Union[int, str], user_id: int, self, chat_id: Union[int, str], user_id: int,
only_if_banned: Optional[bool]=False) -> bool: only_if_banned: Optional[bool]=False) -> bool:
@ -1699,54 +1731,49 @@ class TeleBot:
setting is off in the target group. 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 :param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
(in the format @channelusername) (in the format @channelusername)
:return:
""" """
return apihelper.delete_chat_photo(self.token, chat_id) return apihelper.delete_chat_photo(self.token, chat_id)
def get_my_commands(self, def get_my_commands(self, scope: Optional[types.BotCommandScope],
scope: Optional[Union[ language_code: Optional[str]) -> List[types.BotCommand]:
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats,
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]]=None,
language_code: Optional[str]=None) -> List[types.BotCommand]:
""" """
Use this method to get the current list of the bot's commands for the given scope and user language Use this method to get the current list of the bot's commands.
:param scope: scope of users for which the commands are relevant
:param language_code: A two-letter ISO 639-1 language code
Returns List of BotCommand on success. 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) result = apihelper.get_my_commands(self.token, scope, language_code)
return [types.BotCommand.de_json(cmd) for cmd in result] return [types.BotCommand.de_json(cmd) for cmd in result]
def set_my_commands(self, commands: List[types.BotCommand], def set_my_commands(self, commands: List[types.BotCommand],
scope: Optional[Union[ scope: Optional[types.BotCommandScope]=None,
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats, language_code: Optional[str]=None) -> bool:
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]] = None,
language_code: Optional[str]=None) -> bool:
""" """
Use this method to change the list of the bot's commands. 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 commands: List of BotCommand. At most 100 commands can be specified.
:param scope: scope of users for which the commands are relevant :param scope: The scope of users for which the commands are relevant.
:param language_code: A two-letter ISO 639-1 language code 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:
""" """
return apihelper.set_my_commands(self.token, commands, scope, language_code) return apihelper.set_my_commands(self.token, commands, scope, language_code)
def delete_my_commands(self, def delete_my_commands(self, scope: Optional[types.BotCommandScope]=None,
scope: Optional[Union[ language_code: Optional[int]=None) -> bool:
types.BotCommandScopeDefault, types.BotCommandScopeAllPrivateChats,
types.BotCommandScopeAllGroupChats, types.BotCommandScopeAllChatAdministrators,
types.BotCommandScopeChat,
types.BotCommandScopeChatAdministrators, types.BotCommandScopeChatMember]]=None,
language_code: Optional[str]=None) -> bool:
""" """
Use this method to delete the list of the bot's commands for the given scope and user language. 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 After deletion, higher level commands will be shown to affected users.
:param language_code: A two-letter ISO 639-1 language code Returns True on success.
:return: :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) return apihelper.delete_my_commands(self.token, scope, language_code)
@ -2907,12 +2934,16 @@ class AsyncTeleBot(TeleBot):
return TeleBot.close(self) return TeleBot.close(self)
@util.async_dec() @util.async_dec()
def 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) return TeleBot.get_my_commands(self, *args, **kwargs)
@util.async_dec() @util.async_dec()
def set_my_commands(self, *args, **kwargs): def set_my_commands(self, *args, **kwargs):
return TeleBot.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() @util.async_dec()
def get_file(self, *args): def get_file(self, *args):
@ -2941,6 +2972,10 @@ class AsyncTeleBot(TeleBot):
@util.async_dec() @util.async_dec()
def get_chat_members_count(self, *args): def get_chat_members_count(self, *args):
return TeleBot.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() @util.async_dec()
def set_chat_sticker_set(self, *args): def set_chat_sticker_set(self, *args):
@ -3037,6 +3072,10 @@ class AsyncTeleBot(TeleBot):
@util.async_dec() @util.async_dec()
def kick_chat_member(self, *args, **kwargs): def kick_chat_member(self, *args, **kwargs):
return TeleBot.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() @util.async_dec()
def unban_chat_member(self, *args, **kwargs): 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 # Long polling hangs for given time. Read timeout should be greater that long_polling_timeout
read_timeout = max(params['timeout'] + 10, read_timeout) read_timeout = max(params['timeout'] + 10, read_timeout)
params = params or None #set params to None if empty
result = None result = None
if RETRY_ON_ERROR: if RETRY_ON_ERROR:
@ -335,6 +336,12 @@ def get_chat_members_count(token, chat_id):
return _make_request(token, method_url, params=payload) 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): def set_sticker_set_thumb(token, name, user_id, thumb):
method_url = r'setStickerSetThumb' method_url = r'setStickerSetThumb'
payload = {'name': name, 'user_id': user_id} 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') 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): def unban_chat_member(token, chat_id, user_id, only_if_banned):
method_url = 'unbanChatMember' method_url = 'unbanChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id} 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') 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' method_url = r'getMyCommands'
payload = {} payload = {}
if scope is not None: if scope:
payload['scope'] = scope.to_json() payload['scope'] = scope.to_json()
if language_code is not None: if language_code:
payload['language_code'] = 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' method_url = r'setMyCommands'
payload = {'commands': _convert_list_json_serializable(commands)} payload = {'commands': _convert_list_json_serializable(commands)}
if scope is not None: if scope:
payload['scope'] = scope.to_json() payload['scope'] = scope.to_json()
if language_code is not None: if language_code:
payload['language_code'] = 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, method='post')
def delete_my_commands(token, scope, language_code): def delete_my_commands(token, scope=None, language_code=None):
method_url = r'deleteMyCommands' method_url = r'deleteMyCommands'
payload = {} payload = {}
if scope is not None: if scope:
payload['scope'] = scope.to_json() payload['scope'] = scope.to_json()
if language_code is not None: if language_code:
payload['language_code'] = 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, method='post')

View File

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