Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: build

on:
push:
branches: [ main, develop, gh-actions ]
branches: [ main, gh-actions ]
pull_request:
branches: [ main, develop, gh-actions ]
branches: [ main, gh-actions ]

jobs:
build:
Expand Down
2 changes: 0 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
from . import keyboards
from . import services

import main
import create_bot

34 changes: 34 additions & 0 deletions src/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from aiogram import executor

from create_bot import dp, logger
from handlers import admin, client, shared
from db import sqlite_db


async def on_startup(_) -> None:
logger.info("Bot now is online!")
sqlite_db.start_db()


async def on_shutdown(_) -> None:
logger.info("Bot now is offline!")
sqlite_db.stop_db()


def main():
# Регистрация handler-функций.
admin.register_admin_handlers(dp)
client.register_client_handlers(dp)
shared.register_shared_handlers(dp)

# Запуск бота в режиме опроса.
executor.start_polling(
dispatcher=dp,
skip_updates=True,
on_startup=on_startup,
on_shutdown=on_shutdown,
)


if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion src/create_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

from dotenv import load_dotenv

from aiogram import Bot, Dispatcher, types
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage

load_dotenv('.env')

# Configure logging
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)

# Initialize storage for FSM.
Expand Down
4 changes: 2 additions & 2 deletions src/db/init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ CREATE TABLE IF NOT EXISTS queues_list

CREATE TABLE IF NOT EXISTS queue
(
id INTEGER REFERENCES queues_list (id),
msg_id INTEGER
id INTEGER REFERENCES queues_list (id),
msg_id INTEGER
);
109 changes: 75 additions & 34 deletions src/db/sqlite_db.py
Original file line number Diff line number Diff line change
@@ -1,130 +1,171 @@
import os
import sqlite3
import logging

from datetime import datetime
from typing import Tuple

conn = sqlite3.connect('queue_bot.db')
conn = sqlite3.connect(
os.getenv(
'DATABASE',
default='queue_bot.db',
)
)
cursor = conn.cursor()

logging.basicConfig(level=logging.INFO)

def start_db() -> None:
if os.getenv('DEBUG', 'True') == 'True':
sql_file_path = 'db/init_db.sql'
else:
sql_file_path = '/app/src/db/init_db.sql'

def start_db() -> None:
debug = bool(os.getenv('DEBUG', 'True'))
logging.info(f'DEBUG={debug}')
sql_file_path = 'db/init_db.sql' if debug else '/app/src/db/init_db.sql'
with open(sql_file_path, 'r') as sql_file:
sql_script = sql_file.read()

cursor.executescript(sql_script)
conn.commit()

if conn:
print("Data base has been connected!")
logging.info("Database has been connected!")


def stop_db() -> None:
cursor.close()
conn.close()
logging.info("Database has been disconnected!")


def sql_get_queue_list(admin_id_: int) -> list:
cursor.execute(
"SELECT id, queue_name, start, chat_id, chat_title FROM queues_list WHERE assignee_id = ?",
(admin_id_,)
"SELECT id, queue_name, start, chat_id, chat_title "
"FROM queues_list "
"WHERE assignee_id = ?",
(admin_id_,),
)

return cursor.fetchall()


async def sql_get_queue_from_list(id_: int) -> tuple:
cursor.execute(
"SELECT * FROM queues_list WHERE id = ?",
(id_,)
"SELECT * "
"FROM queues_list "
"WHERE id = ?",
(id_,),
)

return cursor.fetchone()


def sql_get_chat_title(chat_id_: int) -> tuple:
cursor.execute(
"SELECT chat_title FROM chat WHERE chat_id = ?", (chat_id_,)
"SELECT chat_title "
"FROM chat "
"WHERE chat_id = ?",
(chat_id_,),
)

return cursor.fetchone()


def sql_get_managed_chats(admin_id_: int) -> list:
cursor.execute(
f"SELECT chat_id, chat_title FROM chat WHERE assignee_id = ?", (admin_id_,)
f"SELECT chat_id, chat_title "
f"FROM chat "
f"WHERE assignee_id = ?",
(admin_id_,),
)

return cursor.fetchall()


async def sql_add_admin(admin_id_: int, user_name_: str) -> None:
cursor.execute(
"INSERT OR IGNORE INTO admin VALUES (?, ?)",
(admin_id_, user_name_)
"INSERT OR IGNORE INTO admin "
"VALUES (?, ?)",
(admin_id_, user_name_),
)
conn.commit()


async def sql_add_managed_chat(admin_id_: int, chat_id_: int, chat_title_: str) -> None:
cursor.execute(
"INSERT INTO chat ('assignee_id', 'chat_id', 'chat_title') VALUES (?, ?, ?)",
(admin_id_, chat_id_, chat_title_)
"INSERT INTO chat "
"('assignee_id', 'chat_id', 'chat_title') "
"VALUES (?, ?, ?)",
(admin_id_, chat_id_, chat_title_),
)
conn.commit()


async def sql_delete_managed_chat(chat_id_: int) -> None:
cursor.execute(
"DELETE FROM chat WHERE chat_id = ?", (chat_id_,)
"DELETE FROM chat "
"WHERE chat_id = ?",
(chat_id_,),
)
conn.commit()
cursor.execute(
"DELETE FROM queues_list WHERE chat_id = ?", (chat_id_,)
"DELETE FROM queues_list "
"WHERE chat_id = ?",
(chat_id_,),
)
conn.commit()


async def sql_add_queue(admin_id_: int, queue_name_: str, start_dt: datetime, chat_id_: int, chat_title_: str) -> tuple:
cursor.execute(
"INSERT INTO queues_list ('assignee_id', 'queue_name', 'start', 'chat_id', 'chat_title') "
"VALUES (?, ?, ?, ?, ?)", (admin_id_, queue_name_, start_dt, chat_id_, chat_title_)
"INSERT INTO queues_list "
"('assignee_id', 'queue_name', 'start', 'chat_id', 'chat_title') "
"VALUES (?, ?, ?, ?, ?)",
(admin_id_, queue_name_, start_dt, chat_id_, chat_title_),
)
conn.commit()

cursor.execute(
"SELECT id FROM queues_list WHERE assignee_id = ? AND queue_name = ? AND chat_id = ?",
(admin_id_, queue_name_, chat_id_)
"SELECT id "
"FROM queues_list "
"WHERE assignee_id = ? AND queue_name = ? AND chat_id = ?",
(admin_id_, queue_name_, chat_id_),
)

return cursor.fetchone()


async def sql_delete_queue(id_: int) -> Tuple[int, int]:
cursor.execute(
"SELECT chat_id FROM queues_list WHERE id = ?", (id_,)
"SELECT chat_id "
"FROM queues_list "
"WHERE id = ?",
(id_,),
)
chat_id: tuple = cursor.fetchone()

cursor.execute(
"DELETE FROM queues_list WHERE id = ?", (id_,)
"DELETE FROM queues_list "
"WHERE id = ?",
(id_,),
)
conn.commit()

cursor.execute(
"SELECT msg_id FROM queue WHERE id = ?", (id_,)
"SELECT msg_id "
"FROM queue "
"WHERE id = ?",
(id_,),
)
msg_id: tuple = cursor.fetchone()

cursor.execute(
"DELETE FROM queue WHERE id = ?", (id_,)
"DELETE FROM queue "
"WHERE id = ?",
(id_,),
)
conn.commit()

return chat_id[0], msg_id[0]


async def sql_post_queue_msg_id(queue_id_: int, msg_id_: int):
cursor.execute(
"INSERT INTO queue ('id', 'msg_id') VALUES (?, ?)", (queue_id_, msg_id_)
"INSERT INTO queue "
"('id', 'msg_id') "
"VALUES (?, ?)",
(queue_id_, msg_id_),
)
conn.commit()
Loading