K
K
Kabrosy2021-11-03 23:19:52
Python
Kabrosy, 2021-11-03 23:19:52

TypeError: Object of type KeyboardButton is not JSON serializable (Aiogram), how to solve?

I have been racking my brain for a day now over why an error pops up with the seemingly simplest telegram game bot code. Stuck at the very beginning, when creating the menu. I am writing it on aiogram, but every time I try to launch my menu, an error pops up.

Code (main.py):

import asyncio
from aiogram import Bot, Dispatcher, executor
from config import BOT_TOKEN

loop = asyncio.get_event_loop()
bot = Bot(BOT_TOKEN, parse_mode ="HTML")
dp = Dispatcher(bot, loop=loop)

if __name__ == "__main__":
    from handlers import dp
    executor.start_polling(dp)


Code (handlers.py):

from main import bot, dp
import keyboards as nav
import time

from aiogram.types import Message


@dp.message_handler(commands=['start'])
async def start_message(message: Message):
    text = f'''Привет, приветствуем тебя в #ScoreIt Game Bot '''
    await bot.send_message(message.from_user.id,
                           'Привет, {0.first_name}, приветствуем тебя в #ScoreIt Game Bot '.format(message.from_user),
                           reply_markup=nav.mainMenu)
    time.sleep(3)
    await bot.send_message(message.from_user.id, 'Этот бот погрузит тебя в атмосферу футбольного матча и поможет '
                                                 'представить себя в шкуре футбольного тренера ⚽'.format(
        message.from_user))
    time.sleep(3)


Code (keyboards.py):
from aiogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove, KeyboardButton

give_money_btn = KeyboardButton(text='Пополнить счёт  (для платной игры)'),
play_match_btn = KeyboardButton(text='Играть матч ⚽ (Одиночная игра)'),
play_tournament_btn = KeyboardButton(text='Играть турнир  (Одиночная игра)'),
play_with_friend_btn = KeyboardButton(text='Играть с другом ')

mainMenu = ReplyKeyboardMarkup(resize_keyboard=True).add(give_money_btn, play_match_btn, play_tournament_btn, play_with_friend_btn)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question