0

In this block of code:

@bot.message_handler(content_types=["document"])
def sending_document(message):
    if ".docx" in message.document.file_name:
        bot.send_document(message.chat.id, message.document) #<- problem

I don't know where to get the document from the message.

I tried to do a lot of things but it didn't work.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
George
  • 3
  • 1

2 Answers2

0

Disclaimer: I'm familiar with the telebot library, but with the Telegram Bot API itself.


The sendDocument expects for the parameter document either a URL, a file id or a multipart-uploaded file. However, message.document is a Document object. The file_id of that document should be available via message.document.file_id, so I'd expect bot.send_document(message.chat.id, message.document.file_id to work.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
0

As CallMeStag mentioned, you can use bot.send_document(message.chat.id, message.document.file_id (this is correct).
I'd like to add something

bot.send_document accepts chat_id and "document" as arguments.
As said in Telebot documentation document may be file id from telegram servers or url to any file in the internet

However i didn't see they mentioning you can pass file descriptor. Example:

import telebot

bot = telebot.TeleBot("token here")

f = open("any_existing_file")
bot.send_document(chat_id, f)
f.close()

if you just want send them document untouched you should use file_id, but if you want to modify document before sending, you can send it this way.

P.S. if you are having hard time figuring out properties of variables (e. g. message) consider using VS Code and explicitly setting variable type:

# example
bot: telebot.TeleBot = telebot.TeleBot("token here")

#....
@bot.message_handler(...)
def funcName(message: telebot.types.Message):
    # do stuff
#...

Now VS Code will know what type variables have and it will show you all their properties helping you learn.
You don't need explicitly set variable type everytime, but it helps to write code.