mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-15 03:08:51 +00:00
Some callback_data_factory.py refactor
This commit is contained in:
parent
61e1015c1e
commit
846f83f117
2 changed files with 16 additions and 14 deletions
|
|
@ -339,7 +339,7 @@ class Executor:
|
||||||
async def _skip_updates(self):
|
async def _skip_updates(self):
|
||||||
await self.dispatcher.reset_webhook(True)
|
await self.dispatcher.reset_webhook(True)
|
||||||
await self.dispatcher.skip_updates()
|
await self.dispatcher.skip_updates()
|
||||||
log.warning(f"Updates are skipped successfully.")
|
log.warning(f'Updates were skipped successfully.')
|
||||||
|
|
||||||
async def _welcome(self):
|
async def _welcome(self):
|
||||||
user = await self.dispatcher.bot.me
|
user = await self.dispatcher.bot.me
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import uuid
|
import uuid
|
||||||
|
|
@ -21,12 +20,12 @@ dp.middleware.setup(LoggingMiddleware())
|
||||||
|
|
||||||
POSTS = {
|
POSTS = {
|
||||||
str(uuid.uuid4()): {
|
str(uuid.uuid4()): {
|
||||||
'title': f"Post {index}",
|
'title': f'Post {index}',
|
||||||
'body': 'Lorem ipsum dolor sit amet, '
|
'body': 'Lorem ipsum dolor sit amet, '
|
||||||
'consectetur adipiscing elit, '
|
'consectetur adipiscing elit, '
|
||||||
'sed do eiusmod tempor incididunt ut '
|
'sed do eiusmod tempor incididunt ut '
|
||||||
'labore et dolore magna aliqua',
|
'labore et dolore magna aliqua',
|
||||||
'votes': random.randint(-2, 5)
|
'votes': random.randint(-2, 5),
|
||||||
} for index in range(1, 6)
|
} for index in range(1, 6)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,21 +41,24 @@ def get_keyboard() -> types.InlineKeyboardMarkup:
|
||||||
markup.add(
|
markup.add(
|
||||||
types.InlineKeyboardButton(
|
types.InlineKeyboardButton(
|
||||||
post['title'],
|
post['title'],
|
||||||
callback_data=posts_cb.new(id=post_id, action='view'))
|
callback_data=posts_cb.new(id=post_id, action='view')),
|
||||||
)
|
)
|
||||||
return markup
|
return markup
|
||||||
|
|
||||||
|
|
||||||
def format_post(post_id: str, post: dict) -> (str, types.InlineKeyboardMarkup):
|
def format_post(post_id: str, post: dict) -> (str, types.InlineKeyboardMarkup):
|
||||||
text = f"{md.hbold(post['title'])}\n" \
|
text = md.text(
|
||||||
f"{md.quote_html(post['body'])}\n" \
|
md.hbold(post['title']),
|
||||||
f"\n" \
|
md.quote_html(post['body']),
|
||||||
f"Votes: {post['votes']}"
|
'', # just new empty line
|
||||||
|
f"Votes: {post['votes']}",
|
||||||
|
sep = '\n',
|
||||||
|
)
|
||||||
|
|
||||||
markup = types.InlineKeyboardMarkup()
|
markup = types.InlineKeyboardMarkup()
|
||||||
markup.row(
|
markup.row(
|
||||||
types.InlineKeyboardButton('👍', callback_data=posts_cb.new(id=post_id, action='like')),
|
types.InlineKeyboardButton('👍', callback_data=posts_cb.new(id=post_id, action='like')),
|
||||||
types.InlineKeyboardButton('👎', callback_data=posts_cb.new(id=post_id, action='unlike')),
|
types.InlineKeyboardButton('👎', callback_data=posts_cb.new(id=post_id, action='dislike')),
|
||||||
)
|
)
|
||||||
markup.add(types.InlineKeyboardButton('<< Back', callback_data=posts_cb.new(id='-', action='list')))
|
markup.add(types.InlineKeyboardButton('<< Back', callback_data=posts_cb.new(id='-', action='list')))
|
||||||
return text, markup
|
return text, markup
|
||||||
|
|
@ -84,7 +86,7 @@ async def query_view(query: types.CallbackQuery, callback_data: dict):
|
||||||
await query.message.edit_text(text, reply_markup=markup)
|
await query.message.edit_text(text, reply_markup=markup)
|
||||||
|
|
||||||
|
|
||||||
@dp.callback_query_handler(posts_cb.filter(action=['like', 'unlike']))
|
@dp.callback_query_handler(posts_cb.filter(action=['like', 'dislike']))
|
||||||
async def query_post_vote(query: types.CallbackQuery, callback_data: dict):
|
async def query_post_vote(query: types.CallbackQuery, callback_data: dict):
|
||||||
try:
|
try:
|
||||||
await dp.throttle('vote', rate=1)
|
await dp.throttle('vote', rate=1)
|
||||||
|
|
@ -100,10 +102,10 @@ async def query_post_vote(query: types.CallbackQuery, callback_data: dict):
|
||||||
|
|
||||||
if action == 'like':
|
if action == 'like':
|
||||||
post['votes'] += 1
|
post['votes'] += 1
|
||||||
elif action == 'unlike':
|
elif action == 'dislike':
|
||||||
post['votes'] -= 1
|
post['votes'] -= 1
|
||||||
|
|
||||||
await query.answer('Voted.')
|
await query.answer('Vote accepted')
|
||||||
text, markup = format_post(post_id, post)
|
text, markup = format_post(post_id, post)
|
||||||
await query.message.edit_text(text, reply_markup=markup)
|
await query.message.edit_text(text, reply_markup=markup)
|
||||||
|
|
||||||
|
|
@ -114,4 +116,4 @@ async def message_not_modified_handler(update, error):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
executor.start_polling(dp, skip_updates=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue