1

I'm coding a Telegram bot and I need to send files to users. I want to get file_id of files I want to send. I am following this answer.

To get file_id, I should send the file to my Telegram bot. The question is how to send files via my bot to itself? (maybe using the API?)

maugch
  • 1,276
  • 3
  • 22
  • 46
cooleck
  • 343
  • 1
  • 5
  • 14
  • I'm not entirely sure if this helps you for any kind of file: Methods like [this one](https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html?highlight=send_message#telegram.Bot.send_photo) accept both an integer (chat ID) and a string (@channel_name) for `chat_id`. So with the [bot's username](https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html?highlight=send_message#telegram.Bot.name), I guess you could send a message from the bot to itself. – Michael H. Jan 07 '19 at 19:40
  • No, it doesn't work. – cooleck Jan 07 '19 at 21:10
  • You can NOT send anything from a bot to a bot (itself or other bots). – Ali Hashemi Jan 09 '19 at 11:33

1 Answers1

1

You can send files without file_id:

There are three ways to send files (photos, stickers, audio, media, etc.):

  • If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
  • Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
  • Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files

You can use file_id only if you have already sent it to someone before (using URL or multipart/form-data).


So if a list of files you want to send is predefined, "static", you can upload all of them once, get their file_id's (using getUpdates method for example), store associations between files and their respective file_id's and finally your bot can use only file_id to communicate with users.

On the other hand, if a list of possible files varies, you can send files only via URL or multipart/form-data.

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
  • 1
    I have chosen the third method: Post the file using multipart/form-data. I have already seen a lot of questions about this method and nobody could answer directly on it. So can you explain in details this method, please? – cooleck Jan 10 '19 at 12:10
  • Sure. What's the problem with multipart/form-data? – Ivan Vinogradov Jan 10 '19 at 12:12
  • @cooleck maybe [this question](https://stackoverflow.com/questions/43969042/how-to-send-photo-by-telegram-bot-using-multipart-form-data) (with good answer) helps you? – Ivy Growing Jan 10 '22 at 17:10