Search code examples
pythontelegramtelegram-botpy-telegram-bot-api

How to send a document to a Telegram Bot using pyTelegramBotAPI


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'

Solution

  • 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.