Python aiogram 2.25.2 ошибка TypeError: Object of type InlineKeyboardButton is not JSON serializable
помогите исправить ошибку TypeError: Object of type InlineKeyboardButton is not JSON serializable
main.py:
from config import TOKEN
from logging import basicConfig, INFO
from buttons import *
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
basicConfig(level=INFO)
@dp.message_handler(commands='start')
async def start(message: types.Message):
await message.answer('Добро пожаловать в бота!', reply_markup=button_1)
@dp.message_handler(text='Кнопка 1')
async def button1(message: types.Message):
await message.answer('Ответ на кнопку 1')
@dp.message_handler(text='Кнопка 2')
async def button1(message: types.Message):
await message.answer('Ответ на кнопку 2')
@dp.message_handler(text='Кнопка 3')
async def button1(message: types.Message):
await message.answer('Ответ на кнопку 3')
if __name__ == '__main__':
executor.start_polling(dp)
buttons.py:
from aiogram import types
button_1l = [
types.InlineKeyboardButton('Кнопка 1'),
types.InlineKeyboardButton('Кнопка 2'),
types.InlineKeyboardButton('Кнопка 3')
]
button_1 = types.InlineKeyboardMarkup(resize_keyboard=True).add(button_1l)
После запуска бота и ввода /start возникает ошибка TypeError: Object of type InlineKeyboardButton is not JSON serializable
Смотрел в интернете решение, но ничего особо полезного не нашел. Пытался перейти на aiogam 3.2 но там возникли другие ошибки
Python: 3.10.2 Aiogam: 2.25.2
Ответы (1 шт):
Автор решения: misha plotnikov
→ Ссылка
main.py:
from aiogram import *
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from config import TOKEN
from logging import basicConfig, INFO
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
basicConfig(level=INFO)
keyboard = InlineKeyboardMarkup()
b1 = InlineKeyboardButton("Кнопка 1", callback_data="fun1")
b2 = InlineKeyboardButton("Кнопка 3", callback_data="fun2")
b3 = InlineKeyboardButton("Кнопка 4", callback_data="fun3")
keyboard.add(b1, b2, b3) #так вы их разместите в ряд
@dp.message_handler(commands='start')
async def start(message: types.Message):
await message.answer('Добро пожаловать в бота!', reply_markup=keyboard)
@dp.callback_query_handler(lambda call: call.data.startswith('fun1'))
async def fun1(call: types.CallbackQuery):
await bot.send_message(call.message.chat.id, "Ответ на кнопку 1'")
@dp.callback_query_handler(lambda call: call.data.startswith('fun2'))
async def fun2(call: types.CallbackQuery):
await bot.send_message(call.message.chat.id, "Ответ на кнопку 2'")
@dp.callback_query_handler(lambda call: call.data.startswith('fun3'))
async def fun3(call: types.CallbackQuery):
await bot.send_message(call.message.chat.id, "Ответ на кнопку 3'")
if __name__ == '__main__':
executor.start_polling(dp)
Файл buttons.py вам не нужен Если будут вопросы, или где-то допущена моя ошибка(вроде нету), я с удовольствием отвечу в коментариях