-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (114 loc) · 6.25 KB
/
main.py
File metadata and controls
132 lines (114 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
def create_folder(folder_name):
"""Создание папки"""
try:
os.makedirs(folder_name)
print(f"Папка {folder_name} успешно создана")
except FileExistsError:
print(f"Папка {folder_name} уже существует")
def create_file_with_extension(file_name, extension):
"""Создание файла с расширением"""
file_path = os.path.join(os.getcwd(), file_name + '.' + extension)
with open(file_path, 'w') as f:
print(f"Файл {file_path} успешно создан")
def main():
name_project = input("Введите название проекта: ")
create_folder(f"{name_project}/handlers") # Создание папки
create_folder(f"{name_project}/keyboards") # Создание папки
create_folder(f"{name_project}/system") # Создание папки
create_folder(f"{name_project}/setting") # Создание папки
create_file_with_extension(f'{name_project}/requirements', 'txt') # Создание файла с расширением
create_file_with_extension(f'{name_project}/README', 'md') # Создание файла с расширением
create_file_with_extension(f'{name_project}/main', 'py') # Создание файла с расширением
create_file_with_extension(f'{name_project}/', 'gitignore') # Создание файла с расширением
create_file_with_extension(f'{name_project}/system/dispatcher', 'py') # Создание файла с расширением
create_file_with_extension(f'{name_project}/setting/config', 'ini') # Создание файла с расширением
create_file_with_extension(f'{name_project}/handlers/handlers', 'py') # Создание файла с расширением
create_file_with_extension(f'{name_project}/keyboards/keyboards', 'py') # Создание файла с расширением
# --- Создаём .env ---
env_path = os.path.join(name_project, ".env")
with open(env_path, "w", encoding="utf-8") as f:
f.write("BOT_TOKEN=\n")
print(f"Файл {env_path} успешно создан")
# --- dispatcher.py ---
# Шаг 1: Создание файла и запись кода
with open(f'{name_project}/system/dispatcher.py', 'w', encoding='utf-8') as file:
code_lines = [
'import os\n',
'from dotenv import load_dotenv\n',
'from aiogram import Bot, Dispatcher, Router\n',
'from aiogram.fsm.storage.memory import MemoryStorage\n\n',
'# Загружаем переменные окружения\n',
'load_dotenv()\n\n',
'BOT_TOKEN = os.getenv("BOT_TOKEN")\n',
'if not BOT_TOKEN:\n',
' raise ValueError("❌ Не найден BOT_TOKEN в .env файле!")\n\n',
'bot = Bot(token=BOT_TOKEN)\n',
'storage = MemoryStorage()\n',
'dp = Dispatcher(storage=storage)\n\n',
'ADMIN_USER_ID = (535185511, 301634256)\n\n',
'router = Router()\n',
'dp.include_router(router)\n',
]
file.writelines(code_lines)
# --- main.py ---
# Шаг 2: Создание файла и запись кода
with open(f'{name_project}/main.py', 'w', encoding='utf-8') as file:
code_lines = [
'import asyncio\n',
'import logging\n',
'import sys\n\n',
'from loguru import logger\n',
'from handlers.handlers import register_greeting_handler\n',
'from system.dispatcher import dp, bot\n\n',
'logger.add("logs/log.log", retention="1 days", enqueue=True) # Логирование бота\n\n',
'async def main() -> None:\n',
' register_greeting_handler()\n',
' await dp.start_polling(bot)\n\n',
'if __name__ == "__main__":\n',
' logging.basicConfig(level=logging.INFO, stream=sys.stdout)\n',
' asyncio.run(main())\n\n',
]
file.writelines(code_lines)
# --- handlers/handlers.py ---
# Шаг 3: Создание файла и запись кода
with open(f'{name_project}/handlers/handlers.py', 'w', encoding='utf-8') as file:
code_lines = [
'from aiogram.filters import CommandStart\n',
'from aiogram.types import Message\n',
'from loguru import logger\n\n',
'from system.dispatcher import bot, dp\n\n',
'@dp.message(CommandStart())\n',
'async def command_start_handler(message: Message) -> None:\n',
' user_id = message.from_user.id\n',
' user_name = message.from_user.username\n',
' user_first_name = message.from_user.first_name\n',
' user_last_name = message.from_user.last_name\n',
' user_date = message.date.strftime("%Y-%m-%d %H:%M:%S")\n',
' logger.info(f"{user_id} {user_name} {user_first_name} {user_last_name} {user_date}")\n\n',
' sign_up_text = "Добро пожаловать!"\n',
' await bot.send_message(message.from_user.id, sign_up_text, disable_web_page_preview=True)\n\n',
'def register_greeting_handler():\n',
' """Регистрируем handlers для бота"""\n',
' dp.message.register(command_start_handler)\n\n',
]
file.writelines(code_lines)
# --- keyboards/keyboards.py ---
# Шаг 4: Создание файла и запись кода (клавиатуры)
with open(f'{name_project}/keyboards/keyboards.py', 'w', encoding='utf-8') as file:
code_lines = [
'from aiogram.types import ReplyKeyboardMarkup, KeyboardButton\n\n',
'start_keyboard = ReplyKeyboardMarkup(resize_keyboard=True)\n',
'start_keyboard.add(KeyboardButton("👋 Привет!"))\n\n',
]
file.writelines(code_lines)
# requirements.txt
with open(f'{name_project}/requirements.txt', 'w', encoding='utf-8') as file:
code_lines = [
'loguru\n',
'aiogram\n',
'python-dotenv\n',
]
file.writelines(code_lines)
if __name__ == '__main__':
main()