Search code examples
bashcurltelegramtelegram-bot

How to send multiple file like .txt to Telegram via Curl


I'm trying to send multiple messages such as two text files to Telegram via Curl. It works fine if it would be only one Txt file, but if I'd like to send two Txt files it'll be don't work. I also tried that thing, but not work:

curl -X POST -F "chat_id=-CHAT_ID" \
     -F "document=@/tmp/test/1.txt" \
     -F "document=@/tmp/test/2.txt" \
     https://api.telegram.org/API_TOKEN/sendDocument 

The command works fine without any error, But only the first Txt file is sent. There's no second Txt file. Any idea? Thanks for your helps.


Solution

  • sendDocument can only send a single file.

    You'll need to use sendMediaGroup to send multiple files with a single CURL command.

    Then pass the data as multipart/form-data, in the following format:

    [{ "type": "document", "media": "attach://file1" }, ...]
    

    For more info please read the inputMediaDocument documentation.


    So an example curl command will look like:

    curl -F "chat_id=13245679" \
         -F 'media=[{"type": "document", "media": "attach://file1"}, {"type": "document", "media": "attach://file2" }]' \
         -F "file1=@/tmp/test1.txt" \
         -F "file2=@/tmp/test2.txt" \
         "https://api.telegram.org/bot<BOT-TOKEN>/sendMediaGroup"
    

    Which will result in a mediaGroup containing those 2 files:

    enter image description here