Bot API v3.1

This commit is contained in:
Evgeny Petrov 2017-06-30 19:47:09 +03:00
parent 32c2178b29
commit 25a37db2bb
3 changed files with 270 additions and 6 deletions

View File

@ -44,7 +44,26 @@ class TeleBot:
sendChatAction
getUserProfilePhotos
getUpdates
"""
getFile
kickChatMember
unbanChatMember
restrictChatMember
promoteChatMember
exportChatInviteLink
setChatPhoto
deleteChatPhoto
setChatTitle
setChatDescription
pinChatMessage
unpinChatMessage
leaveChat
getChat
getChatAdministrators
getChatMembersCount
getChatMember
answerCallbackQuery
answerInlineQuery
"""
def __init__(self, token, threaded=True, skip_pending=False, num_threads=2):
"""
@ -613,6 +632,134 @@ class TeleBot:
def unban_chat_member(self, chat_id, user_id):
return apihelper.unban_chat_member(self.token, chat_id, user_id)
def restrict_chat_member(self, chat_id, user_id):
"""
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.
Returns True on success.
:param chat_id: Int or String : Unique identifier for the target group or username of the target supergroup
or channel (in the format @channelusername)
:param user_id: Int : Unique identifier of the target user
:return: types.Message
"""
return apihelper.restrict_chat_member(self.token, chat_id, user_id)
def promote_chat_member(self, chat_id, user_id, can_change_info=False, can_post_messages=False,
can_edit_messages=False, can_delete_messages=False, can_invite_users=False,
can_restrict_members=False, can_pin_messages=False, can_promote_members=False):
"""
Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator
in the chat for this to work and must have the appropriate admin rights.
Pass False for all boolean parameters to demote a user. Returns True on success.
:param chat_id: Unique identifier for the target chat or username of the target channel (
in the format @channelusername)
:param user_id: Int : Unique identifier of the target user
:param can_change_info: Bool: Pass True, if the administrator can change chat title, photo and other settings
:param can_post_messages: Bool : Pass True, if the administrator can create channel posts, channels only
:param can_edit_messages: Bool : Pass True, if the administrator can edit messages of other users, channels only
:param can_delete_messages: Bool : Pass True, if the administrator can delete messages of other users
:param can_invite_users: Bool : Pass True, if the administrator can invite new users to the chat
:param can_restrict_members: Bool: Pass True, if the administrator can restrict, ban or unban chat members
:param can_pin_messages: Bool: Pass True, if the administrator can pin messages, supergroups only
:param can_promote_members: Bool: Pass True, if the administrator can add new administrators with a subset
of his own privileges or demote administrators that he has promoted, directly or indirectly
(promoted by administrators that were appointed by him)
:return:
"""
return apihelper.promote_chat_member(self.token, chat_id, user_id, can_change_info, can_post_messages,
can_edit_messages, can_delete_messages, can_invite_users,
can_restrict_members, can_pin_messages, can_promote_members)
def export_chat_invite_link(self, chat_id):
"""
Use this method to export an invite link to a supergroup or a channel. The bot must be an administrator
in the chat for this to work and must have the appropriate admin rights.
Returns exported invite link as String on success.
:param chat_id: Id: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:return:
"""
return apihelper.export_chat_invite_link(self.token, chat_id)
def set_chat_photo(self, chat_id, photo):
"""
Use this method to set a new profile photo for the chat. Photos can't be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins
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)
:param photo: InputFile: New chat photo, uploaded using multipart/form-data
:return:
"""
return apihelper.set_chat_photo(self.token, chat_id, photo)
def delete_chat_photo(self, chat_id):
"""
Use this method to delete a chat photo. Photos can't be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins
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 set_chat_title(self, chat_id, title):
"""
Use this method to change the title of a chat. Titles can't be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins
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)
:param title: New chat title, 1-255 characters
:return:
"""
return apihelper.set_chat_title(self.token, chat_id, title)
def set_chat_description(self, chat_id, description):
"""
Use this method to change the description of a supergroup or a channel.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:param description: Str: New chat description, 0-255 characters
:return:
"""
return apihelper.set_chat_description(self.token, chat_id, description)
def pin_chat_message(self, chat_id, message_id, disable_notification=False):
"""
Use this method to pin a message in a supergroup.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:param message_id: Int: Identifier of a message to pin
:param disable_notification: Bool: Pass True, if it is not necessary to send a notification
to all group members about the new pinned message
:return:
"""
return apihelper.pin_chat_message(self.token, chat_id, message_id, disable_notification)
def unpin_chat_message(self, chat_id):
"""
Use this method to unpin a message in a supergroup chat.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
Returns True on success.
: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.unpin_chat_message(self.token, chat_id)
def edit_message_text(self, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,
disable_web_page_preview=None, reply_markup=None):
result = apihelper.edit_message_text(self.token, text, chat_id, message_id, inline_message_id, parse_mode,

View File

@ -431,6 +431,71 @@ def unban_chat_member(token, chat_id, user_id):
return _make_request(token, method_url, params=payload, method='post')
def restrict_chat_member(token, chat_id, user_id):
method_url = 'restrictChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id}
return _make_request(token, method_url, params=payload, method='post')
def promote_chat_member(token, chat_id, user_id, can_change_info=False, can_post_messages=False,
can_edit_messages=False, can_delete_messages=False, can_invite_users=False,
can_restrict_members=False, can_pin_messages=False, can_promote_members=False):
method_url = 'promoteChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id, 'can_change_info': can_change_info,
'can_post_messages': can_post_messages, 'can_edit_messages': can_edit_messages,
'can_delete_messages': can_delete_messages, 'can_invite_users': can_invite_users,
'can_restrict_members': can_restrict_members, 'can_pin_messages': can_pin_messages,
'can_promote_members': can_promote_members }
return _make_request(token, method_url, params=payload, method='post')
def export_chat_invite_link(token, chat_id):
method_url = 'exportChatInviteLink'
payload = {'chat_id': chat_id}
return _make_request(token, method_url, params=payload, method='post')
def set_chat_photo(token, chat_id, photo):
method_url = 'setChatPhoto'
payload = {'chat_id': chat_id}
files = None
if not util.is_string(photo):
files = {'photo': photo}
else:
payload['photo'] = photo
return _make_request(token, method_url, params=payload, files=files, method='post')
def delete_chat_photo(token, chat_id):
method_url = 'deleteChatPhoto'
payload = {'chat_id': chat_id}
return _make_request(token, method_url, params=payload, method='post')
def set_chat_title(token, chat_id, title):
method_url = 'setChatTitle'
payload = {'chat_id': chat_id, 'title': title}
return _make_request(token, method_url, params=payload, method='post')
def set_chat_description(token, chat_id, description):
method_url = 'setChatDescription'
payload = {'chat_id': chat_id, 'description': description}
return _make_request(token, method_url, params=payload, method='post')
def pin_chat_message(token, chat_id, message_id, disable_notification=False):
method_url = 'pinChatMessage'
payload = {'chat_id': chat_id, 'message_id': message_id, 'disable_notification': disable_notification}
return _make_request(token, method_url, params=payload, method='post')
def unpin_chat_message(token, chat_id):
method_url = 'unpinChatMessage'
payload = {'chat_id': chat_id}
return _make_request(token, method_url, params=payload, method='post')
# Updating messages
def edit_message_text(token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,

View File

@ -215,10 +215,15 @@ class Chat(JsonDeserializable):
first_name = obj.get('first_name')
last_name = obj.get('last_name')
all_members_are_administrators = obj.get('all_members_are_administrators')
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators)
if 'photo' in obj:
photo = ChatPhoto.de_json(obj['photo'])
description = obj.get('description')
invite_link = obj.get('invite_link')
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators,
photo, description, invite_link)
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None,
all_members_are_administrators=None):
all_members_are_administrators=None, photo=None, description=None, invite_link=None):
self.type = type
self.last_name = last_name
self.first_name = first_name
@ -226,6 +231,9 @@ class Chat(JsonDeserializable):
self.id = id
self.title = title
self.all_members_are_administrators = all_members_are_administrators
self.photo = photo
self.description = description
self.invite_link = invite_link
class Message(JsonDeserializable):
@ -850,18 +858,62 @@ class CallbackQuery(JsonDeserializable):
self.inline_message_id = inline_message_id
class ChatPhoto(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
small_file_id = obj['small_file_id']
big_file_id = obj['big_file_id']
return cls(small_file_id, big_file_id)
def __init__(self, small_file_id, big_file_id):
self.small_file_id = small_file_id
self.big_file_id = big_file_id
class ChatMember(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
user = User.de_json(obj['user'])
status = obj['status']
return cls(user, status)
until_date = obj.get('until_date')
can_be_edited = obj.get('can_be_edited')
can_change_info = obj.get('can_change_info')
can_post_messages = obj.get('can_post_messages')
can_edit_messages = obj.get('can_edit_messages')
can_delete_messages = obj.get('can_delete_messages')
can_invite_users = obj.get('can_invite_users')
can_restrict_members = obj.get('can_restrict_members')
can_pin_messages = obj.get('can_pin_messages')
can_promote_members = obj.get('can_promote_members')
can_send_messages = obj.get('can_send_messages')
can_send_media_messages = obj.get('can_send_media_messages')
can_send_other_messages = obj.get('can_send_other_messages')
can_add_web_page_previews = obj.get('can_add_web_page_previews')
return cls(user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews)
def __init__(self, user, status):
def __init__(self, user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews):
self.user = user
self.status = status
self.until_date = until_date
self.can_be_edited = can_be_edited
self.can_change_info = can_change_info
self.can_post_messages = can_post_messages
self.can_edit_messages = can_edit_messages
self.can_delete_messages = can_delete_messages
self.can_invite_users = can_invite_users
self.can_restrict_members = can_restrict_members
self.can_pin_messages = can_pin_messages
self.can_promote_members = can_promote_members
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
# InlineQuery