TyRex
TyRex

Reputation: 43

How can I make my bot receive and save a file I send it?

How can I make my bot receive and save a file that I send it in a telegram? I want my bot to receive a pdf file that I send from my cell phone. I'm doing it in python but I'm half lost about it and I don't know how to solve it.

Upvotes: 1

Views: 2275

Answers (2)

Rafael Colombo
Rafael Colombo

Reputation: 435

Keep in mind that to deal with files on Telegram Bot you must have the file_id of the file.

Handling the document:

def file_handler (update, context):
    chat_id = update.message.chat_id    
    
    ## bot geting the document and its file_name
    doc = update.message['document']['file_id'].get_file()
    fileName = update.message['document']['file_name']
    
    ##bot saving this file to a directory on your PC
    doc.download(f'{path_to_your_directory}\\{fileName}')

and on def main():

dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.document, file_handler))

Upvotes: 1

TyRex
TyRex

Reputation: 43

I did this but I didn't get a result

list="filename.pdf"

def CambiarLista(update, context):
    bot = context.bot
    text = update.message.text
    ChatId = update.message.chat_id
    update.message.reply_text('Enviame el documento!')
    file_content = list._get_file()
    update.message.reply_text('Este es el documento que recibi:' + file_content + 'es correcto?')
    keyboard = []
    keyboard.append([KeyboardButton(f'Si, remplazar y guardar', callback_data='9'), KeyboardButton(f'No, eliminar y salir', callback_data='10')])
    reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True)
    update.message.reply_text('Elige una de las siguientes opciones:',  reply_markup=reply_markup)

As you can see in this line I wait to receive the file, but I think I didn't do it right, since I don't receive the following message after that line

file_content = list._get_file()

Upvotes: 0

Related Questions