Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading