user_choise = {
}
click = {
}
connect1 = sqlite3.connect("tovar.db", check_same_thread=False)
cursor1 = connect1.cursor()
connect2 = sqlite3.connect("users.db",check_same_thread=False)
cursor2 = connect2.cursor()
@bot.message_handler(commands=["start"])
def about(call):
click[call.from_user.id]=0
keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True,one_time_keyboard=True)
about_you = telebot.types.KeyboardButton(text = "about you")
start_shopping = telebot.types.KeyboardButton(text = "start shopping")
keyboard.add(about_you,start_shopping)
about_sms = bot.send_message(call.from_user.id, text = 'you are register in hikan shop',reply_markup=keyboard)
bot.register_next_step_handler(about_sms,start_shop)
@bot.message_handler(content_types=['text'])
def start_shop(call):
list_ids = []
cursor2.execute("SELECT id FROM login_id")
rows = cursor2.fetchall()
for row in rows:
for id in row:
list_ids.append(id)
if call.text == "about you" and call.from_user.id in list_ids:
cursor2.execute(f"SELECT * FROM login_id WHERE id = {call.from_user.id}")
info = cursor2.fetchone()
bot.send_message(call.from_user.id,"you are register in 'hikan shop' and there are some information about you.\n"
"Please,check thats a valid data, because information about you can be use to make delivery.\n\n"
f"id: {info[0]}\n"
f"name: {info[1]}\n"
f"phone number: {info[2]}\n"
f"your role: {info[3]}\n"
f"you bought recently: {None}"
)
connect1.commit()
if call.text == "start shopping":
cursor1.execute("SELECT category FROM tovars")
categories = cursor1.fetchall()
markup = types.ReplyKeyboardMarkup(resize_keyboard=True,one_time_keyboard=True)
list_of_categories = []
for categ_in_list in categories:
for categ_in_tuple in categ_in_list:
list_of_categories.append(categ_in_tuple)
for category in list(set(list_of_categories)):
category = types.KeyboardButton(text = category)
markup.add(category)
wait_for_choise = bot.send_message(call.from_user.id, text = "select category",reply_markup=markup)
bot.register_next_step_handler(wait_for_choise,choise)
@bot.message_handler(content_types=['text'])
def choise(call):
chosen_categ = call.text
cursor1.execute("SELECT * FROM tovars WHERE category = '{}'".format(chosen_categ,))
about_cloth = cursor1.fetchall()
buttons = telebot.types.InlineKeyboardMarkup()
forw = telebot.types.InlineKeyboardButton(text = "->", callback_data="->")
backw = telebot.types.InlineKeyboardButton(text = "<-", callback_data="<-")
bought = telebot.types.InlineKeyboardButton(text = f"{about_cloth[0][0]} {about_cloth[0][1]}", callback_data="choisen_first")
buttons.add(backw,forw)
buttons.add(bought)
next = bot.send_message(call.from_user.id,
f"category: {about_cloth[0][0]}\n"
f"name: {about_cloth[0][1]}\n"
f"price: {about_cloth[0][2]}\n"
f"count: {about_cloth[0][3]}\n"
f"size: {about_cloth[0][4]}", reply_markup=buttons)#add photo
user_choise[call.from_user.id] = about_cloth
bot.register_next_step_handler(next,sending_cloth_from_database)
@bot.callback_query_handler(func=lambda call: True)
def sending_cloth_from_database (call):
about_cloth = user_choise[call.from_user.id]
try:
if call.data == "->" or call.data == "->1":
if call.from_user.id not in list(click.keys()):
click[call.from_user.id] = 1
else:
click[call.from_user.id]+=1
try:
cloth= user_choise[call.from_user.id][click[call.from_user.id]]
buttons1 = telebot.types.InlineKeyboardMarkup()
forw1 = telebot.types.InlineKeyboardButton(text = "->", callback_data="->1")
backw1 = telebot.types.InlineKeyboardButton(text = "<-", callback_data="1<-")
bought1 = telebot.types.InlineKeyboardButton(text = f"{cloth[0]} {cloth[1]}", callback_data="choisen_second")
buttons1.add(backw1,forw1)
buttons1.add(bought1)
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text =
f"ctagory: {cloth[0]}\n"
f"name: {cloth[1]}\n"
f"price: {cloth[2]}\n"
f"count: {cloth[3]}\n"
f"size: {cloth[4]}\n", reply_markup=buttons1)#edit photo
except TypeError:
pass
except IndexError:
click.pop(call.from_user.id)
click[call.from_user.id] = 0
buttons = telebot.types.InlineKeyboardMarkup()
forw = telebot.types.InlineKeyboardButton(text = "->", callback_data="->")
backw = telebot.types.InlineKeyboardButton(text = "<-", callback_data="<-")
bought = telebot.types.InlineKeyboardButton(text = f"{about_cloth[0][0]} {about_cloth[0][1]}", callback_data="choisen_first")
buttons.add(backw,forw)
buttons.add(bought)
bot.edit_message_text(chat_id = call.message.chat.id,message_id = call.message.message_id,text =
f"category: {about_cloth[0][0]}\n"
f"name: {about_cloth[0][1]}\n"
f"price: {about_cloth[0][2]}\n"
f"count: {about_cloth[0][3]}\n"
f"size: {about_cloth[0][4]}", reply_markup=buttons)#add photo
if call.data == "<-" or call.data == "1<-":
if call.from_user.id not in list(click.keys()):
click[call.from_user.id] = 1
else:
click[call.from_user.id]-=1
try:
cloth= user_choise[call.from_user.id][click[call.from_user.id]]
buttons1 = telebot.types.InlineKeyboardMarkup()
forw1 = telebot.types.InlineKeyboardButton(text = "->", callback_data="->1")
backw1 = telebot.types.InlineKeyboardButton(text = "<-", callback_data="1<-")
bought1 = telebot.types.InlineKeyboardButton(text = f"{cloth[0]} {cloth[1]}", callback_data="choisen_second")
buttons1.add(backw1,forw1)
buttons1.add(bought1)
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text =
f"ctagory: {cloth[0]}\n"
f"name: {cloth[1]}\n"
f"price: {cloth[2]}\n"
f"count: {cloth[3]}\n"
f"size: {cloth[4]}\n", reply_markup=buttons1)#edit photo
except TypeError:
pass
except IndexError:
click.pop(call.from_user.id)
click[call.from_user.id] = 0
if call.data == "choisen_first" or call.data == "choisen_second":
buttons2 = telebot.types.InlineKeyboardMarkup()
forw2 = telebot.types.InlineKeyboardButton(text = "->", callback_data="->2")
backw2 = telebot.types.InlineKeyboardButton(text = "<-", callback_data="2<-")
bought2 = telebot.types.InlineKeyboardButton(text = "buy", callback_data="choise")
buttons2.add(backw2,forw2)
buttons2.add(bought2)
cloth= user_choise[call.from_user.id][click[call.from_user.id]]
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text =
f"ctagory: {cloth[0]}\n"
f"name: {cloth[1]}\n"
f"price: {cloth[2]}\n"
f"count: {cloth[3]}\n"
f"size: {cloth[4]}\n", reply_markup=buttons2)#edit photo
if call.data == "choise":
cloth= user_choise[call.from_user.id][click[call.from_user.id]]
prices = [LabeledPrice(label=f'{cloth[0]} {cloth[1]}', amount=int(str(cloth[2])+"00"))]
bot.send_invoice(
call.from_user.id, #chat_id
f'{cloth[0]}', #title
f'name: {cloth[1]}\nsize: {cloth[4]}', #description
'chip', #invoice_payload
provider_token, #provider_token
'rub', #currency
prices, #prices
photo_url='https://бордшоп1.рф/pictures/product/big/38119_big.jpg',
photo_height=512, # !=0/None or picture won't be shown
photo_width=512,
photo_size=512,
is_flexible=False, # True If you need to set up Shipping Fee
start_parameter='time-machine-example')
except AttributeError:
pass
bot.polling(none_stop=True)`введите сюда код`