F
F
From Prog2021-07-06 17:00:10
Python
From Prog, 2021-07-06 17:00:10

How to send a file to aiogram?

In order to make an echo bot, I had to write the following code:

bot = Bot(token = TOKEN)
mybot = Dispatcher(bot)

@mybot.message_handler(content_types = ContentType.TEXT)
async def messages(message: types.Message):
  await  message.reply(message.text)


if __name__ == '__main__':
  executor.start_polling(mybot)

This is the only way it works, although before that I wrote mybot.send_message , as in all tutorials, and it showed an error that Dispather does not have a send_message method, and the same thing with send_document

I tried this with the document:
@mybot.message_handler(commands = 'file')
async def send_file(message: types.Document):
  await message.reply(open('settings\\settings.py', 'r'))

Bot sends <_io.TextIOWrapper name='settings\\settings.py' mode='r' encoding='UTF-8'

> and in general, if in all the tutorials and "explainers" they show what to do in a way that I can't do, then where can I learn this library

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-07-06
@FromProg

60e4699dc8680996773081.jpeg
The IDE bakes on other reply methods, among which is reply_document
Well, the file must be opened in byte read mode.

await message.reply_document(open('settings\\settings.py', 'rb'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question