Compare commits
No commits in common. "3d69805765a8d7a755ee911ff39885e2c4c30488" and "89f28281facbd38f54186b415779aa75784b8eac" have entirely different histories.
3d69805765
...
89f28281fa
2 changed files with 4 additions and 59 deletions
63
main.py
63
main.py
|
@ -8,59 +8,12 @@ class Database:
|
|||
self.__create_table()
|
||||
def __create_table(self):
|
||||
sql = self.connect_db()
|
||||
sql["cursor"].execute("""
|
||||
create table if not exists users (
|
||||
id integer primary key autoincrement,
|
||||
id_telegram integer not null unique,
|
||||
username text,
|
||||
first_name text,
|
||||
last_name text,
|
||||
date_register date,
|
||||
access boolean default 1
|
||||
)
|
||||
""")
|
||||
sql["cursor"].execute("""
|
||||
create table if not exists messages (
|
||||
id integer primary key autoincrement,
|
||||
id_user integer not null,
|
||||
message_id integer not null,
|
||||
message_text text not null,
|
||||
date_send date,
|
||||
status boolean default 0 check(status in (0,1)),
|
||||
foreign key (id_user) references users(id)
|
||||
)
|
||||
""")
|
||||
|
||||
self.close(sql['connect'],sql['cursor'])
|
||||
def connect_db(self):
|
||||
with sqlite3.connect(self.db_name) as connect:
|
||||
cursor = connect.cursor()
|
||||
return {"connect":connect,"cursor":cursor}
|
||||
def check_user(self,user_id: int):
|
||||
sql = self.connect_db()
|
||||
sql["cursor"].execute("select * from users where id_telegram = ?", (user_id,))
|
||||
info_users = sql["cursor"].fetchone()
|
||||
print(info_users)
|
||||
self.close(sql['connect'],sql['cursor'])
|
||||
if info_users is None:
|
||||
return {"status":False}
|
||||
return {"status":True,"info_user":info_users}
|
||||
def create_user(self, message: telebot.types.Message):
|
||||
sql = self.connect_db()
|
||||
date = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
sql["cursor"].execute("""
|
||||
insert into users (id_telegram, username, first_name, last_name, date_register) values (?,?,?,?,?)
|
||||
""", (message.from_user.id, message.from_user.username,message.from_user.first_name,message.from_user.last_name,date))
|
||||
sql["connect"].commit()
|
||||
self.close(sql['connect'],sql['cursor'])
|
||||
def create_message(self,message: telebot.types.Message):
|
||||
sql = self.connect_db()
|
||||
date = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
# TODO: specify params to query( you can get from check_user)
|
||||
sql["cursor"].execute("""
|
||||
insert into messages (id_user, message_id, message_text, date_send) values (?,?,?,?)
|
||||
""", ())
|
||||
self.close(sql['connect'],sql['cursor'])
|
||||
def close(self,connect,cursor):
|
||||
cursor.close()
|
||||
connect.close()
|
||||
|
@ -71,28 +24,20 @@ class TelegramBot(Database):
|
|||
super().__init__(db_name)
|
||||
self.bot = telebot.TeleBot(token)
|
||||
self.router()
|
||||
|
||||
def router(self):
|
||||
@self.bot.message_handler(commands=['start'])
|
||||
def start(message):
|
||||
text = ""
|
||||
if self.check_user(message.from_user.id)["status"]:
|
||||
text += f"С возвращением, {message.from_user.first_name}"
|
||||
else:
|
||||
self.create_user(message)
|
||||
text += f"Добро пожаловать, {message.from_user.first_name}"
|
||||
self.bot.send_message(
|
||||
message.chat.id,
|
||||
text
|
||||
f"Добро пожаловать, {message.from_user.first_name}"
|
||||
)
|
||||
@self.bot.message_handler(func=lambda message: True)
|
||||
def echo_all(message):
|
||||
|
||||
self.bot.reply_to(message,"Сообщение отправлено админу")
|
||||
|
||||
self.bot.reply_to(message,"Не понимаю")
|
||||
self.bot.delete_message(chat_id=message.chat.id,message_id=message.message_id,timeout=1000)
|
||||
self.bot.polling()
|
||||
|
||||
TelegramBot(
|
||||
db_name="tg.db",
|
||||
token="7037219892:AAGoZ1bKjN79KMiEGqYCWtPOEOk8s_N0RI4"
|
||||
token=""
|
||||
)
|
BIN
req.txt
BIN
req.txt
Binary file not shown.
Loading…
Reference in a new issue