Вопрос касается телеграмм бота на библиотеке telebot
Ссылка на базу данных
не судите строго, я новичок и плохо разбираюсь даже в своём коде, но учусь. Проблема заключается в функциях корзины. Всё завязано с базами данных SQLite. Устроена корзина так: 1)После нажатия на кнопку появляется список товаров которые уже в корзине. 2)После вывода текста с корзиной пользователь исходя из инструкции должен ввести свои данные и отправить в бот. 3) После этого заказ с текстом пользователя отправится мне в сообщения телеграмм, сохранится в таблицу orders и очиститься из корзины, всё это работает с id пользователя.
Проблема в том, что я не могу реализовать выход из корзины, когда в неё уже перешёл, когда пытаюсь выйти в "Каталог" (код я привёл), корзина принимает текст кнопки и обрабатывает заказ. Также я добавил кнопку отчистки корзины, но она тоже не работает, интерпритатор выдаёт различные ошибки, как по мне не связанные с проблемой совсем. Код с функцией я удалил, устроено это было через def "имя" и внутри функции выполнялось clear_cart(), эта функция есть в конце кода.
Буду очень признателен за любые подсказки или помощь!
import telebot
import sqlite3
from telebot.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto
from telebot import types
API_TOKEN = ''
bot = telebot.TeleBot(API_TOKEN)
bottom_menu = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
catalog_button = KeyboardButton("Каталог свечей ?️")
basket_button = KeyboardButton("Корзина ?️")
bottom_menu.row(catalog_button, basket_button)
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS cart (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT, price REAL, quantity INTEGER)')
@bot.message_handler(commands=['help','start'])
def send_welcome(message):
bot.reply_to(message, message.from_user.first_name + "\nДобро пожаловать в телеграмм-магазин свечей ARTEG ?️\nПриятных покупок ?")
bot.send_video(message.chat.id, 'https://i.imgur.com/EZuE61u.gif', None, 'Text')
bot.send_message(message.chat.id, 'Пожалуйста используйте клавиатуру для навигации по магазину.', reply_markup=bottom_menu)
@bot.message_handler(func=lambda message: message.text == 'Каталог свечей ?️')
def catalog_candles(message):
markup = InlineKeyboardMarkup()
with sqlite3.connect('candles.db') as conn:
cur = conn.cursor()
# get all candles from the database
cur.execute('SELECT id, name FROM candles')
rows = cur.fetchall()
for row in rows:
button_text = row[1]
button_callback = str(row[0])
button = InlineKeyboardButton(button_text, callback_data=button_callback)
markup.add(button)
bot.send_message(message.chat.id, 'Каталог свечей ?️:', reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def handle_callback_query(call):
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
if call.data.startswith('add_to_cart_'):
id = int(call.data.split('_')[-1])
cur.execute('SELECT name, price FROM candles WHERE id = ?', (id,))
row = cur.fetchone()
name = row[0]
price = row[1]
cur.execute('SELECT quantity FROM cart WHERE name = ?', (name,))
row = cur.fetchone()
if row is None:
user_id = call.message.chat.id
cur.execute('INSERT INTO cart (user_id, name, price, quantity) VALUES (?, ?, ?, 1)', (user_id, name, price))
else:
quantity = row[0] + 1
cur.execute('UPDATE cart SET quantity = ? WHERE name = ?', (quantity, name))
conn.commit()
cur.close()
conn.close()
bot.answer_callback_query(call.id, text=f"{name} добавлен в корзину ?")
else:
id = int(call.data)
cur.execute('SELECT name, description, price, image_url FROM candles WHERE id = ?', (id,))
row = cur.fetchone()
name = row[0]
description = row[1]
price = row[2]
image_url = row[3]
message_text = f"{name}\n\n{description}\n\nPrice: ₽{price:.2f}"
markup = InlineKeyboardMarkup()
add_to_cart_button = InlineKeyboardButton("Добавить в корзину ?", callback_data=f"add_to_cart_{id}")
markup.add(add_to_cart_button)
bot.send_photo(call.message.chat.id, photo=image_url, caption=message_text, reply_markup=markup)
cur.close()
conn.close()
bot.answer_callback_query(call.id)
@bot.callback_query_handler(func=lambda call: call.data.startswith('add_to_cart_'))
def handle_add_to_cart(call):
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
id = int(call.data.split('_')[-1])
user_id = call.message.chat.id
cur.execute('SELECT name, price FROM candles WHERE id = ?', (id,))
row = cur.fetchone()
name = row[0]
price = row[1]
cur.execute('SELECT quantity FROM cart WHERE user_id = ? AND name = ? AND id = ?', (call.message.chat.id, name, id))
row = cur.fetchone()
if row is None:
cur.execute('INSERT INTO cart (user_id, name, price, quantity) VALUES (?, ?, ?, 1)', (user_id, name, price))
else:
quantity = row[0] + 1
cur.execute('UPDATE cart SET quantity = ? WHERE user_id = ? AND name = ? AND id = ?', (quantity, user_id, name, id))
conn.commit()
cur.close()
conn.close()
bot.answer_callback_query(call.id, text=f"{name} added to cart")
@bot.message_handler(func=lambda message: message.text == 'Корзина ?️')
def handle_basket_button(message):
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
global user_id
user_id = message.chat.id
cur.execute('SELECT name, price, quantity FROM cart WHERE user_id = ?', (user_id,))
rows = cur.fetchall()
if not rows:
bot.send_message(message.chat.id, "Корзина пуста")
return
button = types.InlineKeyboardButton(text="Очистить корзину", callback_data=1)
# создаем клавиатуру с этой кнопкой
keyboard = types.InlineKeyboardMarkup()
keyboard.add(button)
message_text = 'Ваша корзина:\n\n'
total_price = 0
for row in rows:
global name1
global quantity1
name1 = row[0]
quantity1 = row[2]
name = row[0]
price = row[1]
quantity = row[2]
total_price += price * quantity
message_text += f"{name} - ₽{price:.2f} x {quantity}\n"
message_text += f"\nИтого: ₽{total_price:.2f}\n➖➖➖➖➖➖➖➖➖➖➖➖\nВведите свои данные по форме для подтверждения заказа:\n➖➖➖➖➖➖➖➖➖➖➖➖\nПример: \nВаше имя\nЦвет свечи(если есть выбор)\nЗапах свечи" \
f"\nСпособ доставки(самовывоз/курьером)\nАдрес(город,улица,номер дома,этаж,квартира)\n➖➖➖➖➖➖➖➖➖➖➖➖\nПроизведите оплату с указанием в комментарии своего имени.\n\nТелефон для перевода (Сбер) +79198836943\n"
bot.send_message(message.chat.id, message_text, reply_markup=keyboard)
bot.register_next_step_handler(message, awaiting_confirmation, total_price)
cur.close()
conn.close()
def awaiting_confirmation(message, total_price):
# Save the order and user input to the database
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
global user_id
user_id = message.chat.id
cur.execute('SELECT name, price, quantity FROM cart WHERE user_id = ?', (user_id,))
rows = cur.fetchall()
if not rows:
bot.send_message(message.chat.id, "Корзина пуста")
return
message_text = '\n\n'
total_price = 0
for row in rows:
global name1
global quantity1
name1 = row[0]
quantity1 = row[2]
name = row[0]
price = row[1]
quantity = row[2]
total_price += price * quantity
message_text += f"{name} - ₽{price:.2f} x {quantity}\n"
message_text += f""
order = message.text
cur.execute('INSERT INTO orders (`order`, total_price) VALUES (?, ?)', (order, total_price))
conn.commit()
cur.close()
conn.close()
# Send a message with the order details and user input to you
message_text = f"Новый заказ {message_text}Итого: ₽{total_price:.2f}\n{order}"
bot.send_message(357930008, message_text)
# Clear the cart
clear_cart()
# Send a confirmation message to the user
bot.send_message(message.chat.id, f"Ваш заказ на сумму ₽{total_price:.2f} подтвержден ✅")
# Add back the main menu buttons
global bottom_menu
bottom_menu = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
catalog_button = KeyboardButton("Каталог свечей ?️")
basket_button = KeyboardButton("Корзина ?️")
bottom_menu.row(catalog_button, basket_button)
bot.send_message(message.chat.id, "Возвращаемся в главное меню", reply_markup=bottom_menu)
def clear_cart():
conn = sqlite3.connect('candles.db')
cur = conn.cursor()
cur.execute('DELETE FROM cart WHERE user_id = ?', (user_id,))
conn.commit()
cur.close()
conn.close()
bot.infinity_polling()