import telebot
from database.dbapi import DatabaseConnector
API_TOKEN = 'your_telegram_bot_token_here'
bot = telebot.TeleBot(API_TOKEN)
connection_string = "postgresql+psycopg2://username:password@localhost:5432/db_name"
db_connector = DatabaseConnector(connection_string)
user_data = {}
class UserData:
def init(self):
self.title = None
self.author = None
self.published = None
@bot.message_handler(commands=['add_book'])
def start_add_book(message):
user_data[message.chat.id] = UserData()
bot.send_message(message.chat.id, "Введите название книги:")
bot.register_next_step_handler(message, process_title_step)
def process_title_step(message):
user_data[message.chat.id].title = message.text
bot.send_message(message.chat.id, "Введите автора книги:")
bot.register_next_step_handler(message, process_author_step)
def process_author_step(message):
user_data[message.chat.id].author = message.text
bot.send_message(message.chat.id, "Введите год издания книги:")
bot.register_next_step_handler(message, process_published_step)
def process_published_step(message):
user_data[message.chat.id].published = int(message.text)
add_book_to_db(message.chat.id)
def add_book_to_db(chat_id):
title = user_data[chat_id].title
author = user_data[chat_id].author
published = user_data[chat_id].published
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Добро пожаловать! Введите /add_book, чтобы начать добавление книги.")
bot.polling()