Answer the question
In order to leave comments, you need to log in
How to fix "(Telebot) AttributeError: 'Message' object has no attribute 'data'" error?
I want to create a bot like a game.
My bot has a menu, a robot, a shop, etc...
And the bot has a type of robot. It is necessary to solve random tasks that the bot sets (example: 130 + 459 how much will it be?) and when solving, money is added to the balance, and so I solve one problem, then I press repeat, the bot displays the task, I write the answer. And an error appears:
Traceback (most recent call last):
File "C:/Users/HOME/PycharmProjects/simProject/main.py", line 201, in <module>
bot.polling(none_stop=True, interval=0)
File "C:\Users\HOME\PycharmProjects\simProject\venv\lib\site-packages\telebot\__init__.py", line 637, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "C:\Users\HOME\PycharmProjects\simProject\venv\lib\site-packages\telebot\__init__.py", line 699, in __threaded_polling
raise e
File "C:\Users\HOME\PycharmProjects\simProject\venv\lib\site-packages\telebot\__init__.py", line 659, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\HOME\PycharmProjects\simProject\venv\lib\site-packages\telebot\util.py", line 130, in raise_exceptions
raise self.exception_info
File "C:\Users\HOME\PycharmProjects\simProject\venv\lib\site-packages\telebot\util.py", line 82, in run
task(*args, **kwargs)
File "C:/Users/HOME/PycharmProjects/simProject/main.py", line 79, in shop
if call.data == 'car_salon':
AttributeError: 'Message' object has no attribute 'data'
Process finished with exit code 1
import telebot
import sqlite3
from telebot import types
from random import randint
import codecs
global username
global user_login
global balance
token1 = '2008418418:AAGROso2yiEwKWOvyGbVPS6PidazfoFzKPc'
curency = 'грн'
bot = telebot.TeleBot(token1)
levl = 1
with sqlite3.connect('database_sim.db', check_same_thread=False) as db:
sql = db.cursor()
user_login_your = 'vova_6_6_2'
user_login = user_login_your
for i1 in sql.execute(f'SELECT money FROM users WHERE login = "{user_login}"'):
balance = i1[0]
@bot.message_handler(commands=['start'])
def start(message):
if message.chat.type == 'private':
if message.text == '/start':
user_id = message.from_user.id
#sql.execute(f"UPDATE users SET id = {user_id} WHERE login = '{user_login}'")
#db.commit()
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup1 = types.KeyboardButton('1.Робота')
markup2 = types.KeyboardButton('2.Магазин')
markup3 = types.KeyboardButton('3.Банк')
markup4 = types.KeyboardButton('4.Мій дім')
markup.add(markup1, markup2, markup3, markup4)
bot.send_message(message.chat.id, 'Привіт вітаємо у нашій грі Simulator life! \nВиберіть щось із меню', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def menu(message):
if message.text == '1.Робота':
inline_makup = types.InlineKeyboardMarkup()
in_markup1 = types.InlineKeyboardButton('Клікер', callback_data='kliker1')
in_markup2 = types.InlineKeyboardButton('Майнинг♾', callback_data='maining2')
in_markup3 = types.InlineKeyboardButton('Трейдинг', callback_data='treding3')
in_markup4 = types.InlineKeyboardButton('Банкір', callback_data='bankir_menu')
in_markup5 = types.InlineKeyboardButton('Казіно', callback_data='kazino5')
inline_makup.add(in_markup1, in_markup2, in_markup3, in_markup4, in_markup5)
bot.send_message(message.chat.id, 'Виберіть роботу', reply_markup=inline_makup)
elif message.text == '2.Магазин':
inline = types.InlineKeyboardMarkup()
inline1 = types.InlineKeyboardButton('1.Техніка', callback_data='tech')
inline2 = types.InlineKeyboardButton('2.Автосалон', callback_data='car_salon')
inline3 = types.InlineKeyboardButton('3.Дома', callback_data='house')
inline4 = types.InlineKeyboardButton('4.????', callback_data='???')
inline.add(inline1, inline2, inline3, inline4)
for i3 in sql.execute(f"SELECT money FROM users WHERE login = '{user_login}'"):
balance_shop = i3[0]
bot.send_message(message.chat.id, f"_____Shop_____ \nУ вас {balance_shop} грн", reply_markup=inline)
@bot.callback_query_handler(func=lambda call:True)
def shop(call):
if call.data == 'car_salon':
inline = types.InlineKeyboardMarkup()
markup_inl1 = types.InlineKeyboardButton('Toyota', callback_data='toyota121')
markup_inl2 = types.InlineKeyboardButton('Audi', callback_data='audi121')
markup_inl3 = types.InlineKeyboardButton('Bmw', callback_data='bmw121')
markup_inl4 = types.InlineKeyboardButton('Mersedes', callback_data='mersedes121')
inline.add(markup_inl1, markup_inl2, markup_inl3,markup_inl4)
bot.send_message(call.message.chat.id, 'Виберіть марку авто:', reply_markup=inline)
elif call.data == 'bmw121':
bot.send_message(call.message.chat.id, 'Доступні машини:')
for id_user in sql.execute(f"SELECT id FROM users WHERE login = '{user_login}'"):
pass
bot.send_photo(id_user, codecs.open(r"C:\Users\HOME\PycharmProjects\Кеша бот python\Новий бот-Ігра\Фото\Car\bmw_E39.jpg", 'rb'), caption='Bmw e39 - 150.000грн')
bot.send_photo(id_user, codecs.open(r"C:\Users\HOME\PycharmProjects\Кеша бот python\Новий бот-Ігра\Фото\Car\bmw_x3_2011.jpg ", 'rb'), caption='Bmw X3 - 520.000грн')
bot.send_photo(id_user, codecs.open(r"C:\Users\HOME\PycharmProjects\Кеша бот python\Новий бот-Ігра\Фото\Car\bmw_m5f90.jpg", 'rb'), caption='Bmw 5mF90 - 3.000.000грн')
bot.send_message(call.message.chat.id, 'Виберіть авто!' )
elif call.data == 'kliker1':
#for my_levl in sql.execute(f"SELECT levl FROM users WHERE login = '{user_login}'"):
# pass
inline_markup = types.InlineKeyboardMarkup()
in_markup = types.InlineKeyboardButton('Почати роботу', callback_data='kliker1_congif')
inline_markup.add(in_markup)
bot.send_message(call.message.chat.id, f'Клікер, на цій роботі вам потрібно клікати на кнопку і за один клік ви получите 0.50 Гривень. \n Потрібен 1 уровень \nВаш уровень: {levl}',reply_markup=inline_markup)
elif call.data == 'kliker1_congif':
inline_markup = types.InlineKeyboardMarkup()
in_markup = types.InlineKeyboardButton('Тикніть', callback_data='kliker_money_6787')
inline_markup.add(in_markup)
bot.send_message(call.message.chat.id, ' ', reply_markup=inline_markup)
elif call.data == 'kliker_money_6787':
inl = types.InlineKeyboardMarkup()
inl_markup = types.InlineKeyboardButton('Тикніть', callback_data='kliker_money_6787')
inl.add(inl_markup)
bot.send_message(call.message.chat.id, ' ')
for i12 in sql.execute(f'SELECT money FROM users WHERE login = "{user_login}"'):
balance1 = i12[0]
sql.execute(f"UPDATE users SET money = {0.5 + balance1} WHERE login = '{user_login}'")
for i13 in sql.execute(f"SELECT money FROM users WHERE login = '{user_login}'"):
balance2 = i13[0]
db.commit()
bot.send_message(call.message.chat.id, f'Ваш баланс:{balance2}', reply_markup=inl)
for i_money in sql.execute('SELECT money FROM users'):
print(i_money)
elif call.data == 'bankir_menu':
# for my_levl in sql.execute(f"SELECT levl FROM users WHERE login = '{user_login}'"):
# pass
inline = types.InlineKeyboardMarkup()
inl_markup = types.InlineKeyboardButton('Почати роботу..', callback_data='Ще раз...')
inline.add(inl_markup)
bot.send_message(call.message.chat.id,f'Банкір - це робота на якій потрібно рахувати задачі виведені на екран \nОдна задача: 50грн \nУровень роботи: 7 \nВаш уровень: ',
reply_markup=inline)
elif call.data == 'Ще раз...':
global res
num = randint(1, 200)
num1 = randint(1, 150)
res = num + num1
print(res)
msg = bot.send_message(call.message.chat.id, f'Скільки буде {num} + {num1} ?')
bot.register_next_step_handler(msg, bankir_menu_next)
@bot.message_handler(content_types=['text'])
def bankir_menu_next(message):
if int(message.text) == res:
inline = types.InlineKeyboardMarkup()
inline1 = types.InlineKeyboardButton('Ще раз...', callback_data='Ще раз...')
inline2 = types.InlineKeyboardButton('Menu', callback_data='Menu')
inline.add(inline1, inline2)
for i2 in sql.execute(f"SELECT money FROM users WHERE login = '{user_login}'"):
balance2 = i2[0]
sql.execute(f"UPDATE users SET money = {50 + balance2} WHERE login = '{user_login}'")
db.commit()
msg = bot.send_message(message.chat.id, 'Правильно. Ви заробили 50 грн', reply_markup=inline)
bot.register_next_step_handler(msg, shop)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question