Skip to content

Commit 38af4f4

Browse files
authored
Merge pull request eternnoir#368 from MasterGroosha/botapi_v3.1
Bot API v3.1
2 parents 32c2178 + 662a834 commit 38af4f4

3 files changed

Lines changed: 289 additions & 7 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,26 @@ class TeleBot:
4444
sendChatAction
4545
getUserProfilePhotos
4646
getUpdates
47-
"""
47+
getFile
48+
kickChatMember
49+
unbanChatMember
50+
restrictChatMember
51+
promoteChatMember
52+
exportChatInviteLink
53+
setChatPhoto
54+
deleteChatPhoto
55+
setChatTitle
56+
setChatDescription
57+
pinChatMessage
58+
unpinChatMessage
59+
leaveChat
60+
getChat
61+
getChatAdministrators
62+
getChatMembersCount
63+
getChatMember
64+
answerCallbackQuery
65+
answerInlineQuery
66+
"""
4867

4968
def __init__(self, token, threaded=True, skip_pending=False, num_threads=2):
5069
"""
@@ -613,6 +632,148 @@ def kick_chat_member(self, chat_id, user_id):
613632
def unban_chat_member(self, chat_id, user_id):
614633
return apihelper.unban_chat_member(self.token, chat_id, user_id)
615634

635+
def restrict_chat_member(self, chat_id, user_id, until_date=None, can_send_messages=True,
636+
can_send_media_messages=True, can_send_other_messages=True,
637+
can_add_web_page_previews=True):
638+
"""
639+
Use this method to restrict a user in a supergroup.
640+
The bot must be an administrator in the supergroup for this to work and must have
641+
the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user.
642+
Returns True on success.
643+
:param chat_id: Int or String : Unique identifier for the target group or username of the target supergroup
644+
or channel (in the format @channelusername)
645+
:param user_id: Int : Unique identifier of the target user
646+
:param until_date: Date when restrictions will be lifted for the user, unix time.
647+
If user is restricted for more than 366 days or less than 30 seconds from the current time,
648+
they are considered to be restricted forever
649+
:param can_send_messages: Pass True, if the user can send text messages, contacts, locations and venues
650+
:param can_send_media_messages Pass True, if the user can send audios, documents, photos, videos, video notes
651+
and voice notes, implies can_send_messages
652+
:param can_send_other_messages: Pass True, if the user can send animations, games, stickers and
653+
use inline bots, implies can_send_media_messages
654+
:param can_add_web_page_previews: Pass True, if the user may add web page previews to their messages,
655+
implies can_send_media_messages
656+
:return: types.Message
657+
"""
658+
return apihelper.restrict_chat_member(self.token, chat_id, user_id, until_date, can_send_messages,
659+
can_send_media_messages, can_send_other_messages,
660+
can_add_web_page_previews)
661+
662+
def promote_chat_member(self, chat_id, user_id, can_change_info=False, can_post_messages=False,
663+
can_edit_messages=False, can_delete_messages=False, can_invite_users=False,
664+
can_restrict_members=False, can_pin_messages=False, can_promote_members=False):
665+
"""
666+
Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator
667+
in the chat for this to work and must have the appropriate admin rights.
668+
Pass False for all boolean parameters to demote a user. Returns True on success.
669+
:param chat_id: Unique identifier for the target chat or username of the target channel (
670+
in the format @channelusername)
671+
:param user_id: Int : Unique identifier of the target user
672+
:param can_change_info: Bool: Pass True, if the administrator can change chat title, photo and other settings
673+
:param can_post_messages: Bool : Pass True, if the administrator can create channel posts, channels only
674+
:param can_edit_messages: Bool : Pass True, if the administrator can edit messages of other users, channels only
675+
:param can_delete_messages: Bool : Pass True, if the administrator can delete messages of other users
676+
:param can_invite_users: Bool : Pass True, if the administrator can invite new users to the chat
677+
:param can_restrict_members: Bool: Pass True, if the administrator can restrict, ban or unban chat members
678+
:param can_pin_messages: Bool: Pass True, if the administrator can pin messages, supergroups only
679+
:param can_promote_members: Bool: Pass True, if the administrator can add new administrators with a subset
680+
of his own privileges or demote administrators that he has promoted, directly or indirectly
681+
(promoted by administrators that were appointed by him)
682+
:return:
683+
"""
684+
return apihelper.promote_chat_member(self.token, chat_id, user_id, can_change_info, can_post_messages,
685+
can_edit_messages, can_delete_messages, can_invite_users,
686+
can_restrict_members, can_pin_messages, can_promote_members)
687+
688+
def export_chat_invite_link(self, chat_id):
689+
"""
690+
Use this method to export an invite link to a supergroup or a channel. The bot must be an administrator
691+
in the chat for this to work and must have the appropriate admin rights.
692+
Returns exported invite link as String on success.
693+
:param chat_id: Id: Unique identifier for the target chat or username of the target channel
694+
(in the format @channelusername)
695+
:return:
696+
"""
697+
return apihelper.export_chat_invite_link(self.token, chat_id)
698+
699+
def set_chat_photo(self, chat_id, photo):
700+
"""
701+
Use this method to set a new profile photo for the chat. Photos can't be changed for private chats.
702+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
703+
Returns True on success.
704+
Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’
705+
setting is off in the target group.
706+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
707+
(in the format @channelusername)
708+
:param photo: InputFile: New chat photo, uploaded using multipart/form-data
709+
:return:
710+
"""
711+
return apihelper.set_chat_photo(self.token, chat_id, photo)
712+
713+
def delete_chat_photo(self, chat_id):
714+
"""
715+
Use this method to delete a chat photo. Photos can't be changed for private chats.
716+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
717+
Returns True on success.
718+
Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’
719+
setting is off in the target group.
720+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
721+
(in the format @channelusername)
722+
:return:
723+
"""
724+
return apihelper.delete_chat_photo(self.token, chat_id)
725+
726+
def set_chat_title(self, chat_id, title):
727+
"""
728+
Use this method to change the title of a chat. Titles can't be changed for private chats.
729+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
730+
Returns True on success.
731+
Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’
732+
setting is off in the target group.
733+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
734+
(in the format @channelusername)
735+
:param title: New chat title, 1-255 characters
736+
:return:
737+
"""
738+
return apihelper.set_chat_title(self.token, chat_id, title)
739+
740+
def set_chat_description(self, chat_id, description):
741+
"""
742+
Use this method to change the description of a supergroup or a channel.
743+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
744+
Returns True on success.
745+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
746+
(in the format @channelusername)
747+
:param description: Str: New chat description, 0-255 characters
748+
:return:
749+
"""
750+
return apihelper.set_chat_description(self.token, chat_id, description)
751+
752+
def pin_chat_message(self, chat_id, message_id, disable_notification=False):
753+
"""
754+
Use this method to pin a message in a supergroup.
755+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
756+
Returns True on success.
757+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
758+
(in the format @channelusername)
759+
:param message_id: Int: Identifier of a message to pin
760+
:param disable_notification: Bool: Pass True, if it is not necessary to send a notification
761+
to all group members about the new pinned message
762+
:return:
763+
"""
764+
return apihelper.pin_chat_message(self.token, chat_id, message_id, disable_notification)
765+
766+
def unpin_chat_message(self, chat_id):
767+
"""
768+
Use this method to unpin a message in a supergroup chat.
769+
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
770+
Returns True on success.
771+
:param chat_id: Int or Str: Unique identifier for the target chat or username of the target channel
772+
(in the format @channelusername)
773+
:return:
774+
"""
775+
return apihelper.unpin_chat_message(self.token, chat_id)
776+
616777
def edit_message_text(self, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,
617778
disable_web_page_preview=None, reply_markup=None):
618779
result = apihelper.edit_message_text(self.token, text, chat_id, message_id, inline_message_id, parse_mode,

‎telebot/apihelper.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,75 @@ def unban_chat_member(token, chat_id, user_id):
431431
return _make_request(token, method_url, params=payload, method='post')
432432

433433

434+
def restrict_chat_member(token, chat_id, user_id, until_date=None, can_send_messages=True,
435+
can_send_media_messages=True, can_send_other_messages=True,
436+
can_add_web_page_previews=True):
437+
method_url = 'restrictChatMember'
438+
payload = {'chat_id': chat_id, 'user_id': user_id, 'until_date': until_date, 'can_send_messages': can_send_messages,
439+
'can_send_media_messages': can_send_media_messages, 'can_send_other_messages': can_send_other_messages,
440+
'can_add_web_page_previews': can_add_web_page_previews}
441+
return _make_request(token, method_url, params=payload, method='post')
442+
443+
444+
def promote_chat_member(token, chat_id, user_id, can_change_info=False, can_post_messages=False,
445+
can_edit_messages=False, can_delete_messages=False, can_invite_users=False,
446+
can_restrict_members=False, can_pin_messages=False, can_promote_members=False):
447+
method_url = 'promoteChatMember'
448+
payload = {'chat_id': chat_id, 'user_id': user_id, 'can_change_info': can_change_info,
449+
'can_post_messages': can_post_messages, 'can_edit_messages': can_edit_messages,
450+
'can_delete_messages': can_delete_messages, 'can_invite_users': can_invite_users,
451+
'can_restrict_members': can_restrict_members, 'can_pin_messages': can_pin_messages,
452+
'can_promote_members': can_promote_members }
453+
return _make_request(token, method_url, params=payload, method='post')
454+
455+
456+
def export_chat_invite_link(token, chat_id):
457+
method_url = 'exportChatInviteLink'
458+
payload = {'chat_id': chat_id}
459+
return _make_request(token, method_url, params=payload, method='post')
460+
461+
462+
def set_chat_photo(token, chat_id, photo):
463+
method_url = 'setChatPhoto'
464+
payload = {'chat_id': chat_id}
465+
files = None
466+
if not util.is_string(photo):
467+
files = {'photo': photo}
468+
else:
469+
payload['photo'] = photo
470+
return _make_request(token, method_url, params=payload, files=files, method='post')
471+
472+
473+
def delete_chat_photo(token, chat_id):
474+
method_url = 'deleteChatPhoto'
475+
payload = {'chat_id': chat_id}
476+
return _make_request(token, method_url, params=payload, method='post')
477+
478+
479+
def set_chat_title(token, chat_id, title):
480+
method_url = 'setChatTitle'
481+
payload = {'chat_id': chat_id, 'title': title}
482+
return _make_request(token, method_url, params=payload, method='post')
483+
484+
485+
def set_chat_description(token, chat_id, description):
486+
method_url = 'setChatDescription'
487+
payload = {'chat_id': chat_id, 'description': description}
488+
return _make_request(token, method_url, params=payload, method='post')
489+
490+
491+
def pin_chat_message(token, chat_id, message_id, disable_notification=False):
492+
method_url = 'pinChatMessage'
493+
payload = {'chat_id': chat_id, 'message_id': message_id, 'disable_notification': disable_notification}
494+
return _make_request(token, method_url, params=payload, method='post')
495+
496+
497+
def unpin_chat_message(token, chat_id):
498+
method_url = 'unpinChatMessage'
499+
payload = {'chat_id': chat_id}
500+
return _make_request(token, method_url, params=payload, method='post')
501+
502+
434503
# Updating messages
435504

436505
def edit_message_text(token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,

‎telebot/types.py‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,25 @@ def de_json(cls, json_string):
215215
first_name = obj.get('first_name')
216216
last_name = obj.get('last_name')
217217
all_members_are_administrators = obj.get('all_members_are_administrators')
218-
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators)
218+
if 'photo' in obj:
219+
photo = ChatPhoto.de_json(obj['photo'])
220+
description = obj.get('description')
221+
invite_link = obj.get('invite_link')
222+
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators,
223+
photo, description, invite_link)
219224

220225
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None,
221-
all_members_are_administrators=None):
226+
all_members_are_administrators=None, photo=None, description=None, invite_link=None):
222227
self.type = type
223228
self.last_name = last_name
224229
self.first_name = first_name
225230
self.username = username
226231
self.id = id
227232
self.title = title
228233
self.all_members_are_administrators = all_members_are_administrators
234+
self.photo = photo
235+
self.description = description
236+
self.invite_link = invite_link
229237

230238

231239
class Message(JsonDeserializable):
@@ -850,18 +858,62 @@ def __init__(self, id, from_user, data, chat_instance, message=None, inline_mess
850858
self.inline_message_id = inline_message_id
851859

852860

861+
class ChatPhoto(JsonDeserializable):
862+
@classmethod
863+
def de_json(cls, json_type):
864+
obj = cls.check_json(json_type)
865+
small_file_id = obj['small_file_id']
866+
big_file_id = obj['big_file_id']
867+
return cls(small_file_id, big_file_id)
868+
869+
def __init__(self, small_file_id, big_file_id):
870+
self.small_file_id = small_file_id
871+
self.big_file_id = big_file_id
872+
873+
853874
class ChatMember(JsonDeserializable):
854875
@classmethod
855876
def de_json(cls, json_type):
856877
obj = cls.check_json(json_type)
857878
user = User.de_json(obj['user'])
858879
status = obj['status']
859-
return cls(user, status)
860-
861-
def __init__(self, user, status):
880+
until_date = obj.get('until_date')
881+
can_be_edited = obj.get('can_be_edited')
882+
can_change_info = obj.get('can_change_info')
883+
can_post_messages = obj.get('can_post_messages')
884+
can_edit_messages = obj.get('can_edit_messages')
885+
can_delete_messages = obj.get('can_delete_messages')
886+
can_invite_users = obj.get('can_invite_users')
887+
can_restrict_members = obj.get('can_restrict_members')
888+
can_pin_messages = obj.get('can_pin_messages')
889+
can_promote_members = obj.get('can_promote_members')
890+
can_send_messages = obj.get('can_send_messages')
891+
can_send_media_messages = obj.get('can_send_media_messages')
892+
can_send_other_messages = obj.get('can_send_other_messages')
893+
can_add_web_page_previews = obj.get('can_add_web_page_previews')
894+
return cls(user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
895+
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
896+
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews)
897+
898+
def __init__(self, user, status, until_date, can_be_edited, can_change_info, can_post_messages, can_edit_messages,
899+
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members,
900+
can_send_messages, can_send_media_messages, can_send_other_messages, can_add_web_page_previews):
862901
self.user = user
863902
self.status = status
864-
903+
self.until_date = until_date
904+
self.can_be_edited = can_be_edited
905+
self.can_change_info = can_change_info
906+
self.can_post_messages = can_post_messages
907+
self.can_edit_messages = can_edit_messages
908+
self.can_delete_messages = can_delete_messages
909+
self.can_invite_users = can_invite_users
910+
self.can_restrict_members = can_restrict_members
911+
self.can_pin_messages = can_pin_messages
912+
self.can_promote_members = can_promote_members
913+
self.can_send_messages = can_send_messages
914+
self.can_send_media_messages = can_send_media_messages
915+
self.can_send_other_messages = can_send_other_messages
916+
self.can_add_web_page_previews = can_add_web_page_previews
865917

866918
# InlineQuery
867919

0 commit comments

Comments
 (0)