2

How can I send (not forward) file existing on Telegram to user according to file ID?

I know all files uploaded on Telegram have unique ID. How can I extract that ID and what's the function or method to send that file by extracted ID? (I use Python Telepot library)

import telepot
from telepot.loop import MessageLoop
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg,'chat')
if content_type == 'text':
    user_msg_text = msg['text']
    if user_msg_text == '/start':
        bot.sendDocument(chat_id=chat_id, )
TOKEN = "479761462:AAE8yqX2RGCbynHJgShIdJzCZWYF9SSBUkU"
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
                  'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

what I should enter on second argument of sendDocument() method?

Ali Heydari
  • 145
  • 3
  • 11

1 Answers1

1
import telepot
from telepot.loop import MessageLoop
def on_chat_message(msg):
   content_type, chat_type, chat_id = telepot.glance(msg,'chat')
     if content_type == 'document':
        file_id = msg['document']['file_id']
        print(file_id)
TOKEN = "479761462:AAE8yqX2RGCbynHJgShIdJzCZWYF9SSBUkU"
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
              'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

You should first extract file_id from document file.when you run your bot,send a document to bot,you can print it and copy it,and past it to this command: bot.sendDocument(chat_id=chat_id,file_id) You should place the phrase you copied instead of file_id.

samira
  • 118
  • 1
  • 2
  • 18
  • thank you for answering but I found that the file ID that was extracted could not be used for another bot until it was sent to the bot. Another problem I'm facing is that if the bot does not send that file to anyone or uses it in general, I encounter a file fail error. – Ali Heydari Mar 22 '18 at 07:32
  • When you upload the file in your bot,then you can send the file to every one and every users that you have their chat_id. – samira Mar 30 '18 at 12:40