@bot.callback_query_handler(func=lambda call: True)
def callback(call):
if call.data == 'companies_list':
conn = sqlite3.connect('ETP_bot.sql')
cur = conn.cursor()
cur.execute("SELECT * FROM users")
users = cur.fetchall()
info = ''
for el in users:
info += f'<b>Компания</b>: "{el[1]}"\n <u>Культура</u>: <em>{el[2]}</em>\n'
cur.close()
conn.close()
bot.send_message(call.message.chat.id, info)
if call.data == 'buy':
bot.send_message(call.message.chat.id, f'Ознакомьтесь со списком компаний', reply_markup=companies_list)
if call.data == 'sell':
def test(message):
conn2 = sqlite3.connect('ETP_bot_table.sql')
cur2 = conn2.cursor()
cur2.execute('CREATE TABLE IF NOT EXISTS table_1 (id int auto_increment primary key, product varchar(50), price varchar(50))')
conn2.commit()
cur2.close()
conn2.close()
....
所以,首先我为callback_data添加了装饰器,并在其中检查即将到来的数据。如果按下负责数据“出售”的按钮,我将像第一次一样创建新的数据库(下面的代码)。但这并没有发生,python 以灰色 def test(message) 突出显示并拒绝创建数据库。
@bot.message_handler(commands=['start'])
def main(message):
bot.send_message(message.chat.id, f'Добро пожаловать, дорогой пользователь! Введите название вашей организации.')
conn = sqlite3.connect('ETP_bot.sql')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS users (id int auto_increment primary key, name varchar(50), kultura varchar(50))')
conn.commit()
cur.close()
conn.close()
bot.register_next_step_handler(message, user_name)
def user_name(message):
global name
name = message.text.strip()
bot.send_message(message.chat.id, f'Теперь введите культуру, которой занимается ваша компания.')
bot.register_next_step_handler(message, user_kultura)
def user_kultura(message):
kultura = message.text.strip()
conn = sqlite3.connect('ETP_bot.sql')
cur = conn.cursor()
cur.execute("INSERT INTO users (name, kultura) VALUES ('%s', '%s')" % (name, kultura))
conn.commit()
cur.close()
conn.close()
bot.send_message(message.chat.id, f'Компания успешно зарегистрирована!', reply_markup=companies_list)
基本上我尝试删除 if cal.data == "sell" 中的每个 def,但它不起作用。
是这么说的
product = call.text.strip()
^^^^^^^^^
AttributeError: 'CallbackQuery' object has no attribute 'text'
您似乎正在尝试访问
text
对象的 CallbackQuery
属性,但该对象不存在。回调查询没有 text
属性;相反,他们有 data
。
在
callback
函数中,您正在处理回调查询,因此您应该访问 data
对象的 call
属性来确定按下了哪个按钮。
像这样:
@bot.callback_query_handler(func=lambda call: True)
def callback(call):
if call.data == 'companies_list':
conn = sqlite3.connect('ETP_bot.sql')
cur = conn.cursor()
cur.execute("SELECT * FROM users")
users = cur.fetchall()
info = ''
for el in users:
info += f'<b>Компания</b>: "{el[1]}"\n <u>Культура</u>: <em>{el[2]}</em>\n'
cur.close()
conn.close()
bot.send_message(call.message.chat.id, info)
if call.data == 'buy':
bot.send_message(call.message.chat.id, f'Ознакомьтесь со списком компаний', reply_markup=companies_list)
if call.data == 'sell':
# Define the function for selling
def sell(message):
product = message.text.strip()
conn2 = sqlite3.connect('ETP_bot_table.sql')
cur2 = conn2.cursor()
cur2.execute('CREATE TABLE IF NOT EXISTS table_1 (id INTEGER PRIMARY KEY AUTOINCREMENT, product VARCHAR(50), price VARCHAR(50))')
conn2.commit()
cur2.close()
conn2.close()
# Prompt the user to enter the product they want to sell
bot.send_message(call.message.chat.id, "Enter the product you want to sell:")
# Register the next step handler to receive the product name
bot.register_next_step_handler(call.message, sell)