TypeError: Dispatcher._init_() takes 1 positional argument but 2 were given
and if i am doing this “dp = Dispatcher()” it will throw an error —
AttributeError: ‘Dispatcher’ object has no attribute ‘message_handler’
so help me
here is my code –
import os import logging import re import toml import asyncio from aiogram import Bot, Dispatcher, types from telegram.constants import ParseMode CONFIG_FILE = "config.toml" # Function to create a config file with the bot token def create_config_file(): bot_token = input("Enter your Telegram bot token: ") config_data = {"telegram": {"bot_token": bot_token}} with open(CONFIG_FILE, "w") as f: toml.dump(config_data, f) # Load bot token from the TOML file or create the file if it doesn't exist if not os.path.exists(CONFIG_FILE): create_config_file() with open(CONFIG_FILE, "r") as f: config = toml.load(f) bot_token = config["telegram"]["bot_token"] # Initialize the Telegram bot with aiogram bot = Bot(token=bot_token) dp = Dispatcher(bot) def extract_episode_number(file_name): match = re.search(r"E(d+)", file_name) if match: return int(match.group(1)) return 0 # Enable logging (optional but recommended) logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) # Define a function to handle the "/start" command @dp.message_handler(commands=['start']) async def start(message: types.Message): await message.reply_text( "Hi! I'm a bot that can upload video files. Just send me a video file and I'll do the rest." ) # Define a function to handle the video file upload @dp.message_handler(content_types=types.ContentType.VIDEO) async def handle_video_upload(message: types.Message): # Download the video file to the local storage file_id = message.video.file_id file_path = f"{file_id}.mp4" await bot.download_file_by_id(file_id, file_path) # Send the video file back to the chat with open(file_path, "rb") as video: await message.reply_document(video) # Send a confirmation message await message.reply_text("Video uploaded successfully!") # Define a function to handle errors @dp.errors_handler() async def error_handler(update: types.Update, exception: Exception): logging.error(f"Update {update} caused error {exception}") # Define a function to handle the directory upload @dp.message_handler(commands=['dir']) async def handle_directory_upload(message: types.Message): undone = [] # Get the directory name from the message directory_name = " ".join(message.get_args()) # Check if the directory exists if not os.path.exists(directory_name): await message.reply_text("Directory not found!") return await message.reply_text("Please wait while uploading the files...") # Get the list of video files in the directory video_files = [] for root, _, files in os.walk(directory_name): for file in files: if file.endswith(".mp4"): video_files.append(os.path.join(root, file)) sorted_episode_list = sorted(video_files, key=lambda file: extract_episode_number(file)) # Upload each video file in the directory for video_file in sorted_episode_list: logging.info(f"Uploading: {video_file}") with open(video_file, "rb") as f: try: caption = os.path.relpath(video_file, directory_name) await message.reply_document(f, caption=caption) except Exception as e: logging.error(f"Error uploading {video_file}: {e}") undone.append(video_file) if undone: undone_files = "n".join([os.path.relpath(file, directory_name) for file in undone]) await message.reply_text(f"These files could not be uploaded:n{undone_files}") else: await message.reply_text("All files uploaded successfully!") # Main function to start the bot and handle the polling loop async def main(): logging.info("Bot Started!") await dp.start_polling() try: while True: await asyncio.sleep(1) except KeyboardInterrupt: logging.info("Bot stopped by user") finally: await dp.stop_polling() if __name__ == '__main__': asyncio.run(main())
i want to try create an uploader but it didnt work
so can anyone told me what should i do
New contributor
learner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.