From 953e2286b854a84364f3b50ae2793841e408f8a9 Mon Sep 17 00:00:00 2001 From: _run Date: Sat, 6 Nov 2021 12:15:28 +0500 Subject: [PATCH] Bot API 5.4 --- examples/chat_join_request.py | 11 +++++++++++ telebot/__init__.py | 28 ++++++++++++++++++++++++++++ telebot/apihelper.py | 15 ++++++++++++++- telebot/types.py | 7 ++++--- telebot/util.py | 2 +- 5 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 examples/chat_join_request.py diff --git a/examples/chat_join_request.py b/examples/chat_join_request.py new file mode 100644 index 0000000..6ab29ed --- /dev/null +++ b/examples/chat_join_request.py @@ -0,0 +1,11 @@ +import telebot + + +bot = telebot.TeleBot('TOKEN') + +@bot.chat_join_request_handler() +def make_some(message: telebot.types.ChatJoinRequest): + bot.send_message(message.chat.id, 'I accepted a new user!') + bot.approve_chat_join_request(message.chat.id, message.from_user.id) + +bot.infinity_polling(allowed_updates=telebot.util.update_types) \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index 185d1ee..4aba9f9 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -484,6 +484,7 @@ class TeleBot: if new_chat_members is None: new_chat_members = [] new_chat_members.append(update.chat_member) if update.chat_join_request: + print('we received1') if chat_join_request is None: chat_join_request = [] chat_join_request.append(update.chat_join_request) @@ -514,6 +515,7 @@ class TeleBot: if new_chat_members: self.process_new_chat_member(new_chat_members) if chat_join_request: + print('we received2') self.process_chat_join_request(chat_join_request) @@ -1747,6 +1749,32 @@ class TeleBot: """ return apihelper.export_chat_invite_link(self.token, chat_id) + def approve_chat_join_request(self, chat_id: Union[str, int], user_id: Union[int, str]) -> bool: + """ + Use this method to approve a chat join request. + The bot must be an administrator in the chat for this to work and must have + the can_invite_users administrator right. Returns True on success. + + :param chat_id: Unique identifier for the target chat or username of the target supergroup + (in the format @supergroupusername) + :param user_id: Unique identifier of the target user + :return: True on success. + """ + return apihelper.approve_chat_join_request(self.token, chat_id, user_id) + + def decline_chat_join_request(self, chat_id: Union[str, int], user_id: Union[int, str]) -> bool: + """ + Use this method to decline a chat join request. + The bot must be an administrator in the chat for this to work and must have + the can_invite_users administrator right. Returns True on success. + + :param chat_id: Unique identifier for the target chat or username of the target supergroup + (in the format @supergroupusername) + :param user_id: Unique identifier of the target user + :return: True on success. + """ + return apihelper.decline_chat_join_request(self.token, chat_id, user_id) + def set_chat_photo(self, chat_id: Union[int, str], photo: Any) -> bool: """ Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 4d4f919..f66db8a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1031,7 +1031,20 @@ def export_chat_invite_link(token, chat_id): payload = {'chat_id': chat_id} return _make_request(token, method_url, params=payload, method='post') - +def approve_chat_join_request(token, chat_id, user_id): + method_url = 'approveChatJoinRequest' + payload = { + 'chat_id': chat_id, + 'user_id': user_id + } + return _make_request(token, method_url, params=payload, method='post') +def decline_chat_join_request(token, chat_id, user_id): + method_url = 'declineChatJoinRequest' + payload = { + 'chat_id': chat_id, + 'user_id': user_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} diff --git a/telebot/types.py b/telebot/types.py index 99b2559..972e2fd 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -110,11 +110,11 @@ class Update(JsonDeserializable): chat_join_request = ChatJoinRequest.de_json(obj.get('chat_join_request')) return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query, chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer, - my_chat_member, chat_member) + my_chat_member, chat_member, chat_join_request) def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query, chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer, - my_chat_member, chat_member): + my_chat_member, chat_member, chat_join_request): self.update_id = update_id self.message = message self.edited_message = edited_message @@ -129,6 +129,7 @@ class Update(JsonDeserializable): self.poll_answer = poll_answer self.my_chat_member = my_chat_member self.chat_member = chat_member + self.chat_join_request = chat_join_request class ChatMemberUpdated(JsonDeserializable): @@ -173,7 +174,7 @@ class ChatJoinRequest(JsonDeserializable): if json_string is None: return None obj = cls.check_json(json_string) obj['chat'] = Chat.de_json(obj['chat']) - obj['from'] = User.de_json(obj['from']) + obj['from_user'] = User.de_json(obj['from']) return cls(**obj) diff --git a/telebot/util.py b/telebot/util.py index 5eb99bc..1ab6201 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -46,7 +46,7 @@ content_type_service = [ update_types = [ "update_id", "message", "edited_message", "channel_post", "edited_channel_post", "inline_query", "chosen_inline_result", "callback_query", "shipping_query", "pre_checkout_query", "poll", "poll_answer", - "my_chat_member", "chat_member" + "my_chat_member", "chat_member", "chat_join_request" ] class WorkerThread(threading.Thread):