Deleted mysql support, redis password, webhook, keyboard constructors, filters, loguru.

Added support of decorators in handlers
Added function to notify admins on startup
This commit is contained in:
Kostiantyn Kriuchkov
2020-04-14 01:50:57 +03:00
parent 13ff8d0478
commit eab45a5088
30 changed files with 141 additions and 223 deletions

View File

@@ -1,2 +1,4 @@
from . import errors
from . import user
from .errors import dp
from .users import dp
__all__ = ["dp"]

View File

View File

@@ -1,9 +1,3 @@
from aiogram import Dispatcher
from aiogram.utils import exceptions
from .error_handler import dp
from .not_modified import message_not_modified, message_to_delete_not_found
def setup(dp: Dispatcher):
dp.register_errors_handler(message_not_modified, exception=exceptions.MessageNotModified)
dp.register_errors_handler(message_to_delete_not_found, exception=exceptions.MessageToDeleteNotFound)
__all__ = ["dp"]

View File

@@ -0,0 +1,56 @@
import logging
from bot import dp
@dp.errors_handler()
async def errors_handler(update, exception):
"""
Exceptions handler. Catches all exceptions within task factory tasks.
:param dispatcher:
:param update:
:param exception:
:return: stdout logging
"""
from aiogram.utils.exceptions import (Unauthorized, InvalidQueryID, TelegramAPIError,
CantDemoteChatCreator, MessageNotModified, MessageToDeleteNotFound,
MessageTextIsEmpty, RetryAfter,
CantParseEntities, MessageCantBeDeleted)
if isinstance(exception, CantDemoteChatCreator):
logging.debug("Can't demote chat creator")
return True
if isinstance(exception, MessageNotModified):
logging.debug('Message is not modified')
return True
if isinstance(exception, MessageCantBeDeleted):
logging.debug('Message cant be deleted')
return True
if isinstance(exception, MessageToDeleteNotFound):
logging.debug('Message to delete not found')
return True
if isinstance(exception, MessageTextIsEmpty):
logging.debug('MessageTextIsEmpty')
return True
if isinstance(exception, Unauthorized):
logging.info(f'Unauthorized: {exception}')
return True
if isinstance(exception, InvalidQueryID):
logging.exception(f'InvalidQueryID: {exception} \nUpdate: {update}')
return True
if isinstance(exception, TelegramAPIError):
logging.exception(f'TelegramAPIError: {exception} \nUpdate: {update}')
return True
if isinstance(exception, RetryAfter):
logging.exception(f'RetryAfter: {exception} \nUpdate: {update}')
return True
if isinstance(exception, CantParseEntities):
logging.exception(f'CantParseEntities: {exception} \nUpdate: {update}')
return True
logging.exception(f'Update: {update} \n{exception}')

View File

@@ -1,10 +0,0 @@
from aiogram import types
from aiogram.utils import exceptions
async def message_not_modified(update: types.Update, error: exceptions.MessageNotModified):
return True
async def message_to_delete_not_found(update: types.Update, error: exceptions.MessageToDeleteNotFound):
return True

View File

View File

@@ -1,10 +0,0 @@
from aiogram import Dispatcher
from aiogram.dispatcher.filters import CommandStart, CommandHelp
from .help import bot_help
from .start import bot_start
def setup(dp: Dispatcher):
dp.register_message_handler(bot_start, CommandStart())
dp.register_message_handler(bot_help, CommandHelp())

View File

@@ -1,5 +0,0 @@
from aiogram import types
async def bot_start(msg: types.Message):
await msg.answer(f'Привет, {msg.from_user.full_name}!')

View File

@@ -0,0 +1,5 @@
from .help import dp
from .start import dp
from .echo import dp
__all__ = ["dp"]

7
handlers/users/echo.py Normal file
View File

@@ -0,0 +1,7 @@
from aiogram import types
from bot import dp
@dp.message_handler()
async def bot_start(message: types.Message):
await message.answer(message.text)

View File

@@ -1,13 +1,16 @@
from aiogram import types
from aiogram.dispatcher.filters.builtin import CommandStart
from bot import dp
from utils.misc import rate_limit
@rate_limit(5, 'help')
async def bot_help(msg: types.Message):
@dp.message_handler(CommandStart())
async def bot_help(message: types.Message):
text = [
'Список команд: ',
'/start - Начать диалог',
'/help - Получить справку'
]
await msg.answer('\n'.join(text))
await message.answer('\n'.join(text))

9
handlers/users/start.py Normal file
View File

@@ -0,0 +1,9 @@
from aiogram import types
from aiogram.dispatcher.filters.builtin import CommandStart
from bot import dp
@dp.message_handler(CommandStart())
async def bot_start(message: types.Message):
await message.answer(f'Привет, {message.from_user.full_name}!')