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

Little update

Allowed other handlers, checked methods and other things
This commit is contained in:
_run 2021-11-05 23:22:03 +05:00
parent 2623fa362c
commit 06c8782127
3 changed files with 94 additions and 12 deletions

View File

@ -185,6 +185,7 @@ class TeleBot:
self.poll_answer_handlers = [] self.poll_answer_handlers = []
self.my_chat_member_handlers = [] self.my_chat_member_handlers = []
self.chat_member_handlers = [] self.chat_member_handlers = []
self.chat_join_request_handlers = []
self.custom_filters = {} self.custom_filters = {}
self.state_handlers = [] self.state_handlers = []
@ -205,7 +206,8 @@ class TeleBot:
'poll': [], 'poll': [],
'poll_answer': [], 'poll_answer': [],
'my_chat_member': [], 'my_chat_member': [],
'chat_member': [] 'chat_member': [],
'chat_join_request': []
} }
self.default_middleware_handlers = [] self.default_middleware_handlers = []
@ -426,6 +428,7 @@ class TeleBot:
new_poll_answers = None new_poll_answers = None
new_my_chat_members = None new_my_chat_members = None
new_chat_members = None new_chat_members = None
chat_join_request = None
for update in updates: for update in updates:
if apihelper.ENABLE_MIDDLEWARE: if apihelper.ENABLE_MIDDLEWARE:
@ -480,6 +483,9 @@ class TeleBot:
if update.chat_member: if update.chat_member:
if new_chat_members is None: new_chat_members = [] if new_chat_members is None: new_chat_members = []
new_chat_members.append(update.chat_member) new_chat_members.append(update.chat_member)
if update.chat_join_request:
if chat_join_request is None: chat_join_request = []
chat_join_request.append(update.chat_join_request)
if new_messages: if new_messages:
self.process_new_messages(new_messages) self.process_new_messages(new_messages)
@ -507,6 +513,9 @@ class TeleBot:
self.process_new_my_chat_member(new_my_chat_members) self.process_new_my_chat_member(new_my_chat_members)
if new_chat_members: if new_chat_members:
self.process_new_chat_member(new_chat_members) self.process_new_chat_member(new_chat_members)
if chat_join_request:
self.process_chat_join_request(chat_join_request)
def process_new_messages(self, new_messages): def process_new_messages(self, new_messages):
self._notify_next_handlers(new_messages) self._notify_next_handlers(new_messages)
@ -550,6 +559,9 @@ class TeleBot:
def process_new_chat_member(self, chat_members): def process_new_chat_member(self, chat_members):
self._notify_command_handlers(self.chat_member_handlers, chat_members) self._notify_command_handlers(self.chat_member_handlers, chat_members)
def process_chat_join_request(self, chat_join_request):
self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request)
def process_middlewares(self, update): def process_middlewares(self, update):
for update_type, middlewares in self.typed_middleware_handlers.items(): for update_type, middlewares in self.typed_middleware_handlers.items():
if getattr(update, update_type) is not None: if getattr(update, update_type) is not None:
@ -1668,8 +1680,10 @@ class TeleBot:
def create_chat_invite_link( def create_chat_invite_link(
self, chat_id: Union[int, str], self, chat_id: Union[int, str],
name: Optional[str]=None,
expire_date: Optional[Union[int, datetime]]=None, expire_date: Optional[Union[int, datetime]]=None,
member_limit: Optional[int]=None) -> types.ChatInviteLink: member_limit: Optional[int]=None,
creates_join_request: Optional[bool]=None) -> types.ChatInviteLink:
""" """
Use this method to create an additional invite link for a chat. Use this method to create an additional invite link for a chat.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
@ -1681,13 +1695,15 @@ class TeleBot:
:return: :return:
""" """
return types.ChatInviteLink.de_json( return types.ChatInviteLink.de_json(
apihelper.create_chat_invite_link(self.token, chat_id, expire_date, member_limit) apihelper.create_chat_invite_link(self.token, chat_id, name, expire_date, member_limit, creates_join_request)
) )
def edit_chat_invite_link( def edit_chat_invite_link(
self, chat_id: Union[int, str], invite_link: str, self, chat_id: Union[int, str], name: Optional[str]=None,
invite_link: Optional[str] = None,
expire_date: Optional[Union[int, datetime]]=None, expire_date: Optional[Union[int, datetime]]=None,
member_limit: Optional[int]=None) -> types.ChatInviteLink: member_limit: Optional[int]=None ,
creates_join_request: Optional[bool]=None) -> types.ChatInviteLink:
""" """
Use this method to edit a non-primary invite link created by the bot. Use this method to edit a non-primary invite link created by the bot.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
@ -1701,7 +1717,7 @@ class TeleBot:
:return: :return:
""" """
return types.ChatInviteLink.de_json( return types.ChatInviteLink.de_json(
apihelper.edit_chat_invite_link(self.token, chat_id, invite_link, expire_date, member_limit) apihelper.edit_chat_invite_link(self.token, chat_id, name, invite_link, expire_date, member_limit, creates_join_request)
) )
def revoke_chat_invite_link( def revoke_chat_invite_link(
@ -3148,6 +3164,39 @@ class TeleBot:
handler_dict = self._build_handler_dict(callback, func=func, **kwargs) handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
self.add_chat_member_handler(handler_dict) self.add_chat_member_handler(handler_dict)
def chat_join_request_handler(self, func=None, **kwargs):
"""
chat_join_request handler
:param func:
:param kwargs:
:return:
"""
def decorator(handler):
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
self.add_chat_join_request_handler(handler_dict)
return handler
return decorator
def add_chat_join_request_handler(self, handler_dict):
"""
Adds a chat_join_request handler
:param handler_dict:
:return:
"""
self.chat_join_request_handlers.append(handler_dict)
def register_chat_join_request_handler(self, callback, func=None, **kwargs):
"""
Registers chat join request handler.
:param callback: function to be called
:param func:
:return: decorated function
"""
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
self.add_chat_join_request_handler(handler_dict)
def _test_message_handler(self, message_handler, message): def _test_message_handler(self, message_handler, message):
""" """
Test message handler Test message handler

View File

@ -973,7 +973,7 @@ def set_chat_permissions(token, chat_id, permissions):
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
def create_chat_invite_link(token, chat_id, expire_date, member_limit): def create_chat_invite_link(token, chat_id, name, expire_date, member_limit, creates_join_request):
method_url = 'createChatInviteLink' method_url = 'createChatInviteLink'
payload = { payload = {
'chat_id': chat_id 'chat_id': chat_id
@ -986,11 +986,15 @@ def create_chat_invite_link(token, chat_id, expire_date, member_limit):
payload['expire_date'] = expire_date payload['expire_date'] = expire_date
if member_limit: if member_limit:
payload['member_limit'] = member_limit payload['member_limit'] = member_limit
if creates_join_request:
payload['creates_join_request'] = creates_join_request
if name:
payload['name'] = name
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
def edit_chat_invite_link(token, chat_id, invite_link, expire_date, member_limit): def edit_chat_invite_link(token, chat_id, invite_link, name, expire_date, member_limit, creates_join_request):
method_url = 'editChatInviteLink' method_url = 'editChatInviteLink'
payload = { payload = {
'chat_id': chat_id, 'chat_id': chat_id,
@ -1005,6 +1009,10 @@ def edit_chat_invite_link(token, chat_id, invite_link, expire_date, member_limit
if member_limit is not None: if member_limit is not None:
payload['member_limit'] = member_limit payload['member_limit'] = member_limit
if name:
payload['name'] = name
if creates_join_request:
payload['creates_join_request'] = creates_join_request
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')

View File

@ -107,6 +107,7 @@ class Update(JsonDeserializable):
poll_answer = PollAnswer.de_json(obj.get('poll_answer')) poll_answer = PollAnswer.de_json(obj.get('poll_answer'))
my_chat_member = ChatMemberUpdated.de_json(obj.get('my_chat_member')) my_chat_member = ChatMemberUpdated.de_json(obj.get('my_chat_member'))
chat_member = ChatMemberUpdated.de_json(obj.get('chat_member')) chat_member = ChatMemberUpdated.de_json(obj.get('chat_member'))
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, 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, chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer,
my_chat_member, chat_member) my_chat_member, chat_member)
@ -166,6 +167,22 @@ class ChatMemberUpdated(JsonDeserializable):
dif[key] = [old[key], new[key]] dif[key] = [old[key], new[key]]
return dif return dif
class ChatJoinRequest(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
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'])
return cls(**obj)
def __init__(self, chat, from_user, date, bio=None, invite_link=None, **kwargs):
self.chat = Chat = chat
self.from_user: User = from_user
self.date: int = date
self.bio: Optional[str] = bio
self.invite_link: Optional[ChatInviteLink] = invite_link
class WebhookInfo(JsonDeserializable): class WebhookInfo(JsonDeserializable):
@classmethod @classmethod
@ -2752,14 +2769,17 @@ class ChatInviteLink(JsonSerializable, JsonDeserializable, Dictionaryable):
obj['creator'] = User.de_json(obj['creator']) obj['creator'] = User.de_json(obj['creator'])
return cls(**obj) return cls(**obj)
def __init__(self, invite_link, creator, is_primary, is_revoked, def __init__(self, invite_link, creator, creates_join_request , is_primary, is_revoked,
expire_date=None, member_limit=None, **kwargs): name=None, expire_date=None, member_limit=None, pending_join_request_count=None, **kwargs):
self.invite_link: str = invite_link self.invite_link: str = invite_link
self.creator: User = creator self.creator: User = creator
self.creates_join_request: bool = creates_join_request
self.is_primary: bool = is_primary self.is_primary: bool = is_primary
self.is_revoked: bool = is_revoked self.is_revoked: bool = is_revoked
self.name: str = name
self.expire_date: int = expire_date self.expire_date: int = expire_date
self.member_limit: int = member_limit self.member_limit: int = member_limit
self.pending_join_request_count: int = pending_join_request_count
def to_json(self): def to_json(self):
return json.dumps(self.to_dict()) return json.dumps(self.to_dict())
@ -2769,12 +2789,17 @@ class ChatInviteLink(JsonSerializable, JsonDeserializable, Dictionaryable):
"invite_link": self.invite_link, "invite_link": self.invite_link,
"creator": self.creator.to_dict(), "creator": self.creator.to_dict(),
"is_primary": self.is_primary, "is_primary": self.is_primary,
"is_revoked": self.is_revoked "is_revoked": self.is_revoked,
"creates_join_request": self.creates_join_request
} }
if self.expire_date: if self.expire_date:
json_dict["expire_date"] = self.expire_date json_dict["expire_date"] = self.expire_date
if self.member_limit: if self.member_limit:
json_dict["member_limit"] = self.member_limit json_dict["member_limit"] = self.member_limit
if self.pending_join_request_count:
json_dict["pending_join_request_count"] = self.pending_join_request_count
if self.name:
json_dict["name"] = self.name
return json_dict return json_dict