Skip to content

Instantly share code, notes, and snippets.

@SanariSan
Last active September 17, 2024 17:30
Show Gist options
  • Save SanariSan/4c7cca1aef10dfe0e27e55cfd97e9a53 to your computer and use it in GitHub Desktop.
Save SanariSan/4c7cca1aef10dfe0e27e55cfd97e9a53 to your computer and use it in GitHub Desktop.
Telegram HTTP bot API via CURL | Send text, photos, documents, etc.

Here are some examples on how to use Telegram bot api via CURL

Prerequisites

For getting messages in private chat with bot

  • Create a bot using @BotFather, get it's token
  • Start conversation with bot
  • Run following curl command
curl https://api.telegram.org/bot<TO:KEN>/getUpdates | grep -Po '"from":{"id":.+?,'

or


For getting messages in public/private chat where bot invited

  • Create a bot using @BotFather, get it's token
  • Invite your bot to desired chat
  • Write message like this /test @your_bot_tag
  • Run following curl command
curl https://api.telegram.org/bot<TO:KEN>/getUpdates | grep -Po '"chat":{"id":.+?,'

You will get output like the one below, keep chat id: "chat":{"id":-1001182736542,


Sending messages either to yourself in the chat with bot or to the group chat

Environment variables used in examples

# bot token
$TOKEN=abc:de123
# your acc or group chat id
$CHAT_ID=12345
To supress curl output, add -s key + > /dev/null in the end

Send text messages

curl -X POST -H "Content-Type:multipart/form-data" -F chat_id=$CHAT_ID -F text="message" "https://api.telegram.org/bot$TOKEN/sendMessage"


Send document from disk

curl -X POST -H "Content-Type:multipart/form-data" -F document=@"path/to/some.file" "https://api.telegram.org/bot$TOKEN/sendDocument"


Example of piping an image from stdout to curl

Creating qr code from string, piping stdout to curl, sending it as an image

qrencode -s 6 -o - "Some text to encode" | curl -X POST -H "Content-Type:multipart/form-data" -F chat_id=$CHAT_ID -F photo=@- "https://api.telegram.org/bot$TOKEN/sendPhoto"

For sending other entity types, replace /sendPhoto and photo with the following:

/sendAudio audio /sendPhoto photo /sendVideo video /sendAnimation animation /sendVoice voice /sendVideoNote video_note


More types, as well as required/optional fields for each type, could be found here
Some examples that might help you - here
@MonteLogic
Copy link

Good gist.

For Unix users replace "|" with ";" to make commands work.

@Yobb17
Copy link

Yobb17 commented Oct 18, 2023

<script src="https://gist.github.com/rpanachi/c7fce3a3e25d2f06c4b8c2c3ff8ea75f.js"></script>

@skostakov
Copy link

HI, how to download file from chat group with curl?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment