Skip to content

Instantly share code, notes, and snippets.

@revathskumar
Forked from SanariSan/readme.md
Created April 23, 2023 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revathskumar/f957e1374434d7af130bd9c2114495c6 to your computer and use it in GitHub Desktop.
Save revathskumar/f957e1374434d7af130bd9c2114495c6 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