Работает только стартовая команда бот телеграм
Прикрепляю код с самого начала, но код перестает работать со строчки 24. Команда /start работает, а на другие команды бот не реагирует. Подскажите, пожалуйста, что не так.
import time
import asyncio
import logging
import requests
import aiogram
from aiogram import Bot, Dispatcher, types
from aiogram.filters.command import Command
#logging.basicConfig(level=logging.info)
bot = Bot(token="")
dp = Dispatcher()
@dp.message(Command("start"))
async def start_handler(message: types.Message):
user_id = message.from_user.id
user_full_name = message.from_user.full_name
logging.info(f'{user_id=} {user_full_name=} {time.asctime()}')
await message.reply(f"Привет! Рад тебя видеть, {user_full_name}")
@dp.message(Command("hello"))
async def info(message: types.Message):
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton('Получить прогноз погоды', callback_data='city'))
markup.add(types.InlineKeyboardButton('Список команд', callback_data='list'))
await message.reply(f"Тут ты можешь узнать прогноз погоды", reply_markup=markup)
@dp.message(Command("listCity"))
async def reply(message: types.Message):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add(types.InlineKeyboardButton('London'))
markup.add(types.InlineKeyboardButton('Moscow'))
markup.add(types.InlineKeyboardButton('Saint Petersburg'))
await message.reply(reply_markup=markup)
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())
Терминал выдает следующее: /usr/local/bin/python3 /Users/sofia/Desktop/dl_testovoe_bot/testovoe.py ERROR:aiogram.event:Cause exception while process update id=284569412 by bot id=7159157542 ValidationError: 1 validation error for InlineKeyboardMarkup inline_keyboard Field required [type=missing, input_value={}, input_type=dict] For further information visit https://errors.pydantic.dev/2.5/v/missing Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 309, in _process_update response = await self.feed_update(bot, update, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 158, in feed_update response = await self.update.wrap_outer_middleware( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/error.py", line 25, in call return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/user_context.py", line 27, in call return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/fsm/middleware.py", line 41, in call return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger return await wrapped_inner(event, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call return await wrapped() ^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 276, in _listen_update return await self.propagate_event(update_type=update_type, event=event, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 128, in propagate_event return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 123, in _wrapped return await self._propagate_event( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 148, in _propagate_event response = await observer.trigger(event, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger return await wrapped_inner(event, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call return await wrapped() ^^^^^^^^^^^^^^^ File "/Users/sofia/Desktop/dl_testovoe_bot/testovoe.py", line 24, in info markup = types.InlineKeyboardMarkup() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pydantic/main.py", line 164, in init pydantic_self.pydantic_validator.validate_python(data, self_instance=pydantic_self) pydantic_core._pydantic_core.ValidationError: 1 validation error for InlineKeyboardMarkup inline_keyboard Field required [type=missing, input_value={}, input_type=dict] For further information visit https://errors.pydantic.dev/2.5/v/missing
Ответы (1 шт):
Уже видел сегодня от Вас вопрос, повторюсь: пожалуйста, ознакомитесь с документацией библиотеки aiogram, вопросов станет в разы меньше. Прочитав документацию, Вы бы знали, что InlineKeyboardButton обязательно должен содержать callback_data,
markup.add(types.InlineKeyboardButton('London'))
markup.add(types.InlineKeyboardButton('Moscow'))
markup.add(types.InlineKeyboardButton('Saint Petersburg'))
Вероятно, тут Вы хотели использовать KeyboardButton, а не InlineKeyboardButton