1

I'm trying to develop a Telegram bot using pyTelegramBotAPI package and I need to output multiple documents when user enter an specific /command. I tried below code but it shows an error.

    def pca_papers(self, message):
        bot.send_message(message.chat.id, "Files incoming")
        file = open('https://atikegalle.com/uploads/1514125303.pdf', 'rb')
        bot.send_document(message.chat.id, file)

Error:

File "C:\Users\pasin\Documents\tgbot\pastpapers.py", line 26, in pca_papers
    file = open('https://atikegalle.com/uploads/1514125303.pdf', 'rb')
OSError: [Errno 22] Invalid argument: 'https://atikegalle.com/uploads/1514125303.pdf'
pasindu
  • 529
  • 1
  • 4
  • 16
  • 2
    `'rb'` is used when the file is in your local machine, you seem to providing a link so `'rb'` and `open(..)` isn't needed there. you can just do `bot.send_document(message.chat.id, file=file_url)` – Aditya Yadav Feb 23 '22 at 09:15

1 Answers1

2

open built-in function is for working with local files, not files accessible via HTTP. According to send_document docs 2nd argument might be

an HTTP URL as a String for Telegram to get a file from the Internet

so following should work

def pca_papers(self, message):
    bot.send_message(message.chat.id, "Files incoming")
    bot.send_document(message.chat.id, 'https://atikegalle.com/uploads/1514125303.pdf')

I do not have ability to test, please try and write if it does works.

Daweo
  • 31,313
  • 3
  • 12
  • 25