From d99a016a2b5ecbdadcfc69b9b53bc67bb61962fe Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 22 Apr 2023 21:16:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B1=D0=B5=D0=B7=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=83=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/profile.py | 12 ++--- bot_modules/projects.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ bot_modules/start.py | 10 ++--- bot_sys/keyboard.py | 22 ++++++++++ main.py | 4 +- 5 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 bot_modules/projects.py diff --git a/bot_modules/profile.py b/bot_modules/profile.py index a8e84fb..f28f970 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -36,9 +36,9 @@ user_profile_button_name = "πŸ“° ΠŸΡ€ΠΎΡ„ΠΈΠ»ΡŒ" # --------------------------------------------------------- # Π Π°Π±ΠΎΡ‚Π° с ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ -def GetProfileKeyboardButtons(a_UserAccess): - start_button_names = start.GetButtonNames(a_UserAccess) - return keyboard.MakeKeyboard([start_button_names]) +def GetStartKeyboardButtons(a_UserAccess): + mods = [start] + return keyboard.MakeKeyboardForMods(mods, a_UserAccess) # --------------------------------------------------------- # ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° сообщСний @@ -50,7 +50,7 @@ async def ProfileOpen(a_Message): msg = profile_message if not user_info is None: msg = msg.replace('@user_id', str(user_info[0])).replace('@user_name', str(user_info[1])) - await bot.send_message(user_id, msg, reply_markup = GetProfileKeyboardButtons(None)) + await bot.send_message(user_id, msg, reply_markup = GetStartKeyboardButtons(None)) # --------------------------------------------------------- # Π Π°Π±ΠΎΡ‚Π° с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ @@ -78,13 +78,13 @@ def GetUserInfo(a_UserID): # --------------------------------------------------------- # API -# ДоступныС ΠΊΠ½ΠΎΠΏΠΊΠΈ +# Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π‘Π” def GetInitBDCommands(): return [init_bd_cmd] # ДоступныС ΠΊΠ½ΠΎΠΏΠΊΠΈ def GetButtonNames(a_UserAccess): - return user_profile_button_name + return [user_profile_button_name] # ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΊΠ½ΠΎΠΏΠΎΠΊ def RegisterHandlers(dp : Dispatcher): diff --git a/bot_modules/projects.py b/bot_modules/projects.py new file mode 100644 index 0000000..3198382 --- /dev/null +++ b/bot_modules/projects.py @@ -0,0 +1,114 @@ +# -*- coding: utf8 -*- +# ΠžΠ±Ρ‰Π΅ΡΡ‚Π²Π΅Π½Π½ΠΎΠ΅ достояниС 2023, АлСксСй Π‘Π΅Π·Π±ΠΎΡ€ΠΎΠ΄ΠΎΠ² (Alexei Bezborodov) + +# ΠŸΡ€ΠΎΠ΅ΠΊΡ‚Ρ‹ + +from bot_sys import bot_bd, log, config, keyboard +from bot_modules import start +from aiogram import Bot, types + +import sqlite3 + +from aiogram.dispatcher import Dispatcher + +bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) + +# --------------------------------------------------------- +# Π‘Π” +init_bd_cmd = '''CREATE TABLE IF NOT EXISTS projects( + projectPhoto TEXT, + projectName TEXT, + projectDesc TEXT, + parentID INTEGER, + projectID INTEGER PRIMARY KEY +)''' + +# --------------------------------------------------------- +# БообщСния + +select_project_message = ''' +πŸ›’ ΠŸΡ€ΠΎΠ΅ΠΊΡ‚Ρ‹ + +ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚: +''' + +error_find_proj_message = ''' +Ошибка, ΠΏΡ€ΠΎΠ΅ΠΊΡ‚ с ID @project_id Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ +''' + +project_open_message = ''' +ΠŸΡ€ΠΎΠ΅ΠΊΡ‚: @proj_name + +@proj_desk +''' + +projects_button_name = "πŸ“° ΠŸΡ€ΠΎΠ΅ΠΊΡ‚Ρ‹" + +# ΠŸΡ€Π΅Ρ„ΠΈΠΊΡΡ‹ +select_project_callback_prefix = 'project:' + +# --------------------------------------------------------- +# Π Π°Π±ΠΎΡ‚Π° с ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ + +def GetStartKeyboardButtons(a_UserAccess): + mods = [start] + return keyboard.MakeKeyboardForMods(mods, a_UserAccess) + +# --------------------------------------------------------- +# ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° сообщСний + +# ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ всСх ΠΏΡ€ΠΎΠ΅ΠΊΡ‚ΠΎΠ² Π±Π΅Π· родитСля +async def ProjectsOpen(a_Message): + projects = GetProjectList(0) + projects_button_list = [] + for t in projects: + b = keyboard.Button(str(t[1]), t[4]) + await bot.send_message(a_Message.from_user.id, select_project_message, reply_markup=keyboard.MakeInlineKeyboard(projects_button_list, select_project_callback_prefix)) + +async def ShowProject(a_CallbackQuery : types.CallbackQuery): + project_id = str(a_CallbackQuery.data).replace(select_project_callback_prefix, '') + project = GetProject(project_id) + if len(project) != 1: + log.Error(f'ΠŸΡ€ΠΎΠ΅ΠΊΡ‚ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ {project_id}') + msg = Ошибка.replace('@project_id', project_id) + await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup=keyboard.MakeKeyboardForMods([start])) + return + + p = project[0] + msg = project_open_message.replace('@proj_name', p[1]).replace('@proj_desk', p[2]) + await bot.send_photo(a_CallbackQuery.from_user.id, p[0], msg, reply_markup=keyboard.MakeKeyboardForMods([start])) + +# --------------------------------------------------------- +# Π Π°Π±ΠΎΡ‚Π° с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ + +def GetProjectList(a_ParentID): + db = sqlite3.connect(bot_bd.GetBDFileName()) + cursor = db.cursor() + projects = cursor.execute('SELECT * FROM projects where parentID = ?', ([a_ParentID])).fetchall() + cursor.close() + db.close() + return projects + +def GetProject(a_ProjectID): + db = sqlite3.connect(bot_bd.GetBDFileName()) + cursor = db.cursor() + project = cursor.execute('SELECT * FROM categories WHERE catID = ?', ([catID])).fetchall() + cursor.close() + db.close() + return project + +# --------------------------------------------------------- +# API + +# Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π‘Π” +def GetInitBDCommands(): + return [init_bd_cmd] + +# ДоступныС ΠΊΠ½ΠΎΠΏΠΊΠΈ +def GetButtonNames(a_UserAccess): + return [projects_button_name] + +# ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΊΠ½ΠΎΠΏΠΎΠΊ +def RegisterHandlers(dp : Dispatcher): + dp.register_message_handler(ProjectsOpen, text = projects_button_name) + dp.register_callback_query_handler(ShowProject, lambda x: x.data.startswith(select_project_callback_prefix)) diff --git a/bot_modules/start.py b/bot_modules/start.py index 4521af0..034a238 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -4,7 +4,7 @@ # Π‘Ρ‚Π°Ρ€Ρ‚ΠΎΠ²ΠΎΠ΅ мСню from bot_sys import log, config, keyboard -from bot_modules import profile +from bot_modules import profile, projects from aiogram.dispatcher import Dispatcher @@ -23,8 +23,8 @@ start_menu_button_name = "☰ Π“Π»Π°Π²Π½ΠΎΠ΅ мСню" # Π Π°Π±ΠΎΡ‚Π° с ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ def GetStartKeyboardButtons(a_UserAccess): - profile_button_names = profile.GetButtonNames(a_UserAccess) - return keyboard.MakeKeyboard([profile_button_names]) + mods = [profile, projects] + return keyboard.MakeKeyboardForMods(mods, a_UserAccess) # --------------------------------------------------------- # ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° сообщСний @@ -40,13 +40,13 @@ async def StartMenu(a_Message): # --------------------------------------------------------- # API -# ДоступныС ΠΊΠ½ΠΎΠΏΠΊΠΈ +# Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π‘Π” def GetInitBDCommands(): return None # ИмСна доступных ΠΊΠ½ΠΎΠΏΠΎΠΊ def GetButtonNames(a_UserAccess): - return start_menu_button_name + return [start_menu_button_name] # ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΊΠ½ΠΎΠΏΠΎΠΊ def RegisterHandlers(dp : Dispatcher): diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index b67396b..52a00e6 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -6,6 +6,14 @@ from aiogram import types, Bot, Dispatcher from aiogram.types import ReplyKeyboardRemove, ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton +def GetButtons(a_ModList, a_UserAccess): + names = [] + for m in a_ModList: + n = m.GetButtonNames(a_UserAccess) + if not n is None or len(n) != 0: + names += n + return names + def MakeKeyboard(a_ButtonList): key = types.ReplyKeyboardMarkup(resize_keyboard = True) for b in a_ButtonList: @@ -13,3 +21,17 @@ def MakeKeyboard(a_ButtonList): key.add(k) return key + +def MakeKeyboardForMods(a_ModList, a_UserAccess): + names = GetButtons(a_ModList, a_UserAccess) + return MakeKeyboard(names) + +class Button: + def __init__(self, a_Label, a_CallBackData): + self.label = a_Label + self.callback_data = a_CallBackData + +def MakeInlineKeyboard(a_ButtonList, a_CallBackPrefix): + inline_keyboard = InlineKeyboardMarkup(resize_keyboard=True, row_width=2) + for b in a_ButtonList: + inline_keyboard.insert(types.InlineKeyboardButton(text=b.label, callback_data=f'{a_CallBackPrefix}{b.callback_data}')) diff --git a/main.py b/main.py index 14c84b9..3406e94 100644 --- a/main.py +++ b/main.py @@ -10,13 +10,13 @@ from aiogram.dispatcher import Dispatcher from aiogram.contrib.fsm_storage.memory import MemoryStorage import sqlite3 from bot_sys import config, log, bot_bd -from bot_modules import profile, start +from bot_modules import profile, start, projects storage = MemoryStorage() bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML) dp = Dispatcher(bot, storage = storage) -mods = [profile, start] +mods = [profile, start, projects] init_bd_cmd = [] for m in mods: