-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.07.02.23.py
282 lines (229 loc) · 10.9 KB
/
store.07.02.23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import sqlite3
import requests
from bs4 import BeautifulSoup
import telebot
API_KEY = "5890376662:AAFaY9EYP-Mfg5_fV2bJizn--PS881teiD0"
database = 'C:\\sqlite\\test.db'
bot = telebot.TeleBot(API_KEY)
def main_menu():
markup = telebot.types.InlineKeyboardMarkup(row_width=1)
button1 = telebot.types.InlineKeyboardButton("Service Shop", callback_data="category_menu")
button2 = telebot.types.InlineKeyboardButton("News", callback_data="news_page")
button3 = telebot.types.InlineKeyboardButton("Spainopedia", callback_data="spainopedia_page")
button4 = telebot.types.InlineKeyboardButton("About us", callback_data="help_page")
markup.add(button1, button2, button3, button4)
return markup
def news_page():
markup = telebot.types.InlineKeyboardMarkup()
url = "https://espanarusa.com/ru/news/index"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find("div", class_="er-fresh-container")
news_elements = results.find_all("div", class_="er-fresh")
print(news_elements)
for news_element in news_elements:
print(news_element, end="\n" * 2)
title_elements = news_element.find("div", class_="er-item-title")
links = news_element.find_all('a')
for link in links:
link_url = link["href"]
message_text = f'<a href="https://espanarusa.com{link_url}">{title_elements.text}</a>\n'
markup.add(telebot.types.InlineKeyboardButton(text="← Main menu", callback_data="main_menu"))
return message_text, markup
def spainopedia_page():
markup = telebot.types.InlineKeyboardMarkup()
message_text = 'none'
markup.add(telebot.types.InlineKeyboardButton(text="← Main menu", callback_data="main_menu"))
return message_text, markup
def help_page():
markup = telebot.types.InlineKeyboardMarkup()
message_text = '<b>Documentation:</b> \n<a>1.</a> <a ' \
'href="https://www.example.com">General Manual:</a>\n' \
'<a>2.</a> <a href="https://www.example.com">Your mum</a>\n' \
'\n'
message_text += '<b>Customer support:</b> @lifeisatestbot'
# Add a button to go back to the main menu
markup.add(telebot.types.InlineKeyboardButton(text="← Main menu", callback_data="main_menu"))
return message_text, markup
def category_menu():
conn = sqlite3.connect(database)
c = conn.cursor()
# Fetch all services from the database
c.execute("SELECT * FROM category")
categories = c.fetchall()
# Create an inline keyboard markup
markup = telebot.types.InlineKeyboardMarkup()
# Add buttons for each category
for category in categories:
button = telebot.types.InlineKeyboardButton(category[1], callback_data=f'category_{category[0]}')
markup.add(button)
markup.add(telebot.types.InlineKeyboardButton(text="← Main menu", callback_data="main_menu"))
conn.close()
return markup
def service_menu(category_id):
# get all services of category_id
conn = sqlite3.connect(database)
c = conn.cursor()
# Fetch all services from the database
c.execute("SELECT c.name, s.id, s.name FROM service AS s "
"INNER JOIN category AS c "
"ON s.category_id = c.id "
"WHERE s.category_id = ?", (category_id,))
services = c.fetchall()
category_name = services[0][0]
markup = telebot.types.InlineKeyboardMarkup()
# Add buttons for each service
for service in services:
button = telebot.types.InlineKeyboardButton(service[2], callback_data=f'service_{category_id}_{service[1]}')
markup.add(button)
# Add button to return to the services
services_button = telebot.types.InlineKeyboardButton(text="← Categories", callback_data="category_menu")
markup.add(services_button)
conn.close()
return category_name, markup
def service_detail_menu(service_id, cart_id):
# get all services of category_id
conn = sqlite3.connect(database)
c = conn.cursor()
# Fetch all services from the database
c.execute("SELECT id, code, name, price, description, category_id FROM service WHERE id = ?", (service_id,))
service = c.fetchall()[0]
markup = telebot.types.InlineKeyboardMarkup()
message_text = f"Service Name: {service[2]}\n"
message_text += f"Service Description: {service[4]}\n"
# message_text += f'Quantity: {}'
message_text += f"Price: {service[3]}$\n"
# Add a button to add the service to the cart
add_to_cart_button = telebot.types.InlineKeyboardButton("Add to cart", callback_data=f'add_{service_id}')
markup.add(add_to_cart_button)
# Add remove buttons
remove_from_cart_button = telebot.types.InlineKeyboardButton('Remove from cart', callback_data=f'remove_'
f'{service_id}')
markup.add(remove_from_cart_button)
# Add button to return to the services
services_button = telebot.types.InlineKeyboardButton(text="← Services", callback_data=f'category_{service[5]}')
markup.add(services_button)
# Add button to view the cart
c.execute('SELECT user_id FROM cart WHERE user_id = ?', (cart_id,))
view_cart_button = telebot.types.InlineKeyboardButton(text="View Cart", callback_data=f'cart_{cart_id}')
markup.add(view_cart_button)
conn.close()
return message_text, markup
def add_to_cart(user_id, service_id, quantity):
# Connect to the database
conn = sqlite3.connect(database)
c = conn.cursor()
query = "SELECT quantity FROM cart WHERE user_id=%s AND service_id=%s" % (user_id, service_id)
c.execute(query)
result = c.fetchone()
if result:
current_quantity = result[0]
new_quantity = current_quantity + quantity
update_query = "UPDATE cart SET quantity=%s WHERE user_id=%s AND service_id=%s" % (new_quantity, user_id,
service_id)
c.execute(update_query)
else:
insert_query = "INSERT INTO cart (user_id, service_id, quantity) VALUES (%s, %s, %s)" % (user_id, service_id,
quantity)
c.execute(insert_query)
conn.commit()
conn.close()
def remove_from_cart(user_id, service_id, quantity):
# Connect to the database
conn = sqlite3.connect(database)
c = conn.cursor()
query = "SELECT quantity FROM cart WHERE user_id=%s AND service_id=%s" % (user_id, service_id)
c.execute(query)
result = c.fetchone()
if result:
current_quantity = result[0]
new_quantity = current_quantity - quantity
if new_quantity > 0:
update_query = "UPDATE cart SET quantity=%s WHERE user_id=%s AND service_id=%s" % (new_quantity, user_id,
service_id)
c.execute(update_query)
else:
delete_query = "DELETE FROM cart WHERE user_id=%s AND service_id=%s" % (user_id, service_id)
c.execute(delete_query)
conn.commit()
conn.close()
def cart_menu(cart_user_id):
# code to view the cart
# Connect to the database
conn = sqlite3.connect(database)
c = conn.cursor()
# Find the user's cart items
c.execute("SELECT service.id, service.name, service.price, service.category_id FROM service "
"INNER JOIN cart ON service.id = cart.service_id "
"WHERE cart.user_id = ?", (cart_user_id, ))
items = c.fetchall()
# Create an inline keyboard markup
markup = telebot.types.InlineKeyboardMarkup()
# Build the message to display the items
if items:
for item in items:
button = telebot.types.InlineKeyboardButton(item[1], callback_data=f'service_{item[3]}_{item[0]}')
markup.add(button)
else:
text = "Your cart is empy"
bot.answer_callback_query(cart_user_id, text=text, cache_time=100)
services_button = telebot.types.InlineKeyboardButton(text="← Categories", callback_data="category_menu")
markup.add(services_button)
# Close the cursor and connection
conn.close()
return markup
# Slash command handler
@bot.message_handler(commands=["menu", "shop", "cart", "help"])
def main_menu_handler(message):
if message.html_text == "/menu":
text = "Main menu"
markup = main_menu()
elif message.html_text == "/shop":
text = "Service Shop"
markup = category_menu()
elif message.html_text == "/cart":
text = "Cart:"
markup = cart_menu(message.from_user.id)
elif message.html_text == "/help":
text, markup = help_page()
else:
markup = "tough titty"
text = "none"
bot.send_message(message.chat.id, text=text, reply_markup=markup, parse_mode='HTML')
@bot.callback_query_handler(func=lambda call: call.data.split("_")[0] in ["main", 'news', 'spainopedia',
"category", "service", "cart"])
def callback_menu_handler(call):
if call.data == "main_menu":
text = "Main menu:"
markup = main_menu()
elif call.data == 'news_page':
text, markup = news_page()
elif call.data == "category_menu":
text = "Service Shop:"
markup = category_menu()
elif call.data.startswith("category_"):
text, markup = service_menu(call.data.split("_")[1])
elif call.data.startswith("service_"):
text, markup = service_detail_menu(call.data.split("_")[2], cart_id=call.from_user.id)
elif call.data.startswith('cart_'):
text = 'Cart'
markup = cart_menu(call.from_user.id)
else:
text = "none"
markup = "no one loves me"
# Edit the current message
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, parse_mode='HTML',
text=text, reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data.split("_")[0] in ["add", "remove"])
def execute_function_callback_handler(callback):
if callback.data.startswith("add_"):
text = "Service added to cart!"
add_to_cart(callback.from_user.id, service_id=callback.data.split("_")[1], quantity=1)
elif callback.data.startswith("remove_"):
text = "Service removed from cart"
remove_from_cart(callback.from_user.id, service_id=callback.data.split("_")[1], quantity=1)
else:
text = "none"
# Confirm the service was added to the cart
bot.answer_callback_query(callback_query_id=callback.id, text=text)
bot.polling(none_stop=True, interval=0)