skip to Main Content

In telegram API documentation I see: “You can either pass a file_id as String to resend a photo that is already on the Telegram servers”, but I can’t find ways to get file_id of uploaded file. How can I get it?

7

Answers


  1. Depending on the method (File type) which you chose to send a file, after sending a file to Telegram a response is returned. For example if you send a MP3 file to Telegram using sendAudio method, Telegram returns an Audio object which contains the file ID.
    Source: https://core.telegram.org/bots/api#audio

    Login or Signup to reply.
  2. Say you receive a Message with an array of PhotoSize

    https://core.telegram.org/bots/api#photosize

    As you can see, there’s a file_id, you can use this to send a photo through sendPhoto.

    If we assume Update is an object, with in it a Message object, which in turn provides a Chat object with in it a id of the chat where the initial message came from and an array of PhotoSize (excuse me for using PHP here, but that’s my main language…)

    $update->message->photo is how you can access the array.
    Use some kind of For loop to iterate over the items, or just access the first one if the array isn’t bigger than 1.

    After that, you can use the result(s) to extract the file_id and send it as a string via sendPhoto‘s photo parameter and the Chat ID via the chat_id parameter.

    I hope this helped!

    P.S. Here is a diagram of my current implementation of the API, i hope it brings some clarity to you!

    Login or Signup to reply.
  3. Its depended to your content_types ,for example:

    Video:

    message.video.file_id
    

    Audio:

    message.audio.file_id
    

    Photo:

    message.photo[2].file_id
    

    For more see this link.

    Login or Signup to reply.
  4. In addition to the answers above, you can log Updates that comes to your bot, Either from https://api.telegram.org/bot'.BOT_TOKEN.'/getUpdates or throw updates that come in your application. there you will find a Json property like below:

    {
        "update_id" = 1111111,
        "message" =
            {
                "message_id" = 1111111,
                "from" =
                    {
                        "id" = 111111,
                        ...
                    }
                "chat" =
                    {
                    "id" = 111111,
                    ...
                    }
                "date" = 111111,
                "photo" = 
                    {
                        {
                            "file_id" = HERE IS YOU FILE ID 1,
                            "file_size" => XXXX,
                            "width" => XX,
                            "height" => XX,
                        }
                    }
            }
    }
    
    Login or Signup to reply.
  5. This is the easiest way I’ve found to do it.

    Upload your file to any chat and forward the message to @RawDataBot. It will return something like this:

    {
        "update_id": 754677603,
        "message": {
            "message_id": 403656,
            "from": {
                "id": xxx,
                "is_bot": false,
                "first_name": "xxx",
                "username": "xxx",
                "language_code": "en"
            },
            "chat": {
                "id": xxx,
                "first_name": "xxx",
                "username": "xxx",
                "type": "private"
            },
            "date": 1589342513,
            "forward_from": {
                "id": xxx,
                "is_bot": false,
                "first_name": "xxx",
                "username": "xxx",
                "language_code": "en"
            },
            "forward_date": 1589342184,
            "document": {
                "file_name": "filename.pdf",
                "mime_type": "application/pdf",
                "file_id": "This_Is_The_Thing_You_Need",
                "file_unique_id": "notthis",
                "file_size": 123605
            }
        }
    }
    

    What you need is the string under file_id. Once you have copied that, you can simply the following code to send the message.

        context.bot.sendDocument(chat_id=update.effective_chat.id, 
            document = "Your_FILE_ID_HERE")
    
    Login or Signup to reply.
  6. if you use PHP:

    you can write this line for full size:

    $file_id = $updates['message']['photo'][1]['file_id'];

    and this line for thumb:

    $file_id = $updates['message']['photo'][0]['file_id'];

    Login or Signup to reply.
  7. According to the latest docs (v20.0a6) plenty of classes have been changed. I have found that the easiest way to get started with files is using the effective_attachment property.

    async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    
        media_item = await context.bot.get_file(update.message.effective_attachment[0].file_id)
    
        media_url = media_item.file_path
    

    For declaring the handler there have also been changes to filters, here is a simple way to declare it:

    application.add_handler(MessageHandler(filters.ATTACHMENT, handle_file))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search