Home>

I am writing a telegram bot. The bot generates a file.csv next to main.py. This file must be sent from the server to the chat.

def send_document(self, chat_id, document):
    params= {'chat_id': chat_id, 'document': document}
    method= 'sendDocument'
    headers= {'content-type': "multipart/form-data"}
    resp= requests.post(self.api_url + method, headers, params)
    return resp
bot.send_document(last_chat_id, open('file.csv', 'rb'))

Tried like this, it returns an error{"ok":false,"error_code":400,"description":"Bad Request: there is no document in the request"}

Tell me what's wrong?

Well, he writes to you that there is no document in the request. What -it can mean ^.^

And2022-01-30 01:38:37

I understand what he writes to me, but I don’t understand why and what to do so that the document in the request is :) in the code before the request I write print (document) in the console I see <_io.BufferedReader name='file.csv'>, which means the document should be inserted into the request , is not it so?

Nikita2022-01-30 01:38:37

Do you just need to send this file or what is inside the file?

Huskey2022-01-30 01:38:37

send the file

Nikita2022-01-30 01:38:37
  • Answer # 1

    I'll show an example of how I posted here:

    def save(message):
        file_to_send= open('client.txt', 'rb') #Open in binary mode
        bot.send_document(chat_id=(chat id), data=file_to_send) #Send file
        file_to_send.close()
        os.remove('client.txt')
    
  • Answer # 2

    I'll show an example of how I posted here:

    def save(message):
        file_to_send= open('client.txt', 'rb') #Open in binary mode
        bot.send_document(chat_id=(chat id), data=file_to_send) #Send file
        file_to_send.close()
        os.remove('client.txt')