diff --git a/bot.py b/bot.py index 0d74980..e670c24 100755 --- a/bot.py +++ b/bot.py @@ -22,11 +22,11 @@ logging.basicConfig( ) logger = logging.getLogger("Bot") -WEBAPP_HOST = config.bot("ip") -WEBAPP_PORT = config.bot("port") +WEBAPP_HOST = config.ip +WEBAPP_PORT = config.port WEBHOOK_HOST = f'http://{WEBAPP_HOST}:{WEBAPP_PORT}' -WEBHOOK_PATH = f'/bot{config.bot("token")}/' +WEBHOOK_PATH = f'/bot{config.token}/' WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}" @@ -40,7 +40,7 @@ async def on_shutdown(dp): def main() -> None: - if config.logging_user: + if config.enable_logging: logger.info("Logging enabled!") else: logger.info("Logging disabled!") @@ -50,7 +50,7 @@ def main() -> None: asyncio.set_event_loop(loop) loop.create_task(scheduler()) - if config.bot("use_webhook").lower() in ['t', 'true', '1', 'yes', 'y']: + if config.use_webhook.lower() in ['t', 'true', '1', 'yes', 'y']: executor.start_webhook( dispatcher=dp, loop=loop, diff --git a/configs/configure.py b/configs/configure.py index e97a63f..2569ec0 100644 --- a/configs/configure.py +++ b/configs/configure.py @@ -1,32 +1,28 @@ from configparser import ConfigParser - -from .module import Config +from easydict import EasyDict as edict CONFIG_FILE = 'config.ini' -class Configure(Config): +class Configure: def __init__(self): - - self.config = ConfigParser() + config = ConfigParser() + config.read(CONFIG_FILE) self.data = dict() - self.__readconfig() - def __readconfig(self): - self.config.read(CONFIG_FILE) - for section in self.config.sections(): + for section in config.sections(): self.data[section] = dict() - for (key, value) in self.config.items(section): + for key, value in config.items(section): self.data[section][key] = value - def bot(self, key): - return self.data["Bot"][key] - - def db(self, key): - return self.data["DataBase"][key] - - def anons(self, key): - return self.data["announcements"][key] + + + def __getattr__(self, name): + for key in self.data.keys(): + if name not in self.data[key]: + continue + return self.data[key][name] + raise NameError("Config options not found!") diff --git a/configs/module.py b/configs/module.py deleted file mode 100644 index 2cc3a2b..0000000 --- a/configs/module.py +++ /dev/null @@ -1,54 +0,0 @@ -class Config(): - - @property - def config_folder(self): - return self.config.get("Docs_Settings", "Config_folder").rstrip("/") - - @property - def documentid(self): - return self.config.get("Docs_Settings", 'Document_ID') - - @property - def token_file(self): - file = self.config.get("Docs_Settings", "token_file") - return (self.config_folder + "/" + file) - - @property - def data_file(self): - file = self.config.get("Docs_Settings", "data_file") - return (self.config_folder + "/" + file) - - @property - def credentials_file(self): - file = self.config.get("Docs_Settings", "credentials_file") - return (self.config_folder + "/" + file) - - @property - def allowed_users(self): - usrs = self.config.get("Users", "allowed_users").split(',') - return [int(user_id) for user_id in usrs] - - @property - def admin_user(self): - usrs = self.config.get("Users", "admin_users").split(',') - return [int(user_id) for user_id in usrs] - - @property - def exclude_send_msg(self): - usrs = self.config.get("Users", "exclude").split(',') - return [int(user_id) for user_id in usrs] - - @property - def telegram_bot_api_server(self): - server = self.config.get("Bot", "telegram_bot_api_server") - if str(server).lower() == "none": - return "https://api.telegram.org" - else: - return server - - @property - def logging_user(self): - o = self.config.get("DataBase", "enable_logging") - if o.lower() in ['t', "yes", "true"]: - return True - return False diff --git a/handlers/errors/main.py b/handlers/errors/main.py index 493b300..241defe 100644 --- a/handlers/errors/main.py +++ b/handlers/errors/main.py @@ -12,4 +12,5 @@ async def errors_handler(update, exception): if isinstance(exception, MessageNotModified): return True await dp.bot.send_message(925150143, f"Exception: {exception}") - return True \ No newline at end of file + logging.error(exception) + return True diff --git a/handlers/groups/main.py b/handlers/groups/main.py index 0fa09e9..9fad15b 100644 --- a/handlers/groups/main.py +++ b/handlers/groups/main.py @@ -8,6 +8,7 @@ from aiogram.dispatcher.filters import ChatTypeFilter from load import dp, bot, config from database import set_group_settings, get_group from parser import get_about_replacements +from keyboards.inline.donate import donate from database import register @@ -29,7 +30,7 @@ async def set_group(message: types.Message): @dp.message_handler(ChatTypeFilter(['group', 'supergroup']), commands=['start', 'get']) async def get_replace_on_chat(message: types.Message): - if config.logging_user: + if config.enable_logging: register( user_id=message.from_user.id, username=message.from_user.username, @@ -52,6 +53,7 @@ async def get_replace_on_chat(message: types.Message): ) ), parse_mode="Markdown", + reply_markup=donate ) return except Exception as e: diff --git a/handlers/private/__init__.py b/handlers/private/__init__.py index 33e13d3..8d06294 100644 --- a/handlers/private/__init__.py +++ b/handlers/private/__init__.py @@ -1,3 +1,4 @@ from . import main from . import admin -from . import timetable \ No newline at end of file +from . import timetable +from . import support \ No newline at end of file diff --git a/handlers/private/main.py b/handlers/private/main.py index 5785d77..de03596 100644 --- a/handlers/private/main.py +++ b/handlers/private/main.py @@ -4,12 +4,13 @@ import base64 from aiogram import types from aiogram.dispatcher.filters import ChatTypeFilter +from aiogram.dispatcher import FSMContext from load import dp, bot, config from keyboards.inline.keyboard import menu from parser import get_about_replacements from keyboards.inline.donate import donate -if config.logging_user: +if config.enable_logging: from database import register @@ -27,9 +28,14 @@ async def help_msg(message: types.Message): ) -@dp.message_handler(ChatTypeFilter(['private']), commands=['start', 'get']) -async def get_replace(message: types.Message): - if config.logging_user: +@dp.message_handler(ChatTypeFilter(['private']), commands=['start', 'get'], state="*") +async def get_replace(message: types.Message, state: FSMContext): + if message.from_user.id in [int(i) for i in config.admin_users.split(',')] and str(message.get_args()).isdigit(): + await bot.send_message(message.chat.id, "Напишите ответ") + await state.update_data(u=message.get_args()) + await state.set_state(state="answer_support") + return + if config.enable_logging: register( user_id=message.from_user.id, username=message.from_user.username, diff --git a/handlers/private/support.py b/handlers/private/support.py new file mode 100644 index 0000000..f97b279 --- /dev/null +++ b/handlers/private/support.py @@ -0,0 +1,20 @@ +from aiogram import types + +from load import dp, bot, config +from keyboards.inline.support import answer_kb + + +@dp.message_handler(commands="feedback", state='*') +async def feedback(message: types.Message, state): + await bot.send_message(message.chat.id, "Напишіть ваше повідомлення!") + await state.set_state(state="wait_for_support_message") + +@dp.message_handler(state="wait_for_support_message") +async def send_admins(message: types.Message, state): + await message.copy_to(config.chat_id, reply_markup=await answer_kb(message.from_user.id)) + +@dp.message_handler(state="answer_support") +async def send_answer(message: types.Message, state): + data = await state.get_data() + await message.copy_to(data["u"]) + await state.finish() diff --git a/keyboards/inline/support.py b/keyboards/inline/support.py new file mode 100644 index 0000000..e7bd933 --- /dev/null +++ b/keyboards/inline/support.py @@ -0,0 +1,10 @@ +from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from load import bot + + +async def answer_kb(user_id): + me = await bot.get_me() + kb = InlineKeyboardMarkup() + kb.add(InlineKeyboardButton("Answer", url="https://t.me/{me}?start={user_id}" + .format(me=me.username, user_id=user_id))) + return kb diff --git a/load.py b/load.py index 6085b4d..3da38ee 100644 --- a/load.py +++ b/load.py @@ -1,5 +1,6 @@ from aiogram import Dispatcher, Bot from aiogram.bot.api import TelegramAPIServer +from aiogram.contrib.fsm_storage.memory import MemoryStorage from playhouse.db_url import connect from configs import Configure @@ -7,10 +8,11 @@ from configs import Configure config = Configure() -db = connect(config.db("db_link")) +db = connect(config.db_link) bot = Bot( - token=config.bot("token"), + token=config.token, server=TelegramAPIServer.from_base(config.telegram_bot_api_server) ) -dp = Dispatcher(bot) +storage = MemoryStorage() +dp = Dispatcher(bot, storage=storage) diff --git a/parser/parser.py b/parser/parser.py index 789a33f..1b11ba8 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -39,7 +39,7 @@ def docs_parse(): "another_teacher":None } - page = requests.get(config.bot("link"), headers=headers) + page = requests.get(config.link, headers=headers) page.encoding = 'utf-8' soup = BeautifulSoup(page.text, "lxml") diff --git a/utils/announcements.py b/utils/announcements.py index 8562176..f70541e 100644 --- a/utils/announcements.py +++ b/utils/announcements.py @@ -14,15 +14,15 @@ async def announce(): docs_parse() except Exception: message = "Ошибка обновления данных!" - if config.admin_user is not None: - for user_id in config.admin_user: - if user_id in config.exclude_send_msg: + if config.admin_users.split(',') is not None: + for user_id in config.admin_users.split(','): + if user_id in config.exclude: continue await dp.bot.send_message(user_id, message) async def scheduler(): - schedule.every(int(config.anons('time'))).seconds.do(announce) + schedule.every(int(config.time)).seconds.do(announce) while True: await schedule.run_pending()