Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
3
Indexable
Never
from sqlalchemy import create_engine, Column, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import telebot

from datetime import datetime
TOKEN = '6461781031:AAEF5F8ejqPPChZ20XyWBHGXLZSsw8hcKAY'

DATABASE_PATH = 'sqlite:///books.db'


bot = telebot.TeleBot(TOKEN)


Base = declarative_base()

class Message(Base):
    __tablename__ = 'messages'
    id = Column(String, primary_key=True)
    text = Column(String)
    date_sent = Column(DateTime, default=datetime.now)
engine = create_engine(DATABASE_PATH)
Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

@bot.message_handler(func=lambda message: True)
def handle_text(message):

    new_message = Message(id=str(message.message_id), text=message.text, date_sent=datetime.now())
    session.add(new_message)
    session.commit()

    bot.send_message(message.chat.id, "Сообщение успешно записано в базу данных!")


if __name__ == "__main__":
    bot.polling(none_stop=True)
Leave a Comment