Added usage example of errors_handler
Optimized generation of default/inline keyboard Customizing webhook path has become more flexible Updated aiogram version to 2.7 (BotAPI 4.7)
This commit is contained in:
parent
c3da772339
commit
2bbbee4651
11
.gitignore
vendored
11
.gitignore
vendored
|
@ -127,3 +127,14 @@ dmypy.json
|
||||||
|
|
||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
.idea/$CACHE_FILE$
|
||||||
|
.idea/.gitignore
|
||||||
|
.idea/aiogram-bot-template.iml
|
||||||
|
.idea/codeStyles/
|
||||||
|
.idea/deployment.xml
|
||||||
|
.idea/dictionaries
|
||||||
|
.idea/inspectionProfiles/
|
||||||
|
.idea/misc.xml
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/vagrant.xml
|
||||||
|
.idea/vcs.xml
|
||||||
|
|
3
bot.py
3
bot.py
|
@ -17,6 +17,7 @@ dp = Dispatcher(bot, storage=storage)
|
||||||
async def on_startup(web_app: web.Application):
|
async def on_startup(web_app: web.Application):
|
||||||
filters.setup(dp)
|
filters.setup(dp)
|
||||||
middlewares.setup(dp)
|
middlewares.setup(dp)
|
||||||
|
handlers.errors.setup(dp)
|
||||||
handlers.user.setup(dp)
|
handlers.user.setup(dp)
|
||||||
await dp.bot.delete_webhook()
|
await dp.bot.delete_webhook()
|
||||||
await dp.bot.set_webhook(config.WEBHOOK_URL)
|
await dp.bot.set_webhook(config.WEBHOOK_URL)
|
||||||
|
@ -37,5 +38,5 @@ async def execute(req: web.Request) -> web.Response:
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
app.on_startup.append(on_startup)
|
app.on_startup.append(on_startup)
|
||||||
app.add_routes([web.post('/webhook/{token}', execute)])
|
app.add_routes([web.post(config.WEBHOOK_PATH, execute)])
|
||||||
web.run_app(app, port=5151, host='localhost')
|
web.run_app(app, port=5151, host='localhost')
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
BOT_TOKEN = ''
|
BOT_TOKEN = ''
|
||||||
WEBHOOK_URL = ''
|
BASE_URL = 'https://example.com' # Webhook domain
|
||||||
|
WEBHOOK_PATH = f'/webhook/bot/{BOT_TOKEN}'
|
||||||
|
WEBHOOK_URL = f'{BASE_URL}{WEBHOOK_PATH}'
|
||||||
|
|
||||||
admins = []
|
admins = []
|
||||||
|
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
from . import errors
|
||||||
from . import user
|
from . import user
|
||||||
|
|
9
handlers/errors/__init__.py
Normal file
9
handlers/errors/__init__.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from aiogram import Dispatcher
|
||||||
|
from aiogram.utils import exceptions
|
||||||
|
|
||||||
|
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)
|
10
handlers/errors/not_modified.py
Normal file
10
handlers/errors/not_modified.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
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
|
15
keyboards/default/consts.py
Normal file
15
keyboards/default/consts.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
|
||||||
|
|
||||||
|
from . import utils
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultConstructor:
|
||||||
|
@staticmethod
|
||||||
|
def _create_kb(actions: List[str], schema: List[int]) -> ReplyKeyboardMarkup:
|
||||||
|
btns = []
|
||||||
|
for a in actions:
|
||||||
|
btns.append(KeyboardButton(a))
|
||||||
|
kb = utils.misc.arrange_default_schema(btns, schema)
|
||||||
|
return kb
|
1
keyboards/default/utils/__init__.py
Normal file
1
keyboards/default/utils/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from . import misc
|
17
keyboards/default/utils/misc.py
Normal file
17
keyboards/default/utils/misc.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
|
||||||
|
|
||||||
|
|
||||||
|
def arrange_default_schema(buttons: List[KeyboardButton], count: List[int]) -> ReplyKeyboardMarkup:
|
||||||
|
kb = ReplyKeyboardMarkup(resize_keyboard=True)
|
||||||
|
kb.row_width = max(count)
|
||||||
|
if sum(count) != len(buttons):
|
||||||
|
raise ValueError('Количество кнопок не совпадает со схемой')
|
||||||
|
tmplist = []
|
||||||
|
for a in count:
|
||||||
|
tmplist.append([])
|
||||||
|
for _ in range(a):
|
||||||
|
tmplist[-1].append(buttons.pop(0))
|
||||||
|
kb.keyboard = tmplist
|
||||||
|
return kb
|
|
@ -5,13 +5,13 @@ from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
||||||
|
|
||||||
def arrange_inline_schema(buttons: List[InlineKeyboardButton], count: List[int]) -> InlineKeyboardMarkup:
|
def arrange_inline_schema(buttons: List[InlineKeyboardButton], count: List[int]) -> InlineKeyboardMarkup:
|
||||||
kb = InlineKeyboardMarkup()
|
kb = InlineKeyboardMarkup()
|
||||||
btns = buttons
|
|
||||||
kb.row_width = max(count)
|
kb.row_width = max(count)
|
||||||
if sum(count) != len(buttons):
|
if sum(count) != len(buttons):
|
||||||
raise ValueError('Количество кнопок не совпадает со схемой')
|
raise ValueError('Количество кнопок не совпадает со схемой')
|
||||||
tmplist = [[InlineKeyboardButton('') for _ in range(count[i])] for i in range(len(count))]
|
tmplist = []
|
||||||
for a in range(len(tmplist)):
|
for a in count:
|
||||||
for b in range(len(tmplist[a])):
|
tmplist.append([])
|
||||||
tmplist[a][b] = btns.pop(0)
|
for _ in range(a):
|
||||||
|
tmplist[-1].append(buttons.pop(0))
|
||||||
kb.inline_keyboard = tmplist
|
kb.inline_keyboard = tmplist
|
||||||
return kb
|
return kb
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
aiomysql==0.0.20
|
aiomysql==0.0.20
|
||||||
aiogram==2.6.1
|
aiogram==2.7
|
||||||
aiohttp==3.6.2
|
aiohttp==3.6.2
|
||||||
aioredis==1.3.1
|
aioredis==1.3.1
|
||||||
loguru==0.4.1
|
loguru==0.4.1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user