-1

So i use AI to write me a telegram bot in python, it run but no function.

I try to make a file manager that the bot can know what file i uploaded and do something similar like google drive stuff.

when i use /upload, any file that i upload should be save, but not. And now I'm stuck. Any solution are welcome.

Here's the full script

def upload(update: Update, context: CallbackContext) -> None:
    if update.message.text == '/upload':
        context.chat_data['uploading'] = True
        update.message.reply_text('Upload mode activated. Send files to upload them. Send /uploadstop to stop uploading.')
        return

    if 'uploading' in context.chat_data and context.chat_data['uploading']:
        try:
            file = update.message.document
            file_name = file.file_name
            file_id = file.file_id
            newFile = context.bot.get_file(file_id)
            newFile.download(file_name)

            if 'uploaded_files' not in context.chat_data:
                context.chat_data['uploaded_files'] = []

            context.chat_data['uploaded_files'].append(file_name)
            update.message.reply_text(f'File {file_name} uploaded successfully!')

            # Show the file status as uploading and uploaded to chat_data
            if 'file_status' not in context.chat_data:
                context.chat_data['file_status'] = {}

            context.chat_data['file_status'][file_name] = 'Uploading'
            update.message.reply_text(f'File {file_name} status: {context.chat_data["file_status"][file_name]}')
            context.chat_data['file_status'][file_name] = 'Uploaded'
            update.message.reply_text(f'File {file_name} status: {context.chat_data["file_status"][file_name]}')
        except Exception as e:
            logging.error(f'Error uploading file: {e}')
            update.message.reply_text(f'Error uploading file: {e}')
            

I'm using python-telegram-bot 13.7, and everything run with no error. But it just can't detect any file i uploaded.

I am using python 3.11, if you want use it feel free to copy.

kiwi 52
  • 11

1 Answers1

0

The problem should be that you're appending only the file_name into the list chat_data['uploaded_files']. If you want to keep stored in memory, like in a dict, your file, try doing something like this:

if 'uploaded_files' not in context.chat_data:
    context.chat_data['uploaded_files'] = {}

context.chat_data['uploaded_files'][file_name] = file
update.message.reply_text(f'File {file_name} uploaded successfully!')
CcmU
  • 780
  • 9
  • 22