-1

I need to get a file from user, download it, pass into a function to process it, and then send it back to the user.

I've read the docs multiple times, but I still don't understand how to get a file_id and work with it.

That's an echo code I've tried. It shows a TypeError: 'NoneType' object is not subscriptable on the fileID line.

    update.message.reply_text('give a file')
    chat_id = update.message.chat_id
    fileID = update.message['document']['file_id']
    context.bot.sendDocument(chat_id = chat_id,
                             caption = 'This is the file that you have sent to bot',
                             document = fileID)

1 Answers1

0

Receive your file like this::

from telegram.ext import Updater, MessageHandler, Filters

BOT_TOKEN = ' ... '

def downloader(update, context):
    context.bot.get_file(update.message.document).download()

    # writing to a custom file
    with open("filename", 'wb') as f:
        context.bot.get_file(update.message.document).download(out=f)


updater = Updater(BOT_TOKEN, use_context=True)

updater.dispatcher.add_handler(MessageHandler(Filters.document, downloader))

updater.start_polling()
updater.idle()

--now send back the file like this:

   import requests
    files= { 'file' : open('filepath','rb')}
    
    resp = requests.post('https://api/telegram.org/bot<<token>>/sendfile?chat_id=<>',files=files)
    
    #check response 
    print(resp.status_code)
DontDownvote
  • 182
  • 7