Dispatcher init typos fix

This commit is contained in:
Suren Khorenyan 2018-02-14 22:14:09 +03:00
parent 70fb332fd3
commit 647472b32c

View file

@ -5,11 +5,12 @@ import logging
import time import time
import typing import typing
from .filters import CommandsFilter, ContentTypeFilter, ExceptionsFilter, RegexpFilter, USER_STATE, \ from .filters import CommandsFilter, ContentTypeFilter, ExceptionsFilter, RegexpFilter, \
generate_default_filters USER_STATE, generate_default_filters
from .handler import CancelHandler, Handler, SkipHandler from .handler import CancelHandler, Handler, SkipHandler
from .middlewares import MiddlewareManager from .middlewares import MiddlewareManager
from .storage import BaseStorage, DELTA, DisabledStorage, EXCEEDED_COUNT, FSMContext, LAST_CALL, RATE_LIMIT, RESULT from .storage import BaseStorage, DELTA, DisabledStorage, EXCEEDED_COUNT, FSMContext, \
LAST_CALL, RATE_LIMIT, RESULT
from .webhook import BaseResponse from .webhook import BaseResponse
from ..bot import Bot from ..bot import Bot
from ..types.message import ContentType from ..types.message import ContentType
@ -30,9 +31,8 @@ class Dispatcher:
""" """
Simple Updates dispatcher Simple Updates dispatcher
It will be can process incoming updates, messages, edited messages, channel posts, edited channels posts, It will process incoming updates: messages, edited messages, channel posts, edited channel posts,
inline query, chosen inline result, callback query, shipping query, pre-checkout query. inline queries, chosen inline results, callback queries, shipping queries, pre-checkout queries.
Provide next step handler and etc.
""" """
def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage] = None, def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage] = None,
@ -181,7 +181,6 @@ class Dispatcher:
state=state) state=state)
return await self.pre_checkout_query_handlers.notify(update.pre_checkout_query) return await self.pre_checkout_query_handlers.notify(update.pre_checkout_query)
except Exception as e: except Exception as e:
success = False
err = await self.errors_handlers.notify(self, update, e) err = await self.errors_handlers.notify(self, update, e)
if err: if err:
return err return err
@ -293,7 +292,7 @@ class Dispatcher:
async def wait_closed(self): async def wait_closed(self):
""" """
Wait closing the long polling Wait for the long-polling to close
:return: :return:
""" """
@ -305,7 +304,7 @@ class Dispatcher:
def is_polling(self): def is_polling(self):
""" """
Check polling is enabled? Check if polling is enabled
:return: :return:
""" """
@ -314,18 +313,18 @@ class Dispatcher:
def register_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None, def register_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None,
state=None, custom_filters=None, run_task=None, **kwargs): state=None, custom_filters=None, run_task=None, **kwargs):
""" """
You can register messages handler by this method Register handler for message
.. code-block:: python3 .. code-block:: python3
# This handler works only is state is None (by default). # This handler works only if state is None (by default).
dp.register_message_handler(cmd_start, commands=['start', 'about']) dp.register_message_handler(cmd_start, commands=['start', 'about'])
dp.register_message_handler(entry_point, commands=['setup']) dp.register_message_handler(entry_point, commands=['setup'])
# That handler works only if current state is "first_step" # This handler works only if current state is "first_step"
dp.register_message_handler(step_handler_1, state="first_step") dp.register_message_handler(step_handler_1, state="first_step")
# If you want to handle all states by one handler then use state="*". # If you want to handle all states by one handler then use `state="*"`.
dp.register_message_handler(cancel_handler, commands=['cancel'], state="*") dp.register_message_handler(cancel_handler, commands=['cancel'], state="*")
dp.register_message_handler(cancel_handler, func=lambda msg: msg.text.lower() == 'cancel', state="*") dp.register_message_handler(cancel_handler, func=lambda msg: msg.text.lower() == 'cancel', state="*")
@ -357,7 +356,7 @@ class Dispatcher:
def message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None, def message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None,
run_task=None, **kwargs): run_task=None, **kwargs):
""" """
Decorator for messages handler Decorator for message handler
Examples: Examples:
@ -375,6 +374,13 @@ class Dispatcher:
@dp.messages_handler(rexexp='^[a-z]+-[0-9]+') @dp.messages_handler(rexexp='^[a-z]+-[0-9]+')
async def msg_handler(message: types.Message): async def msg_handler(message: types.Message):
Filter messages by command regular expression:
.. code-block:: python3
@dp.message_handler(filters.RegexpCommandsFilter(regexp_commands=['item_([0-9]*)']))
async def send_welcome(message: types.Message):
Filter by content type: Filter by content type:
.. code-block:: python3 .. code-block:: python3
@ -431,7 +437,7 @@ class Dispatcher:
def register_edited_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None, def register_edited_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None,
state=None, custom_filters=None, run_task=None, **kwargs): state=None, custom_filters=None, run_task=None, **kwargs):
""" """
Analog of message_handler but only for edited messages Register handler for edited message
:param callback: :param callback:
:param commands: list of commands :param commands: list of commands
@ -462,7 +468,7 @@ class Dispatcher:
def edited_message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, def edited_message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None,
state=None, run_task=None, **kwargs): state=None, run_task=None, **kwargs):
""" """
Analog of message_handler but only for edited messages Decorator for edited message handler
You can use combination of different handlers You can use combination of different handlers
@ -494,7 +500,7 @@ class Dispatcher:
def register_channel_post_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None, def register_channel_post_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None,
state=None, custom_filters=None, run_task=None, **kwargs): state=None, custom_filters=None, run_task=None, **kwargs):
""" """
Register channels posts handler Register handler for channel post
:param callback: :param callback:
:param commands: list of commands :param commands: list of commands
@ -525,7 +531,7 @@ class Dispatcher:
def channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, def channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None,
state=None, run_task=None, **kwargs): state=None, run_task=None, **kwargs):
""" """
Register channels posts handler Decorator for channel post handler
:param commands: list of commands :param commands: list of commands
:param regexp: REGEXP :param regexp: REGEXP
@ -549,7 +555,7 @@ class Dispatcher:
def register_edited_channel_post_handler(self, callback, *, commands=None, regexp=None, content_types=None, def register_edited_channel_post_handler(self, callback, *, commands=None, regexp=None, content_types=None,
func=None, state=None, custom_filters=None, run_task=None, **kwargs): func=None, state=None, custom_filters=None, run_task=None, **kwargs):
""" """
Register handler for edited channels posts Register handler for edited channel post
:param callback: :param callback:
:param commands: list of commands :param commands: list of commands
@ -580,7 +586,7 @@ class Dispatcher:
def edited_channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, def edited_channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None,
state=None, run_task=None, **kwargs): state=None, run_task=None, **kwargs):
""" """
Register handler for edited channels posts Decorator for edited channel post handler
:param commands: list of commands :param commands: list of commands
:param regexp: REGEXP :param regexp: REGEXP
@ -603,7 +609,7 @@ class Dispatcher:
def register_inline_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None, **kwargs): def register_inline_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None, **kwargs):
""" """
Handle inline query Register handler for inline query
Example: Example:
@ -631,7 +637,7 @@ class Dispatcher:
def inline_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs): def inline_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs):
""" """
Handle inline query Decorator for inline query handler
Example: Example:
@ -658,7 +664,7 @@ class Dispatcher:
def register_chosen_inline_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None, def register_chosen_inline_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None,
**kwargs): **kwargs):
""" """
Register chosen inline handler Register handler for chosen inline query
Example: Example:
@ -686,7 +692,7 @@ class Dispatcher:
def chosen_inline_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs): def chosen_inline_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs):
""" """
Register chosen inline handler Decorator for chosen inline query handler
Example: Example:
@ -713,7 +719,7 @@ class Dispatcher:
def register_callback_query_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None, def register_callback_query_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None,
**kwargs): **kwargs):
""" """
Add callback query handler Register handler for callback query
Example: Example:
@ -740,7 +746,7 @@ class Dispatcher:
def callback_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs): def callback_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs):
""" """
Add callback query handler Decorator for callback query handler
Example: Example:
@ -766,7 +772,7 @@ class Dispatcher:
def register_shipping_query_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None, def register_shipping_query_handler(self, callback, *, func=None, state=None, custom_filters=None, run_task=None,
**kwargs): **kwargs):
""" """
Add shipping query handler Register handler for shipping query
Example: Example:
@ -793,7 +799,7 @@ class Dispatcher:
def shipping_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs): def shipping_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs):
""" """
Add shipping query handler Decorator for shipping query handler
Example: Example:
@ -819,7 +825,7 @@ class Dispatcher:
def register_pre_checkout_query_handler(self, callback, *, func=None, state=None, custom_filters=None, def register_pre_checkout_query_handler(self, callback, *, func=None, state=None, custom_filters=None,
run_task=None, **kwargs): run_task=None, **kwargs):
""" """
Add shipping query handler Register handler for pre-checkout query
Example: Example:
@ -846,7 +852,7 @@ class Dispatcher:
def pre_checkout_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs): def pre_checkout_query_handler(self, *custom_filters, func=None, state=None, run_task=None, **kwargs):
""" """
Add shipping query handler Decorator for pre-checkout query handler
Example: Example:
@ -871,7 +877,7 @@ class Dispatcher:
def register_errors_handler(self, callback, *, func=None, exception=None, run_task=None): def register_errors_handler(self, callback, *, func=None, exception=None, run_task=None):
""" """
Register errors handler Register handler for errors
:param callback: :param callback:
:param func: :param func:
@ -887,7 +893,7 @@ class Dispatcher:
def errors_handler(self, func=None, exception=None, run_task=None): def errors_handler(self, func=None, exception=None, run_task=None):
""" """
Decorator for registering errors handler Decorator for errors handler
:param func: :param func:
:param exception: you can make handler for specific errors type :param exception: you can make handler for specific errors type
@ -932,10 +938,10 @@ class Dispatcher:
async def throttle(self, key, *, rate=None, user=None, chat=None, no_error=None) -> bool: async def throttle(self, key, *, rate=None, user=None, chat=None, no_error=None) -> bool:
""" """
Execute throttling manager. Execute throttling manager.
Return True limit is not exceeded otherwise raise ThrottleError or return False Returns True if limit has not exceeded otherwise raises ThrottleError or returns False
:param key: key in storage :param key: key in storage
:param rate: limit (by default is equals with default rate limit) :param rate: limit (by default is equal to default rate limit)
:param user: user id :param user: user id
:param chat: chat id :param chat: chat id
:param no_error: return boolean value instead of raising error :param no_error: return boolean value instead of raising error
@ -983,7 +989,7 @@ class Dispatcher:
await self.storage.set_bucket(chat=chat, user=user, bucket=bucket) await self.storage.set_bucket(chat=chat, user=user, bucket=bucket)
if not result and not no_error: if not result and not no_error:
# Raise if that is allowed # Raise if it is allowed
raise Throttled(key=key, chat=chat, user=user, **data) raise Throttled(key=key, chat=chat, user=user, **data)
return result return result
@ -1035,7 +1041,7 @@ class Dispatcher:
def async_task(self, func): def async_task(self, func):
""" """
Execute handler as task and return None. Execute handler as task and return None.
Use that decorator for slow handlers (with timeouts) Use this decorator for slow handlers (with timeouts)
.. code-block:: python3 .. code-block:: python3