Posting a file via curl to the telegram bot api
Asked Answered
B

2

6

I've tried the following:

curl -F name=document -F upload=@<path_to_the_file> \ 
     -H "Content-Type:multipart/form-data" \ 
     "https://api.telegram.org/bot<token>/sendDocument?chat_id=<chat_id>"

It returns {"ok":false,"error_code":400,"description":"[Error]: Bad Request: there is no document in request"}

What have I done wrong?

Here is some documentation on sendDocument method.

Bulwerlytton answered 22/11, 2015 at 9:50 Comment(0)
L
21

Hope this helps. Note that there is "bot" before <token>.

curl -F document=@"path/to/some.file" https://api.telegram.org/bot<token>/sendDocument?chat_id=<chat_id>
Leotaleotard answered 22/11, 2015 at 10:22 Comment(5)
Oh, thanks, but I do actually type the correct url when I use real credentials. I've edited the question.Bulwerlytton
My mistake was that I typed upload=@<path_to_the_file> instead of document=@<path_to_the_file>. Thanks again.Bulwerlytton
Glad to help. Good luck.Leotaleotard
Yep. It is not clear in their guideline. Shame on them.Madrid
This answer is perfect for files but not pictures because the latter don't auto-display (need to click to make them load). For those who want to send pictures, replace "sendDocument" with "sendPhoto" and "document" by "photo". Example: curl -F photo=@"./image.jpg" https://api.telegram.org/bot$token/sendPhoto?chat_id=$chat_idChervil
R
0

To send not just a file, but a file + text caption, use this:

BOT_TOKEN='1234567890:Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
CHAT_ID='-987654321'
FILE_TO_SEND='./path/to/file.zip'
TEXT_TO_SEND='some *example text* _caption_ here, use Markdown v1 syntax'

curl -4 -s -S -L -w"\n" -o- \
    -F document=@"${FILE_TO_SEND}" \
    -F parse_mode='Markdown' \
    -F caption="${TEXT_TO_SEND}" \
    -X POST https://api.telegram.org/bot${BOT_TOKEN}/sendDocument \
    -F chat_id="${CHAT_ID}"

Some explanation:

  • BOT_TOKEN= - API token of telegram bot you are going to use to post (it would be author of your message) (how to get API token for bot)
  • CHAT_ID= - ID of target chat you are going to post to. Note that IDs are negative numbers (should have dash - before the numbers)
  • FILE_TO_SEND= - path to the file you are going to send, could both be relative (./logs/app.log) or absolute (/home/alex/photo.jpg)
  • TEXT_TO_SEND= - text caption to your file (in this example with Markdown v1 syntax, not the same as Markdown v2, note that you should manually escape characters _, *, `, [ that are not the part of Markdown syntax, but are just these characters themselves)

curl flags:

  • -s - The same as --silent. Do not show progress meter, only show server response. Do not show errors (but we will enable showing errors in next -S param).
  • -S - The same as --show-error. Show error messages, suppressed by -s.
  • -L - The same as --location. Follow HTTP(S) redirects, if requested URL responses with 3XX code. Just as your web-browser does.
  • -w"\n" - The same as --write-out. Only useful when debugging, could be removed from code on production. Will add newline symbol to the end of response, so your console/terminal wouldn't break, especially when dealing with non-alphabetical symbols, emoji and other weird Unicode stuff.
  • -o- - The same as --output. Force curl to show it's output to stdout (your console/terminal), not to file or elsewhere. Just as usual commands like ls does.
  • -4 - The same as --ipv4. Force curl to connect via IPv4. Only useful in certain cases, when your system has IPv6 connectivity, but it works slowly or some other weird way. Could be removed if you are sure you don't have IPv6-related problems at all (or if you don't have IPv6 at all).
Robeson answered 27/10, 2023 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.