Skip to content

Commit e1c9223

Browse files
Chat Boosts full support for handlers;
1 parent 350f7fe commit e1c9223

5 files changed

Lines changed: 514 additions & 6 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def __init__(
229229
self.my_chat_member_handlers = []
230230
self.chat_member_handlers = []
231231
self.chat_join_request_handlers = []
232+
self.chat_boost_handlers = []
233+
self.removed_chat_boost_handlers = []
232234
self.custom_filters = {}
233235
self.state_handlers = []
234236

@@ -684,6 +686,8 @@ def process_new_updates(self, updates: List[types.Update]):
684686
new_my_chat_members = None
685687
new_chat_members = None
686688
new_chat_join_request = None
689+
removed_chat_boosts = None
690+
chat_boosts = None
687691

688692
for update in updates:
689693
if apihelper.ENABLE_MIDDLEWARE and not self.use_class_middlewares:
@@ -747,6 +751,12 @@ def process_new_updates(self, updates: List[types.Update]):
747751
if update.message_reaction_count:
748752
if message_reaction_counts is None: message_reaction_counts = []
749753
message_reaction_counts.append(update.message_reaction_count)
754+
if update.chat_boost:
755+
if chat_boosts is None: chat_boosts = []
756+
chat_boosts.append(update.chat_boost)
757+
if update.removed_chat_boost:
758+
if removed_chat_boosts is None: removed_chat_boosts = []
759+
removed_chat_boosts.append(update.removed_chat_boost)
750760

751761
if new_messages:
752762
self.process_new_messages(new_messages)
@@ -780,6 +790,10 @@ def process_new_updates(self, updates: List[types.Update]):
780790
self.process_new_message_reaction(new_message_reactions)
781791
if message_reaction_counts:
782792
self.process_new_message_reaction_count(message_reaction_counts)
793+
if chat_boosts:
794+
self.process_new_chat_boost(chat_boosts)
795+
if removed_chat_boosts:
796+
self.process_new_removed_chat_boost(removed_chat_boosts)
783797

784798
def process_new_messages(self, new_messages):
785799
"""
@@ -880,6 +894,19 @@ def process_new_chat_join_request(self, chat_join_request):
880894
"""
881895
self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request, 'chat_join_request')
882896

897+
def process_new_chat_boost(self, chat_boosts):
898+
"""
899+
:meta private:
900+
"""
901+
self._notify_command_handlers(self.chat_boost_handlers, chat_boosts, 'chat_boost')
902+
903+
def process_new_removed_chat_boost(self, removed_chat_boosts):
904+
"""
905+
:meta private:
906+
"""
907+
self._notify_command_handlers(self.removed_chat_boost_handlers, removed_chat_boosts, 'removed_chat_boost')
908+
909+
883910
def process_middlewares(self, update):
884911
"""
885912
:meta private:
@@ -7209,6 +7236,105 @@ def register_chat_join_request_handler(self, callback: Callable, func: Optional[
72097236
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
72107237
self.add_chat_join_request_handler(handler_dict)
72117238

7239+
def chat_boost_handler(self, func=None, **kwargs):
7240+
"""
7241+
Handles new incoming chat boost state.
7242+
it passes :class:`telebot.types.ChatBoostUpdated` object.
7243+
7244+
:param func: Function executed as a filter
7245+
:type func: :obj:`function`
7246+
7247+
:param kwargs: Optional keyword arguments(custom filters)
7248+
:return: None
7249+
"""
7250+
def decorator(handler):
7251+
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
7252+
self.add_chat_boost_handler(handler_dict)
7253+
return handler
7254+
7255+
return decorator
7256+
7257+
def add_chat_boost_handler(self, handler_dict):
7258+
"""
7259+
Adds a chat_boost handler.
7260+
Note that you should use register_chat_boost_handler to add chat_boost_handler to the bot.
7261+
7262+
:meta private:
7263+
7264+
:param handler_dict:
7265+
:return:
7266+
"""
7267+
self.chat_boost_handlers.append(handler_dict)
7268+
7269+
def register_chat_boost_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot:Optional[bool]=False, **kwargs):
7270+
"""
7271+
Registers chat boost handler.
7272+
7273+
:param callback: function to be called
7274+
:type callback: :obj:`function`
7275+
7276+
:param func: Function executed as a filter
7277+
:type func: :obj:`function`
7278+
7279+
:param pass_bot: True if you need to pass TeleBot instance to handler(useful for separating handlers into different files)
7280+
7281+
:param kwargs: Optional keyword arguments(custom filters)
7282+
7283+
:return: None
7284+
"""
7285+
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
7286+
self.add_chat_boost_handler(handler_dict)
7287+
7288+
def removed_chat_boost_handler(self, func=None, **kwargs):
7289+
"""
7290+
Handles new incoming chat boost state.
7291+
it passes :class:`telebot.types.ChatBoostRemoved` object.
7292+
7293+
:param func: Function executed as a filter
7294+
:type func: :obj:`function`
7295+
7296+
:param kwargs: Optional keyword arguments(custom filters)
7297+
:return: None
7298+
"""
7299+
def decorator(handler):
7300+
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
7301+
self.add_removed_chat_boost_handler(handler_dict)
7302+
return handler
7303+
7304+
return decorator
7305+
7306+
def add_removed_chat_boost_handler(self, handler_dict):
7307+
"""
7308+
Adds a removed_chat_boost handler.
7309+
Note that you should use register_removed_chat_boost_handler to add removed_chat_boost_handler to the bot.
7310+
7311+
:meta private:
7312+
7313+
:param handler_dict:
7314+
:return:
7315+
"""
7316+
self.removed_chat_boost_handlers.append(handler_dict)
7317+
7318+
def register_removed_chat_boost_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot:Optional[bool]=False, **kwargs):
7319+
"""
7320+
Registers removed chat boost handler.
7321+
7322+
:param callback: function to be called
7323+
:type callback: :obj:`function`
7324+
7325+
:param func: Function executed as a filter
7326+
:type func: :obj:`function`
7327+
7328+
:param pass_bot: True if you need to pass TeleBot instance to handler(useful for separating handlers into different files)
7329+
7330+
:param kwargs: Optional keyword arguments(custom filters)
7331+
7332+
:return: None
7333+
"""
7334+
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
7335+
self.add_removed_chat_boost_handler(handler_dict)
7336+
7337+
72127338
def _test_message_handler(self, message_handler, message):
72137339
"""
72147340
Test message handler

‎telebot/async_telebot.py‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def __init__(self, token: str, parse_mode: Optional[str]=None, offset: Optional[
171171
self.my_chat_member_handlers = []
172172
self.chat_member_handlers = []
173173
self.chat_join_request_handlers = []
174+
self.removed_chat_boost_handlers = []
175+
self.chat_boost_handlers = []
174176
self.custom_filters = {}
175177
self.state_handlers = []
176178
self.middlewares = []
@@ -590,6 +592,8 @@ async def process_new_updates(self, updates: List[types.Update]):
590592
new_my_chat_members = None
591593
new_chat_members = None
592594
chat_join_request = None
595+
removed_chat_boost_handlers = None
596+
chat_boost_handlers = None
593597
for update in updates:
594598
logger.debug('Processing updates: {0}'.format(update))
595599
if update.message:
@@ -640,6 +644,12 @@ async def process_new_updates(self, updates: List[types.Update]):
640644
if update.message_reaction_count:
641645
if new_message_reaction_count_handlers is None: new_message_reaction_count_handlers = []
642646
new_message_reaction_count_handlers.append(update.message_reaction_count)
647+
if update.chat_boost:
648+
if chat_boost_handlers is None: chat_boost_handlers = []
649+
chat_boost_handlers.append(update.chat_boost)
650+
if update.removed_chat_boost:
651+
if removed_chat_boost_handlers is None: removed_chat_boost_handlers = []
652+
removed_chat_boost_handlers.append(update.removed_chat_boost)
643653

644654

645655
if new_messages:
@@ -674,6 +684,8 @@ async def process_new_updates(self, updates: List[types.Update]):
674684
await self.process_new_message_reaction(new_message_reactions)
675685
if new_message_reaction_count_handlers:
676686
await self.process_new_message_reaction_count(new_message_reaction_count_handlers)
687+
if chat_boost_handlers:
688+
await self.process_new_chat_boost(chat_boost_handlers)
677689

678690
async def process_new_messages(self, new_messages):
679691
"""
@@ -772,6 +784,18 @@ async def process_chat_join_request(self, chat_join_request):
772784
"""
773785
await self._process_updates(self.chat_join_request_handlers, chat_join_request, 'chat_join_request')
774786

787+
async def process_new_chat_boost(self, chat_boost):
788+
"""
789+
:meta private:
790+
"""
791+
await self._process_updates(self.chat_boost_handlers, chat_boost, 'chat_boost')
792+
793+
async def process_new_removed_chat_boost(self, removed_chat_boost):
794+
"""
795+
:meta private:
796+
"""
797+
await self._process_updates(self.removed_chat_boost_handlers, removed_chat_boost, 'removed_chat_boost')
798+
775799
async def _get_middlewares(self, update_type):
776800
"""
777801
:meta private:
@@ -2002,6 +2026,105 @@ def register_chat_join_request_handler(self, callback: Callable[[Any], Awaitable
20022026
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
20032027
self.add_chat_join_request_handler(handler_dict)
20042028

2029+
2030+
def chat_boost_handler(self, func=None, **kwargs):
2031+
"""
2032+
Handles new incoming chat boost state.
2033+
it passes :class:`telebot.types.ChatBoostUpdated` object.
2034+
2035+
:param func: Function executed as a filter
2036+
:type func: :obj:`function`
2037+
2038+
:param kwargs: Optional keyword arguments(custom filters)
2039+
:return: None
2040+
"""
2041+
def decorator(handler):
2042+
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
2043+
self.add_chat_boost_handler(handler_dict)
2044+
return handler
2045+
2046+
return decorator
2047+
2048+
def add_chat_boost_handler(self, handler_dict):
2049+
"""
2050+
Adds a chat_boost handler.
2051+
Note that you should use register_chat_boost_handler to add chat_boost_handler to the bot.
2052+
2053+
:meta private:
2054+
2055+
:param handler_dict:
2056+
:return:
2057+
"""
2058+
self.chat_boost_handlers.append(handler_dict)
2059+
2060+
def register_chat_boost_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot:Optional[bool]=False, **kwargs):
2061+
"""
2062+
Registers chat boost handler.
2063+
2064+
:param callback: function to be called
2065+
:type callback: :obj:`function`
2066+
2067+
:param func: Function executed as a filter
2068+
:type func: :obj:`function`
2069+
2070+
:param pass_bot: True if you need to pass TeleBot instance to handler(useful for separating handlers into different files)
2071+
2072+
:param kwargs: Optional keyword arguments(custom filters)
2073+
2074+
:return: None
2075+
"""
2076+
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
2077+
self.add_chat_boost_handler(handler_dict)
2078+
2079+
def removed_chat_boost_handler(self, func=None, **kwargs):
2080+
"""
2081+
Handles new incoming chat boost state.
2082+
it passes :class:`telebot.types.ChatBoostRemoved` object.
2083+
2084+
:param func: Function executed as a filter
2085+
:type func: :obj:`function`
2086+
2087+
:param kwargs: Optional keyword arguments(custom filters)
2088+
:return: None
2089+
"""
2090+
def decorator(handler):
2091+
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
2092+
self.add_removed_chat_boost_handler(handler_dict)
2093+
return handler
2094+
2095+
return decorator
2096+
2097+
def add_removed_chat_boost_handler(self, handler_dict):
2098+
"""
2099+
Adds a removed_chat_boost handler.
2100+
Note that you should use register_removed_chat_boost_handler to add removed_chat_boost_handler to the bot.
2101+
2102+
:meta private:
2103+
2104+
:param handler_dict:
2105+
:return:
2106+
"""
2107+
self.removed_chat_boost_handlers.append(handler_dict)
2108+
2109+
def register_removed_chat_boost_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot:Optional[bool]=False, **kwargs):
2110+
"""
2111+
Registers removed chat boost handler.
2112+
2113+
:param callback: function to be called
2114+
:type callback: :obj:`function`
2115+
2116+
:param func: Function executed as a filter
2117+
:type func: :obj:`function`
2118+
2119+
:param pass_bot: True if you need to pass TeleBot instance to handler(useful for separating handlers into different files)
2120+
2121+
:param kwargs: Optional keyword arguments(custom filters)
2122+
2123+
:return: None
2124+
"""
2125+
handler_dict = self._build_handler_dict(callback, func=func, pass_bot=pass_bot, **kwargs)
2126+
self.add_removed_chat_boost_handler(handler_dict)
2127+
20052128
@staticmethod
20062129
def _build_handler_dict(handler, pass_bot=False, **filters):
20072130
"""

0 commit comments

Comments
 (0)