Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send pdf file back to user using python telegram bot?

I tried to use update.sendDocument(chat_id = update.message.chat_id, document = open(something.pdf, 'b')) but it did not return me a pdf file. Any help?

like image 222
codingsospls Avatar asked Oct 16 '22 02:10

codingsospls


People also ask

Can a Telegram bot send message to user?

In order to send a message to "@Username", you will need them to start your bot, and then store the username with the user_id. Then, you can input the username to find the correct user_id each time you want to send them a message.

Can I send PDF on Telegram?

You can send and receive any kind of file via Telegram. When you want to send a file to someone with any app, the most important issue is speed and security for transferring data.

How do I download a file or photo that was sent to my Telegram bot?

The file can then be downloaded via the link https://api.telegram.org/file/bot/, where is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again. For the moment, bots can download files of up to 20MB in size.

How can I send message to Telegram using Python?

Getting StartedOpen the telegram app and search for @BotFather. Click on the start button or send “/start”. Then send “/newbot” message to set up a name and a username. After setting name and username BotFather will give you an API token which is your bot token.


2 Answers

For reference, here's a full working example, that worked for me. If it still doesnt work, ensure that the pdf docs you send are below 20 MB as per https://core.telegram.org/bots/api#sending-files

#!/usr/bin/python
import sys
import time
import telepot
import cookie
from telepot.loop import MessageLoop
import pdb

def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(content_type, chat_type, chat_id)
    if content_type == 'text' and msg["text"].lower() == "news":
        # let the human know that the pdf is on its way        
        bot.sendMessage(chat_id, "preparing pdf of fresh news, pls wait..")
        file="/home/ubuntu/web/news.pdf"

        # send the pdf doc
        bot.sendDocument(chat_id=chat_id, document=open(file, 'rb'))
    elif content_type == 'text':
        bot.sendMessage(chat_id, "sorry, I can only deliver news")

# replace XXXX.. with your token
TOKEN = 'XXXXXXXXXXXXXXXXXXXX'
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')
# Keep the program running.
while 1:
    time.sleep(10)
like image 59
trohit Avatar answered Oct 18 '22 03:10

trohit


To send document to user you must have the file_id of that document.

Assuming that the user sent the document to bot and you want to send back to user, this is my solution:

def file_handler (update, context):
    chat_id = update.message.chat_id
    ## retrieve file_id from document sent by user
    fileID = update.message['document']['file_id']
    context.bot.sendDocument(chat_id = chat_id,
                             caption = 'This is the file that you've sent to bot',
                             document = fileID)

And on def main() you must add:

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

With this last line, your bot will understand that when user send it a document, and only a document, it will call def file_handler.

like image 44
Rafael Colombo Avatar answered Oct 18 '22 03:10

Rafael Colombo