Skip to content

Instantly share code, notes, and snippets.

@Nirzak
Forked from SanariSan/readme.md
Created June 29, 2024 16:29
Show Gist options
  • Save Nirzak/36e5089fa311e03b35abf4f36d284c4b to your computer and use it in GitHub Desktop.
Save Nirzak/36e5089fa311e03b35abf4f36d284c4b 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment