diff --git a/main.py b/main.py index 6329dd8..30d2a69 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,15 @@ -import os -from dataclasses import dataclass -import sqlite3 -from telegram import Update -from telegram.ext import Updater, CommandHandler, CallbackContext import logging +import os +import sqlite3 +from dataclasses import dataclass +from datetime import datetime from dotenv import load_dotenv +from telegram import Update +from telegram.ext import CallbackContext, CommandHandler, Updater + +from models import Link + load_dotenv() logging.basicConfig(level=logging.DEBUG, @@ -46,15 +50,23 @@ class Bot: link TEXT, user_id TEXT, read_at DATETIME, - added_at DATETIME, - postponed_at DATETIME - ) + added_at DATETIME + ); """) def cmd_start(self, update: Update, context: CallbackContext): context.bot.send_message(chat_id=update.effective_chat.id, text='Hi!') + # TODO: timezones # TODO: ask to set time or something + def cmd_test(self, update: Update, context: CallbackContext): + l = Link( + link="2137", + user_id="papiez" + ) + with self.db as db: + l.create(db) + def run(self): self.updater.start_polling() diff --git a/models.py b/models.py new file mode 100644 index 0000000..df83d9a --- /dev/null +++ b/models.py @@ -0,0 +1,25 @@ +import sqlite3 +from dataclasses import dataclass +from datetime import datetime + + +@dataclass +class Link: + link: str + user_id: str + id: int = None + read_at: datetime = None + added_at: datetime = None + + def create(self, db: sqlite3.Connection): + self.added_at = datetime.now() + cur = db.cursor() + rows = (cur.execute( + "INSERT INTO links(link, user_id, added_at) VALUES(?, ?, ?) RETURNING id;", + (self.link, self.user_id, self.added_at) + )) + r = list(rows) + assert len(r) == 1 + assert len(r[0]) == 1 + assert int(r[0][0]) + self.id = r[0][0]