tbot added

This commit is contained in:
pErfEcto2
2023-04-10 21:23:27 +03:00
parent 13b1d87d79
commit 1b8e94b5f5
2 changed files with 50 additions and 2 deletions
@@ -1,6 +1,5 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from wget import download from wget import download
import psycopg2 as psyc import psycopg2 as psyc
@@ -27,5 +26,4 @@ with psyc.connect(dbname="db_for_parse", user="perfecto") as conn:
cursor.execute(f"""insert into images(link, name, price, description, file) cursor.execute(f"""insert into images(link, name, price, description, file)
values ('{image}', '{title}', '{price}', '{desc}', 'images/{i}.jpg')""") values ('{image}', '{title}', '{price}', '{desc}', 'images/{i}.jpg')""")
conn.commit() conn.commit()
+50
View File
@@ -0,0 +1,50 @@
import telebot as tb
import config
import psycopg2 as ps
def exec_query(query: str) -> list:
with ps.connect(dbname=config.db_name, user=config.user) as conn:
with conn.cursor() as cur:
cur.execute(query)
res = cur.fetchall()
return res
def get_data(mess, bot, rows_number: int) -> None:
try:
product_id = int(mess.text)
except BaseException:
bot.send_message(mess.chat.id, "Invalid id")
return
if product_id > rows_number:
bot.send_message(mess.chat.id, "Id is bigger than number of products")
return
else:
data = exec_query(
f"select * from images where id = {product_id}")[0][1:]
bot.send_message(mess.chat.id, "\n\n".join(data))
bot = tb.TeleBot(config.token)
keyboard = tb.types.ReplyKeyboardMarkup()
keyboard.row("get data")
@bot.message_handler(commands=["start"])
def hi(mess):
bot.send_message(mess.chat.id, "HI there!", reply_markup=keyboard)
@bot.message_handler(content_types=["text"])
def f(mess):
if mess.text == "get data":
row_number = int(exec_query("select count(*) from images")[0][0])
bot.send_message(mess.chat.id, f"Write id between 1 and {row_number}")
bot.register_next_step_handler(mess, get_data, bot, row_number)
bot.polling(non_stop=True)