attempt to create telegramBot

This commit is contained in:
Harutyun
2023-04-26 14:09:54 +03:00
parent 4438313589
commit 842f08f773
3 changed files with 38 additions and 8 deletions
+30
View File
@@ -0,0 +1,30 @@
import psycopg2
import wget
from Задания.task1.Garanyan import parser
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres',
password='Q1w2e3r4')
cursor = connection.cursor()
create_q = '''CREATE TABLE Food
(ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))'''
try:
cursor.execute(create_q)
connection.commit()
except psycopg2.errors.DuplicateTable:
print("s")
for j in range(15):
url = parser.Image[j].find('img').attrs['src']
tempf = f"C:\\Users\\Harutyun\\Desktop\\prog\\food{j}.jpg"
wget.download(url, tempf)
try:
insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{parser.Name[j].text}', '{parser.Price[j].text}', '{tempf}') '''
cursor.execute(insert_query)
connection.commit()
except:
connection.rollback()
cursor.close()
connection.close()
+8 -8
View File
@@ -2,7 +2,7 @@ import telebot
import psycopg2 import psycopg2
from telebot import types from telebot import types
import random import random
#hello
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres',
password='Q1w2e3r4') password='Q1w2e3r4')
cursor = connection.cursor() cursor = connection.cursor()
@@ -11,8 +11,7 @@ try:
cursor.execute(sel_query) cursor.execute(sel_query)
except psycopg2.errors.UndefinedTable: except psycopg2.errors.UndefinedTable:
connection.rollback() connection.rollback()
import tableCreator import problems
sel_query = """SELECT * FROM public.food""" sel_query = """SELECT * FROM public.food"""
cursor.execute(sel_query) cursor.execute(sel_query)
@@ -25,14 +24,14 @@ bot = telebot.TeleBot('841097550:AAFc5MoFRivTEfv-gOSJctqH53NfYMTKpCc')
def start(m, res=False): def start(m, res=False):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True) markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Хот-доги и соусы!") item1 = types.KeyboardButton("Хот-доги и соусы!")
item2 = types.KeyboardButton("Повтори!") item2 = types.KeyboardButton("Случайная фотография нашего товара!")
markup.add(item1) markup.add(item1)
markup.add(item2) markup.add(item2)
bot.send_message(m.chat.id, bot.send_message(m.chat.id,
'"Хот-доги и соусы!" - для получения случайного товара из нашей хотдожной\n "Повтори!" - для повторения сообщений.', '"Хот-доги и соусы!" - для получения случайного товара из нашей хотдожной\n'
' "Случайная фотография нашего товара!" - для вывода фотографий',
reply_markup=markup) reply_markup=markup)
@bot.message_handler(content_types=["text"]) @bot.message_handler(content_types=["text"])
def handle_text(message): def handle_text(message):
if (message.text.strip() == 'Хот-доги и соусы!'): if (message.text.strip() == 'Хот-доги и соусы!'):
@@ -40,8 +39,9 @@ def handle_text(message):
ans = f'Название: {i[1]}\nЦена: {i[2]}\n' ans = f'Название: {i[1]}\nЦена: {i[2]}\n'
bot.send_message(message.chat.id, ans) bot.send_message(message.chat.id, ans)
bot.send_photo(message.chat.id, open(i[3], 'rb')) bot.send_photo(message.chat.id, open(i[3], 'rb'))
elif (message.text.strip() == 'Повтори!'): elif (message.text.strip() == 'Случайная фотография нашего товара!'):
bot.send_message(message.chat.id, 'Ввод: ' + message.text) i = random.choice(array)
bot.send_photo(message.chat.id, open(i[3], 'rb'))
bot.polling(none_stop=True, interval=0) bot.polling(none_stop=True, interval=0)