Last active
March 15, 2024 17:23
-
-
Save alsolovyev/ea4ff418dcea6e80a1b899aa2871cd75 to your computer and use it in GitHub Desktop.
Bash and Fish scripts to send messages (videos, photos, audio, documents, or plain text) to a Telegram chat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendToTelegram -d "Send a message or document to a telegram chat" | |
# | |
# Description: This script allows you to send text messages or files (images, | |
# videos, audio, or documents) to a Telegram chat using a Telegram bot. | |
# Ensure you set the TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID environment variables. | |
# For more information on creating a Telegram bot, visit: https://t.me/BotFather | |
# | |
# Note: Maximum file size allowed is 50 MB. | |
# | |
# Usage: | |
# - To send a text message: sendToTelegram "Hello, World!" | |
# - To send an image file: sendToTelegram /path/to/image.jpg | |
# - To send a video file: sendToTelegram /path/to/video.mp4 | |
# - To send an audio file: sendToTelegram /path/to/audio.mp3 | |
# - To send a generic document: sendToTelegram /path/to/document.txt | |
# | |
# Author: janeRivas <[email protected]> | |
# Date: 08.03.2024 | |
# | |
set max_file_size (math "50 * 1024 * 1024") # In bytes | |
# Check if telegram token is not set | |
if test -z "$TELEGRAM_BOT_TOKEN" | |
echo (set_color red)"Error: Telegram bot token not provided. Set TELEGRAM_BOT_TOKEN environment variable." | |
echo " For more information visit: $(set_color -u blue)https://t.me/BotFather"(set_color normal) | |
return 1 | |
end | |
# Check if telegram chat ID is not set | |
if test -z "$TELEGRAM_CHAT_ID" | |
echo (set_color red)"Error: Telegram chat ID not provided. Set TELEGRAM_CHAT_ID environment variable." | |
echo " Press 'y' to send a request to update endpoint for the correct chat ID." (set_color normal) | |
echo | |
read -n 1 -P "Press 'y' to send request, or any other key to cancel: " choice | |
if test "$choice" = "y" | |
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates" | |
return | |
else | |
echo "Operation canceled." | |
return | |
end | |
end | |
# Check if a message or file path is provided | |
if test -z "$argv[1]" | |
echo (set_color red)"Error: Need to provide a message or path to a file."(set_color normal) | |
return 1 | |
end | |
# Check if the provided argument is not a file | |
if not test -f "$argv[1]" | |
curl -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \ | |
-F chat_id=$TELEGRAM_CHAT_ID \ | |
-F text="$argv" \ | |
-F disable_notification=true \ | |
-s | |
return | |
end | |
# File validation | |
set file_type (file -b --mime "$argv[1]") # Determine the type of the file | |
set file_size (wc -c < "$argv[1]") # Get the file size in bytes | |
# Check if the file size is within the limit | |
# https://core.telegram.org/bots/api#senddocument | |
if test "$file_size" -ge "$max_file_size" | |
set file_size_in_mb (math "$file_size" / 1024 / 1024) | |
set max_file_size_in_mb (math "$max_file_size" / 1024 / 1024) | |
echo (set_color red)"Error: File size exceeds the maximum allowed size." | |
echo " Max: $max_file_size_in_mb MB. Got: $file_size_in_mb MB."(set_color normal) | |
return 1 | |
end | |
# Prepare request | |
if string match -q '*image*' "$file_type" | |
set endpoint sendPhoto | |
set key photo | |
else if string match -q '*video*' "$file_type" | |
set endpoint sendVideo | |
set key video | |
else if echo "$file_type" | grep -qiE "audio|sound" | |
set endpoint sendAudio | |
set key audio | |
else | |
set endpoint sendDocument | |
set key document | |
end | |
# Send a file | |
curl -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/$endpoint" \ | |
-H "Content-Type:multipart/form-data" \ | |
-F chat_id=$TELEGRAM_CHAT_ID \ | |
-F $key=@$argv[1] \ | |
-F disable_notification=true \ | |
-s | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Description: This script allows you to send text messages or files (images, | |
# videos, audio, or documents) to a Telegram chat using a Telegram bot. | |
# Ensure you set the TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID environment variables. | |
# For more information on creating a Telegram bot, visit: https://t.me/BotFather | |
# | |
# Note: Maximum file size allowed is 50 MB. | |
# | |
# Usage: | |
# - To send a text message: ./sendToTelegram.sh "Hello, World!" | |
# - To send an image file: ./sendToTelegram.sh /path/to/image.jpg | |
# - To send a video file: ./sendToTelegram.sh /path/to/video.mp4 | |
# - To send an audio file: ./sendToTelegram.sh /path/to/audio.mp3 | |
# - To send a generic document: ./sendToTelegram.sh /path/to/document.txt | |
# | |
# Author: janeRivas <[email protected]> | |
# Date: 08.03.2024 | |
# | |
# Get Telegram bot token and chat ID from environment variables | |
token=$TELEGRAM_BOT_TOKEN | |
chat_id=$TELEGRAM_CHAT_ID | |
max_file_size=$((50 * 1024 * 1024)) # MB | |
# Check if telegram token is not set | |
if [ -z "$token" ]; then | |
echo "Error: Telegram bot token not provided. Set TELEGRAM_BOT_TOKEN environment variable." | |
echo " For more information visit: https://t.me/BotFather" | |
exit 1 | |
fi | |
# Check if telegram chat id is not set | |
if [ -z "$chat_id" ]; then | |
echo "Error: Telegram chat ID not provided. Set TELEGRAM_CHAT_ID environment variable." | |
echo " Press 'y' to send a request to update endpoint for the correct chat ID." | |
echo | |
read -n 1 -p "Press 'y' to send request, or any other key to cancel: " choice | |
if [ "$choice" = "y" ]; then | |
curl -s "https://api.telegram.org/bot$token/getUpdates" | |
exit 0 | |
else | |
echo | |
echo "Operation canceled." | |
exit 1 | |
fi | |
fi | |
# Check if a message or file path is provided | |
if [ -z "$1" ]; then | |
echo "Error: Need to provide a message or path to a file." | |
exit 1 | |
fi | |
# Check if the provided argument is not a file | |
if [ ! -f "$1" ]; then | |
curl -X POST "https://api.telegram.org/bot$token/sendMessage" \ | |
-F chat_id=$chat_id \ | |
-F text="$*" \ | |
-F disable_notification=true \ | |
-s | |
exit 0 | |
fi | |
# File validation | |
file_type=$(file -b --mime "$1") # Determine the type of the file | |
file_size=$(wc -c < "$1") # Get the file size in bytes | |
# Check if the file size is within the limit | |
# https://core.telegram.org/bots/api#senddocument | |
if [[ "$file_size" -ge "$max_file_size" ]]; then | |
file_size_in_mb=$(($file_size / 1024 / 1024)) | |
max_file_size_in_mb=$(($max_file_size / 1024 / 1024)) | |
echo "Error: file size exceeds the maximum allowed size. Max: $max_file_size_in_mb MB. Got: $file_size_in_mb MB. " | |
exit 1 | |
fi | |
# Prepare request | |
if [[ "$file_type" == *image* ]]; then | |
endpoint="sendPhoto" | |
key="photo" | |
elif [[ "$file_type" == *video* ]]; then | |
endpoint="sendVideo" | |
key="video" | |
elif echo "$file_type" | grep -qiE "audio|sound|stream"; then | |
endpoint="sendAudio" | |
key="audio" | |
else | |
endpoint="sendDocument" | |
key="document" | |
fi | |
# Send a file | |
curl -X POST "https://api.telegram.org/bot$token/$endpoint" \ | |
-H "Content-Type:multipart/form-data" \ | |
-F chat_id=$chat_id \ | |
-F "$key=@$1" \ | |
-F disable_notification=true \ | |
-s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment