|
45 | 45 | DispatcherHandlerStop,
|
46 | 46 | Handler,
|
47 | 47 | InlineQueryHandler,
|
| 48 | + StringCommandHandler, |
| 49 | + StringRegexHandler, |
| 50 | + TypeHandler, |
48 | 51 | )
|
49 | 52 | from telegram._utils.warnings import warn
|
50 | 53 | from telegram.ext._utils.promise import Promise
|
@@ -239,6 +242,14 @@ def __init__(
|
239 | 242 | map_to_parent: Dict[object, object] = None,
|
240 | 243 | run_async: bool = False,
|
241 | 244 | ):
|
| 245 | + # these imports need to be here because of circular import error otherwise |
| 246 | + from telegram.ext import ( # pylint: disable=import-outside-toplevel |
| 247 | + ShippingQueryHandler, |
| 248 | + PreCheckoutQueryHandler, |
| 249 | + PollHandler, |
| 250 | + PollAnswerHandler, |
| 251 | + ) |
| 252 | + |
242 | 253 | self.run_async = run_async
|
243 | 254 |
|
244 | 255 | self._entry_points = entry_points
|
@@ -283,49 +294,77 @@ def __init__(
|
283 | 294 | for state_handlers in states.values():
|
284 | 295 | all_handlers.extend(state_handlers)
|
285 | 296 |
|
286 |
| - if self.per_message: |
287 |
| - for handler in all_handlers: |
288 |
| - if not isinstance(handler, CallbackQueryHandler): |
289 |
| - warn( |
290 |
| - "If 'per_message=True', all entry points, state handlers, and fallbacks" |
291 |
| - " must be 'CallbackQueryHandler', since no other handlers " |
292 |
| - "have a message context.", |
293 |
| - stacklevel=2, |
294 |
| - ) |
295 |
| - break |
296 |
| - else: |
297 |
| - for handler in all_handlers: |
298 |
| - if isinstance(handler, CallbackQueryHandler): |
299 |
| - warn( |
300 |
| - "If 'per_message=False', 'CallbackQueryHandler' will not be " |
301 |
| - "tracked for every message.", |
302 |
| - stacklevel=2, |
303 |
| - ) |
304 |
| - break |
| 297 | + # this loop is going to warn the user about handlers which can work unexpected |
| 298 | + # in conversations |
305 | 299 |
|
306 |
| - if self.per_chat: |
307 |
| - for handler in all_handlers: |
308 |
| - if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)): |
309 |
| - warn( |
310 |
| - "If 'per_chat=True', 'InlineQueryHandler' can not be used, " |
311 |
| - "since inline queries have no chat context.", |
312 |
| - stacklevel=2, |
313 |
| - ) |
314 |
| - break |
| 300 | + # this link will be added to all warnings tied to per_* setting |
| 301 | + per_faq_link = ( |
| 302 | + " Read this FAQ entry to learn more about the per_* settings: https://git.io/JtcyU." |
| 303 | + ) |
315 | 304 |
|
316 |
| - if self.conversation_timeout: |
317 |
| - for handler in all_handlers: |
318 |
| - if isinstance(handler, self.__class__): |
319 |
| - warn( |
320 |
| - "Using `conversation_timeout` with nested conversations is currently not " |
321 |
| - "supported. You can still try to use it, but it will likely behave " |
322 |
| - "differently from what you expect.", |
323 |
| - stacklevel=2, |
324 |
| - ) |
325 |
| - break |
| 305 | + for handler in all_handlers: |
| 306 | + if isinstance(handler, (StringCommandHandler, StringRegexHandler)): |
| 307 | + warn( |
| 308 | + "The `ConversationHandler` only handles updates of type `telegram.Update`. " |
| 309 | + f"{handler.__class__.__name__} handles updates of type `str`.", |
| 310 | + stacklevel=2, |
| 311 | + ) |
| 312 | + elif isinstance(handler, TypeHandler) and not issubclass(handler.type, Update): |
| 313 | + warn( |
| 314 | + "The `ConversationHandler` only handles updates of type `telegram.Update`." |
| 315 | + f" The TypeHandler is set to handle {handler.type.__name__}.", |
| 316 | + stacklevel=2, |
| 317 | + ) |
| 318 | + elif isinstance(handler, PollHandler): |
| 319 | + warn( |
| 320 | + "PollHandler will never trigger in a conversation since it has no information " |
| 321 | + "about the chat or the user who voted in it. Do you mean the " |
| 322 | + "`PollAnswerHandler`?", |
| 323 | + stacklevel=2, |
| 324 | + ) |
| 325 | + |
| 326 | + elif self.per_chat and ( |
| 327 | + isinstance( |
| 328 | + handler, |
| 329 | + ( |
| 330 | + ShippingQueryHandler, |
| 331 | + InlineQueryHandler, |
| 332 | + ChosenInlineResultHandler, |
| 333 | + PreCheckoutQueryHandler, |
| 334 | + PollAnswerHandler, |
| 335 | + ), |
| 336 | + ) |
| 337 | + ): |
| 338 | + warn( |
| 339 | + f"Updates handled by {handler.__class__.__name__} only have information about " |
| 340 | + f"the user, so this handler won't ever be triggered if `per_chat=True`." |
| 341 | + f"{per_faq_link}", |
| 342 | + stacklevel=2, |
| 343 | + ) |
| 344 | + |
| 345 | + elif self.per_message and not isinstance(handler, CallbackQueryHandler): |
| 346 | + warn( |
| 347 | + "If 'per_message=True', all entry points, state handlers, and fallbacks" |
| 348 | + " must be 'CallbackQueryHandler', since no other handlers " |
| 349 | + f"have a message context.{per_faq_link}", |
| 350 | + stacklevel=2, |
| 351 | + ) |
| 352 | + elif not self.per_message and isinstance(handler, CallbackQueryHandler): |
| 353 | + warn( |
| 354 | + "If 'per_message=False', 'CallbackQueryHandler' will not be " |
| 355 | + f"tracked for every message.{per_faq_link}", |
| 356 | + stacklevel=2, |
| 357 | + ) |
| 358 | + |
| 359 | + if self.conversation_timeout and isinstance(handler, self.__class__): |
| 360 | + warn( |
| 361 | + "Using `conversation_timeout` with nested conversations is currently not " |
| 362 | + "supported. You can still try to use it, but it will likely behave " |
| 363 | + "differently from what you expect.", |
| 364 | + stacklevel=2, |
| 365 | + ) |
326 | 366 |
|
327 |
| - if self.run_async: |
328 |
| - for handler in all_handlers: |
| 367 | + if self.run_async: |
329 | 368 | handler.run_async = True
|
330 | 369 |
|
331 | 370 | @property
|
|
0 commit comments