1717# You should have received a copy of the GNU Lesser Public License
1818# along with this program. If not, see [http://www.gnu.org/licenses/].
1919"""This module contains the base class for handlers as used by the Dispatcher."""
20+ import warnings
21+
22+ from telegram .utils .inspection import get_positional_arguments
2023
2124
2225class Handler (object ):
2326 """The base class for all update handlers. Create custom handlers by inheriting from it.
2427
2528 Attributes:
2629 callback (:obj:`callable`): The callback function for this handler.
30+ autowire (:obj:`bool`): Optional. Determines whether objects will be passed to the
31+ callback function automatically.
2732 pass_update_queue (:obj:`bool`): Optional. Determines whether ``update_queue`` will be
2833 passed to the callback function.
2934 pass_job_queue (:obj:`bool`): Optional. Determines whether ``job_queue`` will be passed to
@@ -43,6 +48,11 @@ class Handler(object):
4348 callback (:obj:`callable`): A function that takes ``bot, update`` as positional arguments.
4449 It will be called when the :attr:`check_update` has determined that an update should be
4550 processed by this handler.
51+ autowire (:obj:`bool`, optional): If set to ``True``, your callback handler will be
52+ inspected for positional arguments and pass objects whose names match any of the
53+ ``pass_*`` flags of this Handler. Using any ``pass_*`` argument in conjunction with
54+ ``autowire`` will yield
55+ a warning.
4656 pass_update_queue (:obj:`bool`, optional): If set to ``True``, a keyword argument called
4757 ``update_queue`` will be passed to the callback function. It will be the ``Queue``
4858 instance used by the :class:`telegram.ext.Updater` and :class:`telegram.ext.Dispatcher`
@@ -60,11 +70,15 @@ class Handler(object):
6070
6171 def __init__ (self ,
6272 callback ,
73+ autowire = False ,
6374 pass_update_queue = False ,
6475 pass_job_queue = False ,
6576 pass_user_data = False ,
6677 pass_chat_data = False ):
6778 self .callback = callback
79+ self .autowire = autowire
80+ if self .autowire and any ((pass_update_queue , pass_job_queue , pass_user_data , pass_chat_data )):
81+ warnings .warn ('If `autowire` is set to `True`, it is unnecessary to provide any `pass_*` flags.' )
6882 self .pass_update_queue = pass_update_queue
6983 self .pass_job_queue = pass_job_queue
7084 self .pass_user_data = pass_user_data
@@ -108,18 +122,28 @@ def collect_optional_args(self, dispatcher, update=None):
108122 """
109123 optional_args = dict ()
110124
111- if self .pass_update_queue :
112- optional_args ['update_queue' ] = dispatcher .update_queue
113- if self .pass_job_queue :
114- optional_args ['job_queue' ] = dispatcher .job_queue
115- if self .pass_user_data or self .pass_chat_data :
116- chat = update .effective_chat
117- user = update .effective_user
118-
125+ if self .autowire :
126+ callback_args = get_positional_arguments (self .callback )
127+ if 'update_queue' in callback_args :
128+ optional_args ['update_queue' ] = dispatcher .update_queue
129+ if 'job_queue' in callback_args :
130+ optional_args ['job_queue' ] = dispatcher .job_queue
131+ if 'user_data' in callback_args :
132+ user = update .effective_user
133+ optional_args ['user_data' ] = dispatcher .user_data [user .id if user else None ]
134+ if 'chat_data' in callback_args :
135+ chat = update .effective_chat
136+ optional_args ['chat_data' ] = dispatcher .chat_data [chat .id if chat else None ]
137+ else :
138+ if self .pass_update_queue :
139+ optional_args ['update_queue' ] = dispatcher .update_queue
140+ if self .pass_job_queue :
141+ optional_args ['job_queue' ] = dispatcher .job_queue
119142 if self .pass_user_data :
143+ user = update .effective_user
120144 optional_args ['user_data' ] = dispatcher .user_data [user .id if user else None ]
121-
122145 if self .pass_chat_data :
146+ chat = update .effective_chat
123147 optional_args ['chat_data' ] = dispatcher .chat_data [chat .id if chat else None ]
124148
125149 return optional_args
0 commit comments