Browse Source

Переделка простых сообщений, кроме проектов

test_bot
Alexei 2 years ago
parent
commit
3e1d92b48e
  1. 14
      bot_modules/backup.py
  2. 20
      bot_modules/profile.py
  3. 13
      bot_modules/start.py
  4. 15
      template/simple_message.py

14
bot_modules/backup.py

@ -5,7 +5,8 @@
from bot_sys import bot_bd, log, config, keyboard, user_access
from bot_modules import start, access, groups
from template import file_message
from template import file_message, simple_message
from aiogram import Bot, types
import sqlite3
@ -61,14 +62,6 @@ def GetBackupKeyboardButtons(a_UserGroups):
# ---------------------------------------------------------
# Обработка сообщений
async def BackupOpen(a_Message):
user_id = str(a_Message.from_user.id)
user_groups= groups.GetUserGroupData(user_id)
if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW):
return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetBackupKeyboardButtons(user_groups))
await bot.send_message(user_id, backup_message, reply_markup = GetBackupKeyboardButtons(user_groups))
# ---------------------------------------------------------
# Работа с базой данных пользователей
@ -88,6 +81,7 @@ def GetModuleButtons():
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(BackupOpen, text = backup_button_name)
dp.register_message_handler(simple_message.InfoMessageTemplate(backup_message, GetBackupKeyboardButtons, GetAccess), text = backup_button_name)
dp.register_message_handler(file_message.BackupFileTemplate(bot_bd.GetBDFileName(), backup_bd_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_bd_button_name)
dp.register_message_handler(file_message.BackupFileTemplate(log.g_log_file_name, backup_log_message, GetAccess, GetBackupKeyboardButtons, error_backup_message), text = backup_log_button_name)

20
bot_modules/profile.py

@ -5,14 +5,11 @@
from bot_sys import bot_bd, log, config, keyboard, user_access
from bot_modules import start, access, groups
from aiogram import Bot, types
import sqlite3
from template import simple_message
from aiogram import types
from aiogram.dispatcher import Dispatcher
bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML)
# ---------------------------------------------------------
# БД
module_name = 'profile'
@ -47,18 +44,12 @@ def GetStartKeyboardButtons(a_UserGroups):
# ---------------------------------------------------------
# Обработка сообщений
# Отображение профиля пользователя
async def ProfileOpen(a_Message):
user_id = str(a_Message.from_user.id)
user_groups= groups.GetUserGroupData(user_id)
if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW):
return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups))
user_info = GetUserInfo(user_id)
user_info = GetUserInfo(a_Message.from_user.id)
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 = GetStartKeyboardButtons(user_groups))
return msg
# ---------------------------------------------------------
# Работа с базой данных пользователей
@ -67,7 +58,6 @@ async def ProfileOpen(a_Message):
def AddUser(a_UserID, a_UserName):
bot_bd.SQLRequestToBD("INSERT OR IGNORE INTO users (user_id, userName) VALUES (?, ?);", commit=True, param = (a_UserID, a_UserName))
# Добавление пользователя, если он уже есть, то игнорируем
def GetUserInfo(a_UserID):
user_info = bot_bd.SQLRequestToBD('SELECT * FROM users WHERE user_id = ?', param = [a_UserID])
print(user_info, str(user_info))
@ -91,4 +81,4 @@ def GetModuleButtons():
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(ProfileOpen, text = user_profile_button_name)
dp.register_message_handler(simple_message.SimpleMessageTemplate(ProfileOpen, GetStartKeyboardButtons, GetAccess), text = user_profile_button_name)

13
bot_modules/start.py

@ -5,6 +5,7 @@
from bot_sys import log, config, keyboard, user_access
from bot_modules import profile, projects, groups, access, backup
from template import simple_message
from aiogram.dispatcher import Dispatcher
@ -38,15 +39,11 @@ def GetStartKeyboardButtons(a_UserGroups):
# Первичное привестивие
async def StartMenu(a_Message):
user_id = int(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW):
return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups))
user_id = str(a_Message.from_user.id)
user_name = str(a_Message.from_user.username)
profile.AddUser(user_id, user_name)
log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте')
await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(user_groups), parse_mode='HTML')
return start_message
# ---------------------------------------------------------
# API
@ -64,7 +61,7 @@ def GetModuleButtons():
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(StartMenu, commands = ['start'])
dp.register_message_handler(StartMenu, text = start_menu_button_name)
dp.register_message_handler(simple_message.SimpleMessageTemplate(StartMenu, GetStartKeyboardButtons, GetAccess), commands = ['start'])
dp.register_message_handler(simple_message.SimpleMessageTemplate(StartMenu, GetStartKeyboardButtons, GetAccess), text = start_menu_button_name)

15
template/simple_message.py

@ -8,10 +8,19 @@ from bot_modules import access, groups
from aiogram import types
def InfoMessageTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):
async def Help(a_Message : types.message):
async def GetMessage(a_Message : types.message):
return a_HelpMessage
return SimpleMessageTemplate(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode)
def SimpleMessageTemplate(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW):
async def SimpleMessage(a_Message : types.message):
user_id = str(a_Message.from_user.id)
user_groups = groups.GetUserGroupData(user_id)
if not user_access.CheckAccessString(a_AccessFunc(), user_groups, access_mode):
return await a_Message.answer(access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups))
await a_Message.answer(a_HelpMessage, reply_markup = a_GetButtonsFunc(user_groups))
return Help
msg = await a_WorkFunc(a_Message)
if not msg is None:
await a_Message.answer(msg, reply_markup = a_GetButtonsFunc(user_groups))
return SimpleMessage

Loading…
Cancel
Save