Skip to content

Instantly share code, notes, and snippets.

@av1d
Last active May 30, 2024 18:47
Show Gist options
  • Save av1d/956c60fde31cee277b3ec58011e5f1ca to your computer and use it in GitHub Desktop.
Save av1d/956c60fde31cee277b3ec58011e5f1ca to your computer and use it in GitHub Desktop.
Send animated GIF to Telegram with markdown text using cURL or Python

Replace with token, leave the 'bot' part in the URL.
Replace with your chat ID, example: -1234567890.
You may need to put -100 first, like: -1001234567890, depending on chat/group/channel type.
You can also use &parse_mode=html instead of markdown.

Bash:

# send photo with caption (GIF will not be animated in this mode)
curl -X POST https://api.telegram.org/bot<token>/sendPhoto \
     -F chat_id=-<chat> \
     -F photo=@./KBOX_loop.gif \
     -F caption="*Hello*, _channel_!" \
     -F parse_mode=MarkdownV2

# send GIF with caption (GIF will be animated)
curl https://api.telegram.org/bot<token>/sendDocument \
     -F chat_id=-<chat> \
     -F document=@./KBOX_loop.gif \
     -F caption="*Hello*, _channel_!" \
     -F parse_mode=MarkdownV2

Python:

import requests

files = {
    'chat_id': (None, "-1234567890"),
    'document': open('./KBOX_loop.gif', 'rb'),
    'caption': (None, '*Hello*, _channel_!'),
    'parse_mode': (None, 'MarkdownV2'),
}

response = requests.post(
    f"https://api.telegram.org/botABCDEFGH:IJKLMNOP/sendDocument",
    files=files
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment