From 8fc1cf3f164a173a36aed65e784193f1fdbe0b55 Mon Sep 17 00:00:00 2001 From: Harutyun Date: Thu, 20 Apr 2023 11:06:26 +0300 Subject: [PATCH] parser with database --- Задания/task1/Garanyan/tgbot.py | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Задания/task1/Garanyan/tgbot.py diff --git a/Задания/task1/Garanyan/tgbot.py b/Задания/task1/Garanyan/tgbot.py new file mode 100644 index 0000000..91ffddf --- /dev/null +++ b/Задания/task1/Garanyan/tgbot.py @@ -0,0 +1,47 @@ +import telebot +import psycopg2 +from telebot import types +import random + +connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', + password='Q1w2e3r4') +cursor = connection.cursor() +try: + sel_query = """SELECT * FROM public.food""" + cursor.execute(sel_query) +except psycopg2.errors.UndefinedTable: + connection.rollback() + import tableCreator + + sel_query = """SELECT * FROM public.food""" + cursor.execute(sel_query) + +array = list(cursor.fetchall()) + +bot = telebot.TeleBot('841097550:AAFc5MoFRivTEfv-gOSJctqH53NfYMTKpCc') + + +@bot.message_handler(commands=["start"]) +def start(m, res=False): + markup = types.ReplyKeyboardMarkup(resize_keyboard=True) + item1 = types.KeyboardButton("Хот-доги и соусы!") + item2 = types.KeyboardButton("Повтори!") + markup.add(item1) + markup.add(item2) + bot.send_message(m.chat.id, + '"Хот-доги и соусы!" - для получения случайного товара из нашей хотдожной\n "Повтори!" - для повторения сообщений.', + reply_markup=markup) + + +@bot.message_handler(content_types=["text"]) +def handle_text(message): + if (message.text.strip() == 'Хот-доги и соусы!'): + i = random.choice(array) + ans = f'Название: {i[1]}\nЦена: {i[2]}\n' + bot.send_message(message.chat.id, ans) + bot.send_photo(message.chat.id, open(i[3], 'rb')) + elif (message.text.strip() == 'Повтори!'): + bot.send_message(message.chat.id, 'Ввод: ' + message.text) + + +bot.polling(none_stop=True, interval=0) \ No newline at end of file