Solution:
In my case, the solution was to revoke the bot token
Problem description:
When developing a bot in Python using the framework aiogram 3.0.0b8, was found a problem with the processing of callback_data, when the button inline is pressed nothing happens, but when you enter the command /heroes in the telegram displays only the initial text. callback_data is designed to change the text (next page, previous page)
bot.py (default code):
import logging
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.memory import MemoryStorage
from config_reader import config
from handlers import common, heroes
from utils.commands import set_commands
logger = logging.getLogger(__name__)
def setup_logger(level: logging) -> None:
format = "%(asctime)s - %(levelname)s - %(name)s - %(message)s"
logging.basicConfig(level=level, format=format)
def register_handlers(dp: Dispatcher) -> None:
dp.include_router(common.router)
dp.include_router(heroes.router)
async def main() -> None:
storage = MemoryStorage()
bot = Bot(token=config.bot_token.get_secret_value(), parse_mode="HTML")
dp = Dispatcher(storage=storage)
await set_commands(bot=bot)
register_handlers(dp=dp)
try:
await bot.delete_webhook(True)
await dp.start_polling(bot)
finally:
await dp.storage.close()
await bot.session.close()
if __name__ == "__main__":
try:
setup_logger(logging.INFO)
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logging.info("bot stopped by ctrl+c")
handlers/heroes.py:
import logging
from aiogram import Router
from aiogram.filters import Command, Text
from aiogram.types import Message, CallbackQuery
from aiogram.utils.keyboard import InlineKeyboardBuilder
currentNOH = 0 # current numbers of hero
currentPage = None
router = Router()
@router.message(Command("heroes"))
async def cmd_heroes(message: Message) -> None:
global currentNOH
global currentPage
# creating 10 inline buttons
builder = InlineKeyboardBuilder()
for i in range(10):
builder.button(text=str(i + 1), callback_data=f"hero_{i + 1}")
currentNOH += 1
builder.button(text="<", callback_data="back")
builder.button(text=">", callback_data="next")
builder.adjust(5, 5, 2)
currentPage = await message.answer("Choose a hero:", reply_markup=builder.as_markup())
logging.info("/HEROES <>MESSAGE<>")
@router.callback_query(Text(["back", "next"]))
async def change_page(query: CallbackQuery) -> None:
logging.info("CALLBACK") # not calling when inline button pressed
global currentPage
global currentNOH
builder = InlineKeyboardBuilder()
if query.data == "back":
if currentNOH > 10:
for _ in range(10):
builder.button(text=str(currentNOH - 1), callback_data=f"hero_{currentNOH - 1}")
currentNOH -= 1
elif query.data == "next":
if currentNOH + 10 < 124:
for _ in range(10):
builder.button(text=str(currentNOH + 1), callback_data=f"hero_{currentNOH + 1}")
currentNOH += 1
builder.button(text="<", callback_data="back")
builder.button(text=">", callback_data="next")
if currentNOH <= 5:
builder.adjust(currentNOH, 2)
elif currentNOH <= 10:
builder.adjust(5, currentNOH - 5, 2)
currentPage = await query.message.edit_text("Choose a hero:", reply_markup=builder.as_markup())
Terminal:
$ python bot.py
2023-06-30 18:10:59,804 - INFO - aiogram.dispatcher - Start polling
2023-06-30 18:10:59,894 - INFO - aiogram.dispatcher - Run polling for bot @telegrambot id=1234567890- 'telegrambot'
2023-06-30 18:11:00,817 - INFO - root - /HEROES <>MESSAGE<>
2023-06-30 18:11:00,817 - INFO - aiogram.event - Update id=123456789 is handled. Duration 296 ms by bot id=1234567890
# <-- Pressed the inline button, but the callback is inactive